ConvertTo-Base64String.psm1

function ConvertTo-Base64String
{
    <#
    .SYNOPSIS
    Converts a string, credential, or file contents to a base64 encoded string.
 
    .DESCRIPTION
    This function takes a string, credential (username and password), or file contents and converts it into
    a base64 encoded string. 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 string to be converted to a base64 encoded string.
 
    .PARAMETER Credential
    The credential object containing username and password. The function will convert them to a base64 encoded string.
 
    .PARAMETER Path
    The path to a file. The function will read the file contents and convert them to a base64 encoded string.
 
    .NOTES
    Author: Eric Meinders
    Version: 1.0
 
    #>


    [cmdletbinding(DefaultParameterSetName = "String")]
    param
    (
        [ValidateSet("Default","ASCII","BigEndianUnicode","Unicode","UTF32","UTF8","UTF7")]
        [Parameter(ParameterSetName = "String")]
        [Parameter(ParameterSetName = "Credential")]
        [String]$Encoding = "Default",
        [Parameter(Mandatory, Position = 0, ValueFromPipeline, ParameterSetName = "String")]
        [String]$String,
        [Parameter(Mandatory, ParameterSetName = "Credential")]
        [System.Management.Automation.PSCredential]
        $Credential,
        [Parameter(Mandatory, ParameterSetName = "File")]
        [ValidateScript({
            if (!(Test-Path $_))
            {
                throw "File does not exist"
            }
            if (!(Test-Path $_ -PathType Leaf))
            {
                throw "The specified path must be a file"
            }
            return $true
        })]
        [System.IO.FileInfo]$Path
    )

    function ConvertFrom-Byte ($in)
    {
        [System.Convert]::ToBase64String($in)
    }

    # Convert file contents to base64
    if ($Path)
    {
        $byteArray = Get-Content -Path $Path -Encoding Byte
        ConvertFrom-Byte $byteArray
        return
    }

    # Convert credential to base64
    if ($Credential)
    {
        $String = $Credential.UserName + ":" + $Credential.GetNetworkCredential().Password
    }

    # Convert string to base64 using specified encoding
    $byteArray = switch ($Encoding)
    {
        "Default" { [System.Text.Encoding]::Default.GetBytes($String) }
        "ASCII" { [System.Text.Encoding]::ASCII.GetBytes($String) }
        "BigEndianUnicode" { [System.Text.Encoding]::BigEndianUnicode.GetBytes($String) }
        "Unicode" { [System.Text.Encoding]::Unicode.GetBytes($String) }
        "UTF32" { [System.Text.Encoding]::UTF32.GetBytes($String) }
        "UTF8" { [System.Text.Encoding]::UTF8.GetBytes($String) }
        "UTF7" { [System.Text.Encoding]::UTF7.GetBytes($String) }
    }
    
    # Call the helper function to perform the actual conversion
    ConvertFrom-Byte $byteArray
}