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...
PowerShell Tips
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.
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.
During Scriptember, our calendar provides the best possible overview.
Sep 30, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Sep 30, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Sep 30, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Tobias Weltner and Aleksandar Nikolić joinly wrote the blog post series 'Tobias&Aleksandar's PowerShell tips'. So we introduce both of them here:
--------------------
Tobias is a long-time Microsoft MVP and has been involved with the development of PowerShell since its early days. He invented the PowerShell IDE "ISESteroids", has written numerous books on PowerShell for Microsoft Press and O'Reilly, founded the PowerShell Conference EU (psconf.eu), and is currently contributing to the advancement of PowerShell as member in the "Microsoft Cmdlet Working Group". Tobias shares his expertise as a consultant in projects and as a trainer in in-house trainings for numerous companies and agencies across Europe.
--------------------
Aleksandar Nikolić is a Microsoft Azure MVP and co-founder of PowerShellMagazine.com, the ultimate online source for PowerShell enthusiasts. With over 18 years of experience in system administration, he is a respected trainer and speaker who travels the globe to share his knowledge and skills on Azure, Entra, and PowerShell. He has spoken at IT events such as Microsoft Ignite, ESPC, NIC, CloudBrew, NTK, and PowerShell Conference Europe.