Convert to Base64 encoded string using PowerShell one-liner

PowerShell doesn’t provide built-in cmdlets for conversion from string to Base64 encoded string. This task can be done by combination of .NET methods Encoding.GetBytes and Convert.ToBase64String. PowerShell one-liner is then simple:

[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("MyTestInput"))

We can even omit System namespace to simplify one-liner:

[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("MyTestInput"))

Output is Base64 encoded string:

TXlUZXN0SW5wdXQ=

More about using .NET in PowerShell can be found in documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *