Skip to content

Instantly share code, notes, and snippets.

@themisir
Last active August 30, 2020 17:36
Show Gist options
  • Save themisir/b341a4853b4dd590bdab238d32300959 to your computer and use it in GitHub Desktop.
Save themisir/b341a4853b4dd590bdab238d32300959 to your computer and use it in GitHub Desktop.

Base64 function for PowerShell

Works like base64 tool on UNIX systems.

base64 [-Decode | -d] <input>

Accepts filename or string input. Specify -Decode (or -d) option to decode input.

Install

  1. Open PowerShell Profile file in your editor.
  2. Copy module.ps1 code below.
  3. Paste to the end of the profile file.

You can get profile file path using by running $PROFILE on PowerShell.

Function Base64 {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string] $Source,
[Parameter(Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelineByPropertyName = $false)]
[Alias("d")]
[switch] $Decode = $false
)
$from = $Source
if ([System.IO.File]::Exists($Source)) {
$from = [System.IO.File]::ReadAllText($Source)
}
if (!$Decode) {
$Encoded = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($from))
Write-Output $Encoded
} else {
$Decoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($from))
Write-Output $Decoded
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment