ConvertFrom-Base64String.psm1

function ConvertFrom-Base64String
{
    <#
    .SYNOPSIS
    Converts a base64 encoded string back to its original text representation using different encoding options.
 
    .DESCRIPTION
    This function takes a base64 encoded string and decodes it into its original text representation.
    The user can specify the desired encoding format for the conversion.
 
    .PARAMETER Encoding
    Specifies the encoding format to be used for conversion. Default is "Default".
    Valid options are: Default, ASCII, BigEndianUnicode, Unicode, UTF32, UTF8, and UTF7.
 
    .PARAMETER String
    The base64 encoded string to be converted.
 
    .NOTES
    Author: Eric Meinders
    Version: 1.0
    #>


    [cmdletbinding()]
    param
    (
        [ValidateSet("Default","ASCII","BigEndianUnicode","Unicode","UTF32","UTF8","UTF7")]
        [String]$Encoding = "Default",
        [Parameter(Mandatory, Position = 0, ValueFromPipeline)]
        [String]$String
    )

    # Convert the base64 string to a byte array
    $convertedByte = [System.Convert]::FromBase64String($String)

    # Use the specified encoding to convert the byte array back to text
    switch ($Encoding)
    {
        "Default" { [System.Text.Encoding]::Default.GetString($convertedByte) }
        "ASCII" { [System.Text.Encoding]::ASCII.GetString($convertedByte) }
        "BigEndianUnicode" { [System.Text.Encoding]::BigEndianUnicode.GetString($convertedByte) }
        "Unicode" { [System.Text.Encoding]::Unicode.GetString($convertedByte) }
        "UTF32" { [System.Text.Encoding]::UTF32.GetString($convertedByte) }
        "UTF8" { [System.Text.Encoding]::UTF8.GetString($convertedByte) }
        "UTF7" { [System.Text.Encoding]::UTF7.GetString($convertedByte) }
    }
}