In many projects, you will get requirements to encode and decode strings, and PowerShell is the best option. In this tutorial, I will explain how to use PowerShell to encode and decode strings.
To encode and decode strings in PowerShell, use Base64 encoding by converting the string to bytes with [System.Text.Encoding]::UTF8.GetBytes($string) and then encoding with [Convert]::ToBase64String($bytes). Decode by using [Convert]::FromBase64String($base64String) and converting back to a string with [System.Text.Encoding]::UTF8.GetString($bytes).
What are the Encoding and Decoding in PowerShell?
What is Encoding?
Encoding is the process of converting data from one form to another. It is often used to ensure data can be safely transmitted or stored. For example, Base64 encoding is a standard method that converts binary data into an ASCII string format.
What is Decoding?
Decoding is the reverse process of encoding. It converts encoded data back into its original form. For instance, decoding a Base64 string will return the original binary data.
Encode and Decode Strings using PowerShell
There are various methods for encoding and decoding strings in PowerShell. Let us check each one with example and complete script.
I have executed it using VS code, but you can also use Windows PowerShell ISE.
Method 1: Base64 Encoding and Decoding
Base64 Encoding
Base64 encoding is one of the most used methods to encode binary data as text. Here’s a simple example of how to encode a string to Base64 in PowerShell:
# Original string
$originalString = "Hello, PowerShell!"
# Convert the string to bytes
$bytes = [System.Text.Encoding]::UTF8.GetBytes($originalString)
# Encode the bytes to Base64
$base64String = [Convert]::ToBase64String($bytes)
# Output the Base64 encoded string
Write-Output $base64StringOutput:
SGVsbG8sIFBvd2VyU2hlbGwhYou can see the output in the screenshot below:

Base64 Decoding
To decode a Base64 string back to its original form, you can use the following PowerShell script:
# Base64 encoded string
$base64String = "SGVsbG8sIFBvd2VyU2hlbGwh"
# Decode the Base64 string to bytes
$bytes = [Convert]::FromBase64String($base64String)
# Convert the bytes back to the original string
$decodedString = [System.Text.Encoding]::UTF8.GetString($bytes)
# Output the decoded string
Write-Output $decodedStringIn this example, the Base64 string SGVsbG8sIFBvd2VyU2hlbGwh decodes back to Hello, PowerShell!.
Here is the output in the screenshot below:

Method 2: URL Encoding and Decoding
URL Encoding
URL encoding is used to encode special characters in URLs. PowerShell provides a convenient way to perform URL encoding:
# Original URL
$url = "https://powershellfaqs.com/search?query=PowerShell scripting"
# URL encode the string
$encodedUrl = [System.Web.HttpUtility]::UrlEncode($url)
# Output the URL encoded string
Write-Output $encodedUrlURL Decoding
To decode a URL-encoded string, use the following script:
# URL encoded string
$encodedUrl = "https%3a%2f%2fpowershellfaqs.com%2fsearch%3fquery%3dPowerShell+scripting"
# URL decode the string
$decodedUrl = [System.Web.HttpUtility]::UrlDecode($encodedUrl)
# Output the decoded URL
Write-Output $decodedUrlMethod 3: HTML Encoding and Decoding
HTML Encoding
HTML encoding is used to convert characters that have special meanings in HTML into their corresponding HTML entities. Here’s how to encode an HTML string in PowerShell:
# Original HTML string
$htmlString = "<div>PowerShell & Scripting</div>"
# HTML encode the string
$encodedHtml = [System.Web.HttpUtility]::HtmlEncode($htmlString)
# Output the HTML encoded string
Write-Output $encodedHtmlHTML Decoding
To decode an HTML-encoded string back to its original form:
# HTML encoded string
$encodedHtml = "<div>PowerShell & Scripting</div>"
# HTML decode the string
$decodedHtml = [System.Web.HttpUtility]::HtmlDecode($encodedHtml)
# Output the decoded HTML
Write-Output $decodedHtmlConclusion
PowerShell provides different methods to handle Base64, URL, and HTML encoding and decoding.
In this PowerShell tutorial, I explained how to use PowerShell to encode and decode strings.
You may also like:
- Case Insensitive Strings Comparison in PowerShell
- Add Double Quotes in a String in PowerShell
- Compare Strings in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.