Base64 Encode Decode in Powershell

Published on 06 May 2025 (Updated: 06 May 2025)

Welcome to the Base64 Encode Decode in Powershell page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

function Show-Usage() {
    Write-Host "Usage: please provide a mode and a string to encode/decode"
    Exit 1
}

function Get-Base64Encode([string]$Str) {
    $Bytes = [Text.Encoding]::Ascii.GetBytes($Str)
    [Convert]::ToBase64String($Bytes)
}

function Get-Base64Decode([string]$Str) {
    $Bytes = [Convert]::FromBase64String($Str)
    [Text.Encoding]::Ascii.GetString($Bytes)
}

if ($args.Length -lt 2 -or -not $args[1]) {
    Show-Usage
}

$Mode = $args[0]
$Str = $args[1]
switch ($Mode) {
    "encode" {
        $Result = Get-Base64Encode($Str)
    }
    "decode" {
        try {
            $Result = Get-Base64Decode($Str)
        } catch [Management.Automation.MethodInvocationException] {
            Show-Usage
        }
    }
    default {
        Show-Usage 
    }
}

Write-Host $Result

Base64 Encode Decode in Powershell was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.