Base64 Encode and Decode UTF-8 Text in PowerShell
PowerShell
function ConvertTo-Base64Text {
param([Parameter(Mandatory)][AllowEmptyString()][string]$Text)
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($Text))
}
function ConvertFrom-Base64Text {
param([Parameter(Mandatory)][AllowEmptyString()][string]$Base64)
try {
$bytes = [Convert]::FromBase64String($Base64)
[Text.Encoding]::UTF8.GetString($bytes)
} catch {
throw "Invalid Base64 input: $($_.Exception.Message)"
}
}