How to Use PowerShell to Encode and Decode Strings?

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 $base64String

Output:

SGVsbG8sIFBvd2VyU2hlbGwh

You can see the output in the screenshot below:

Encode and Decode Strings using PowerShell

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 $decodedString

In this example, the Base64 string SGVsbG8sIFBvd2VyU2hlbGwh decodes back to Hello, PowerShell!.

Here is the output in the screenshot below:

decode a Base64 string powershell

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 $encodedUrl

URL 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 $decodedUrl

Method 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 $encodedHtml

HTML Decoding

To decode an HTML-encoded string back to its original form:

# HTML encoded string
$encodedHtml = "&lt;div&gt;PowerShell &amp; Scripting&lt;/div&gt;"

# HTML decode the string
$decodedHtml = [System.Web.HttpUtility]::HtmlDecode($encodedHtml)

# Output the decoded HTML
Write-Output $decodedHtml

Conclusion

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.