Base64 PowerShell Cmdlet Via Advanced Functions

Among the many valuable command line utilities on a Linux system is base64, which encodes to and decodes from the Base64 encoding scheme. As much as I like PowerShell…

(Yes, you read that correctly)

…it sorely lacks a base64-equivalent utility, or cmdlet as they are called in PowerShell land. The only solution was to create one myself. Cmdlets are usually written in C#, but you can also employ the concept of advanced functions, which is what I have done.

Here’s the code for converting strings to Base64. The function supports receiving data from a pipeline, or you can call it directly and pass the value as a parameter. More on the usage later.

Function ConvertFrom-Base64
{
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true)]
        [string] $Base64
    )
    
    Process 
    {
        if ($null -ne $Base64) 
        {
            $Bytes = [Convert]::FromBase64String($Base64)
            Write-Output [System.Text.Encoding]::UTF8.GetString($Bytes)
        }
        else 
        {
            Write-Error "No base64-encoded data provided."
        }
    }
}

The critical standouts are CmdletBinding and the Process {} block. The first turns the function into an advanced function, giving it cmdlet superpowers, and the latter is called for each value passed in via a pipeline or the parameter (separated by commas).

If you want to know more, I urge you to read the documentation I linked earlier and/or watch this video featuring Jeffrey Snover, the inventor of PowerShell. The content is not geared toward seasoned programmers, so the hosts moved too slowly for my tastes. It is worth watching, though, because the information is good.

How data coming from regular invocations or pipelines is handled is a science of its own, and this blog post explores this topic to explain it.

The script must be installed as a module in one of the folders that PowerShell reads. A module consists of a folder with the same name as the script. The script’s file extension must be .psm1. You can get the locations the following way.

PS C:\Users\lober> $env:PSModulePath
C:\Users\lober\OneDrive\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\7\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules

The command and procedure are the same on Linux. Yes, I also tested on Linux. For fun, here is how I installed my script into a Linux PowerShell environment.

rlo@fedora:~$ mkdir /home/rlo/.local/share/powershell/Modules/Convert-Base64 
rlo@fedora:~$ cp Convert-Base64.psm1 /home/rlo/.local/share/powershell/Modules/Convert-Base64
rlo@fedora:~$ cd /home/rlo/.local/share/powershell/Modules/Convert-Base64 
rlo@fedora:~/.local/share/powershell/Modules/Convert-Base64$ ls 
Convert-Base64.psm1

PowerShell imports the module automatically when you start it. If you have PowerShell already running, import the module with Import-Module Convert-Base64.

Because I have documented my script, you can run Get-Help for both functions and add the “-Example” switch to get a list of examples.

Get-Help ConvertTo-Base64 -Example
NAME
    ConvertTo-Base64
SYNOPSIS
    Converts data to base64-encoded format.
    -------------------------- EXAMPLE 1 --------------------------
    PS>ConvertTo-Base64 "Why can't dinosaurs clap their hands? Because they're extinct."
    V2h5IGNhbid0IGRpbm9zYXVycyBjbGFwIHRoZWlyIGhhbmRzPyBCZWNhdXNlIHRoZXkncmUgZXh0aW5jdC4=
    -------------------------- EXAMPLE 2 --------------------------
    PS>ConvertTo-Base64 -Data "I'm afraid for the calendar. Its days are numbered."
    SSdtIGFmcmFpZCBmb3IgdGhlIGNhbGVuZGFyLiBJdHMgZGF5cyBhcmUgbnVtYmVyZWQu
    -------------------------- EXAMPLE 3 --------------------------
    PS>"I was going to try an all almond diet, but that's just nuts." | ConvertTo-Base64
    SSB3YXMgZ29pbmcgdG8gdHJ5IGFuIGFsbCBhbG1vbmQgZGlldCwgYnV0IHRoYXQncyBqdXN0IG51dHMu

You can find my Convert-Base64 module on GitHub.

I hope this was helpful.

Thank you for reading.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.