Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell convert string to base64 value

Base-64 encoding is a way of taking binary data and turning it into text so that it's more easily transmitted in things like e-mail and HTML form data. I had few requirement to convert / encode text string to Base64 string and vice versa, From encoded Base64 string to convert/decode to normal string.

Related Articles: Powershell GUI encode decode images

In the first script to encode a text string I am using .net object [System.Text.Encoding] and [System.Convert].

Microsoft Powershell Base64 encode decode text base64 system.text.encoding utf8.getbytes system.convert tobase64string.png

#Convert To Base64
$readableText = 'This is Powershell!'

$encodedBytes = [System.Text.Encoding]::UTF8.GetBytes($readableText)
$encodedText = [System.Convert]::ToBase64String($encodedBytes)
$encodedText

This is vice versa process to decode convert base 64 string to readable format. I am using same .net object [System.Text.Encoding] and [System.Convert].

Microsoft Powershell encode decode text image system.convert system.text.encoding frombase64string utf8.getstring encrypt decrypt string bytes base64.png

#Convert from Base64
$encodedValue = 'VGhpcyBpcyBQb3dlcnNoZWxsIQ=='

$decodedBytes = [System.Convert]::FromBase64String($encodedValue)
$decodedText = [System.Text.Encoding]::Utf8.GetString($decodedBytes)
$decodedText

Below are some other other format you can use to base convert (encode decode) text string to.

[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($encodedText))
[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($encodedText))
[System.Text.Encoding]::BigEndianUnicode.GetString([System.Convert]::FromBase64String($encodedText))
[System.Text.Encoding]::Default.GetString([System.Convert]::FromBase64String($encodedText))

Download script here or it is also available on github.com.

Microsoft Powershell base64 convert to base64 text string system.text.encoding unicode getstring system.convert frombase64string ascii bigendianunicode default system.convert.png

Useful Articles
Powershell WPF Charts dashboard demo
Part 1: Create WPF XAML powershell GUI form with Visual studio
Part 2: Powershell and WPF: Build GUI applications tutorial
Part 3: Create shorter Microsoft Powershell WPF automated clean script
Powershell PoshGUI: Convert user to SID and vice versa using
Microsoft Powershell GUI: Change Internet Options connections Lan settings proxy server grayed out
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts

Go Back

Comment

Blog Search

Page Views

6068956

Follow me on Blogarama