Quick base64 encoding

In the spirit of other quick snippets, I wanted to show how to work with base64 encoding.

First of all, if you have a one-off string, you can always head over to www.base64decode.org. The encoder is a short click away.

If you have a bunch that you need to encode, maybe from a file or something like a one-per-line block of text or something, well, that's what scripts are for. Enter PowerShell.

PS > $encoded = "aGVsbG8sIHdvcmxkIQ=="
PS > [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encoded))
hello, world!

Note that base64 is encoding bytes, so you have to pick an encoding to turn those bytes into text (I've been writing about this for years). I've picked UTF8 here, which is what you'll see most commonly, especially when working with network protocols.

Happy decoding!

Tags:  dotnetpowershell

Home