SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, tutorials, tools and downloads for SQL Database Programmers, Developers, Windows and Office Users

Decode Base64-Encoded String on Windows using PowerShell

In this tutorial, I will show how Windows users and developers can encode a given text and decode base64 encoded string using PowerShell commands.
Of course, on web there are some online tools that developers can use to decode encoded text. On the other hand, in case of security and privacy is an issue then it will definitely better to use a built-in tool shipped with your computer's operating system.
Here in this how-to guide I will show sample scripts to encode a sample text and then decode base64 encoded text within Windows PowerShell utility.

Encode Text using Base64 using PowerShell Commands

Assume that you want to encode following string value: "Hi, I hope you like the tips shared in kodyaz.com"

Developers can launch Windows PowerShell and execute below sample script to encode the given desired string.

[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("Hi, I hope you like the tips shared in kodyaz.com"))

Note the output of the encoding command, we will use it as input of our following decode command

encode text using Base64 using powerShell commands


Decode Base64 Encoded Text on PowerShell

Let's now assume that you are given the base64 encoded string and you want to decode it using PowerShell.
Here is the command you will run on PowerShell tool

[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("SABpACwAIABJACAAaABvAHAAZQAgAHkAbwB1ACAAbABpAGsAZQAgAHQAaABlACAAdABpAHAAcwAgAHMAaABhAHIAZQBkACAAaQBuACAAawBvAGQAeQBhAHoALgBjAG8AbQA="))

And here is the output of the above decoding script that we encoded previously. Just as expected, we are reaching the original string value after decode from base64 encoded text.

decode Base64 encoded text using PowerShell commands

Of course, it is possible to parameterize the script by breaking it into functional pieces as follows

# Encode
$StringToEncode = "Welcome to kodyaz.com"
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($StringToEncode )
$Base64EncodedString =[Convert]::ToBase64String($Bytes)
$Base64EncodedString

Base64 encoding on Windows PowerShell

And for the reserve operation which is decoding from a base64 encoded string, below revised scipt block can be used on Windows PowerShell tool.

# Decode
$Base64EncodedString = "VwBlAGwAYwBvAG0AZQAgAHQAbwAgAGsAbwBkAHkAYQB6AC4AYwBvAG0A"
$Str = [System.Convert]::FromBase64String($Base64EncodedString)
$Base64EncodedString =[System.Text.Encoding]::UTF8.GetString($Str)
$Base64EncodedString

decoding Base64-encoded text on Windows PowerShell

I hope this guide to be useful for especially decoding base64-encoded string values for developers.




Copyright © 2004 - 2023 Eralper YILMAZ. All rights reserved.