How to Encode-Decode a URL using PowerShell?

Requirement: Encode or Decode a URL in SharePoint Online using PowerShell.

PowerShell to Decode URL:

URL encoding is the process of converting characters in a URL into a format that can be safely transmitted over the internet. Decoding is the reverse process of converting encoded characters back into their original form. In PowerShell, encoding and decoding URLs can be performed using the [System.Uri] class and its methods.

We need decoded URLs in various scenarios in SharePoint. Say we want to pass the file URL as a parameter to some function.

#Parameter
$URL = "https%3A%2F%2Fcrescent.sharepoint.com%2Fsites%2Fmarketing%2F2018%2FDocuments%2FInfo%20Mgmt%20v2%2Epdf"

#Decode URL
[System.Web.HttpUtility]::UrlDecode($URL)

#Output: 
https://crescent.sharepoint.com/sites/marketing/2018/Documents/Info Mgmt v2.pdf

We can also use the UnescapeDataString method from [System.URI] class.

$EncodedURL = "https%3A%2F%2Fcrescent.sharepoint.com%2Fpersonal%2Fsalaudeen_crescent_com%Documents%2FAssets%20%26%20Inventory.xlsx"

[system.uri]::UnescapeDataString($EncodedURL)

Decode SharePoint Online URL using PowerShell:

We can also use the SharePoint Online method to decode a URL: 

#Import PoweShell Module for SharePoint Online
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

#Function to Decode URL
Function Decode-URL([string]$URL)
{
    Try {
        #Decode the URL
        Return [Microsoft.SharePoint.Client.Utilities.HttpUtility]::UrlKeyValueDecode($URL)
    }
    catch {
        Return "Error Getting De-codedURL: $($_.Exception.Message)"
    }
}

#Parameter
$URL = "https%3A%2F%2Fcrescent.sharepoint.com%2Fsites%2Fmarketing%2F2018%2FShared%20Documents%2FInformation%20Management%20v2%2Epdf"

#Call the function to decode URL
Decode-URL $URL 

Encode URL in PowerShell

In another situation, I had to calculate the length of the encoded URL in SharePoint and achieved it with the following:

$URL = "https://crescent.sharepoint.com/personal/salaudeen_crescent_com/Shared Documents/Assets & Inventory.xlsx"
Write-host([URI]::EscapeUriString($URL)) 

With [System.Web] class, You can encode a URL as:

$URL = "https://crescent.sharepoint.com/personal/salaudeen_crescent_com/Shared Documents/Assets & Inventory.xlsx"
[System.Web.HttpUtility]::UrlPathEncode($URL)

To decode SharePoint URLs, you can also use Online tools like:

sharepoint url decode online

In conclusion, encoding and decoding URLs using PowerShell is a straightforward process that can be performed using the [System.Uri] class and its methods. The EscapeDataString() method can be used to encode URLs, while the UnescapeDataString() method can be used to decode URLs.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “How to Encode-Decode a URL using PowerShell?

Leave a Reply

Your email address will not be published. Required fields are marked *