Skip to the main content.

PowerShell Tips

URL Encoding in PowerShell

Table of contents

Post Featured Image

Tobias & Aleksandar's tip #11:

The two very well-known PowerShell experts have teamed up to share their best and most helpful PowerShell tips.

We will be publishing their scripts over the course of Scriptember in 13 blog posts. Don't miss their insights! Be sure to follow all Scriptember events, listed in our calendar here

 

Overview of all 13 articles published during Scriptember

 

 

URL encoding

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]. I’ll 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)


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) 

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.

 

 

Good2know


Scriptember! Stay tuned:

During Scriptember, our calendar provides the best possible overview.  

Scriptember - hier ist die Übersicht

 

  • Follow us on social media, look for and use the hashtags #Scriptember and #PowerShell.
  • Join our live sessions, overview soon here.
  • Participate in challenges and contests. 
  • Share your knowledge.

 

Find all events here!

 

 

Related links 



Related posts

3 min read

Using enumerations in PowerShell (1/2)

Tobias & Aleksandar's tip #1:

The two very well-known PowerShell experts have teamed up to share their best and most...

5 min read

Utilizing external config data in PowerShell (1/4)

Tobias & Aleksandar's tip #3:

The two very well-known PowerShell experts have teamed up to share their best and most...

4 min read

Always use advanced PowerShell functions

Tobias & Aleksandar's tip #7:

The two very well-known PowerShell experts have teamed up to share their best and most...

About the author: