Skip to content

PowerShell Base64 Encode & Decode

In PowerShell, you can easily encode and decode strings to/from Base64 using the ConvertTo-Base64 and ConvertFrom-Base64 cmdlets, respectively. Here’s how you can do it:

Encoding a String to Base64:

# String to encode
$originalString = "Hello, this is a string to encode in Base64."

# Convert the string to Base64
$encodedString = [System.Text.Encoding]::UTF8.GetBytes($originalString)
$base64String = [System.Convert]::ToBase64String($encodedString)

# Output the Base64 encoded string
Write-Host "Base64 Encoded String: $base64String"

Decoding a Base64 String back to the original String:

# Base64 string to decode
$base64String = "SGVsbG8sIHRoaXMgaXMgYSBzdHJpbmcgdG8gZW5jb2RlIGluIEJhc2U2NC4="

# Convert the Base64 string to a byte array
$encodedBytes = [System.Convert]::FromBase64String($base64String)

# Convert the byte array back to the original string
$decodedString = [System.Text.Encoding]::UTF8.GetString($encodedBytes)

# Output the decoded string
Write-Host "Decoded String: $decodedString"

Keep in mind that when decoding, ensure that the Base64 string is valid; otherwise, you might encounter errors. Also, make sure to use the correct character encoding (UTF-8 in this example) to ensure the encoded and decoded strings match correctly.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments