Here is a simple way to encode text as a Base64 string:

#requires -Version 1

$text = 'Hello World!'
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($text), 'InsertLineBreaks')

The resulting text looks similar to this:


Text encoding is used whenever you want to simply obfuscate text, or when you want to protect text against accidental formatting changes. PowerShell.exe, for example, can run commands that are Base64-encoded. Here is an example (make sure you enable sound output on your machine):


To decode a Base64-encoded string, you can use the code below.

#requires -Version 1


You can also use this code to decode encoded commands and see what they do. Simply assign the encoded command to $test instead.

Twitter This Tip! ReTweet this Tip!