In this article, we explore how PowerShell handles URL encoding and decoding using two powerful APIs: System.Web.HttpUtility and System.Uri. We'll dive into examples that showcase the subtle differences between them, helping you choose the right approach for your needs. Whether you're managing web services or working with SharePoint, mastering these techniques will streamline your scripting efforts.

URL encoding take a closer look

Web Services, SharePoint, websites and other entities use URLs, and while URLs are basically strings, they do adhere to certain rules and standards. PowerShell can encode (turn information to a well-formed URL) and decode (extract information from a URL) by accessing appropriate APIs.

Typically, PowerShell scripts use [System.Web.HttpUtility] or [System.Uri]. Ill present both below and highlight their subtle differences.

Here is a decoding example:

$url = 'https://some.site/with%20spa+ces?and+arguments'

[System.Uri]::UnescapeDataString($url
Add-Type -AssemblyName System.Web

[System.Web.HttpUtility]::UrlDecode($url)
Scriptrunner Image

The result show subtle differences UnescapeDataString() does not interpret "+" as an "encoded space":

https://some.site/with spa+ces?and+arguments

https://some.site/with spa ces?and arguments

Here is an encoding example:

$text = 'this text (including & special =? chars)'

[System.Uri]::EscapeDataString($text)

[System.Web.HttpUtility]::UrlEncode($text)

Again, the non-standard "+" is only used by the HttpUtility whereas EscapeDataString() uses Unicode encoding for all special characters including spaces:

this%20text%20%28including%20%26%20special%20%3D%3F%20chars%29

this+text+(including+%26+special+%3d%3f+chars) 
Scriptrunner Image

Choosing the correct API depends on the data you work with: if you both encode and decode (have full control), then you may prefer the HttpUtility shortness. If you must be compatible with external sources, you may want to use the more standardized methods found in System.Uri.

Your ultimate PowerShell Cheat Sheet

Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.The poster is available for download and in paper form.

Get your poster here!

Related links