How to Encode a File Contents into Base64 with Powershell

The aim of this page📝 is to explain how to handle file content in PowerShell based on the particular example of encoding a file’s content in Base64. This is to simulate "base64" command from bash with the bit of luxury that I want the encoded string directly copied to the clipboard as I'm setting that into Hashicorp Consul as a configuration value, in my particular case I've been working on setting TLS certificates there. For a comparison with Python, see https://medium.com/p/b5b5c3ff8b4/edit

Pavol Kutaj
2 min readOct 19, 2023
function b64 ($file_path) {
$content = Get-Content -Path $file_path -Raw
$byte_array = [System.Text.Encoding]::UTF8.GetBytes($content)
$base64 = [System.Convert]::ToBase64String($byte_array)
Write-Host "Original Content:" -foregroundcolor darkcyan
Write-Host $content
Write-Host "--------------------------------------------" -foregroundcolor darkcyan
Write-Host "Base64 Encoded Content:" -foregroundcolor darkcyan
Write-Host $base64
Write-Host "--------------------------------------------" -foregroundcolor darkcyan
Write-Host "+ Clipped" -foregroundcolor darkcyan
Set-Clipboard -Value $base64
}
  • This script reads the content of a file as a single string, preserving newlines. It then converts this string to a byte array using UTF-8 encoding.
  • The byte array is then converted to a Base64 string. Both the original and encoded content are printed to the terminal.
  • Finally, the encoded content is copied to the clipboard.
  • In PowerShell, you can read the content of a file using the Get-Content cmdlet.
  • The Get-Content cmdlet reads the content line by line and returns an array of strings, each representing a line of content.
  • If you want to preserve the newlines in the content, you can use the -Raw parameter with Get-Content, which reads the content as a single string.
  • To convert a string to a byte array in PowerShell, you can use the GetBytes method of the System.Text.Encoding class.
  • The System.Text.Encoding class provides methods to convert strings to byte arrays and byte arrays to strings.
  • The UTF8 property of the System.Text.Encoding class gets an encoding for the UTF-8 format.
  • To convert a byte array to a Base64 string in PowerShell, you can use the ToBase64String method of the System.Convert class.
  • The System.Convert class provides methods to convert base data types to other base data types.
  • You can print content to the terminal in PowerShell using the Write-Host cmdlet.
  • The Write-Host cmdlet customizes output. You can specify colors for foreground and background, and also apply formatting.
  • To copy content to the clipboard in PowerShell, you can use the Set-Clipboard cmdlet.
  • The Set-Clipboard cmdlet sets the current clipboard entry. You can specify text or specify a file whose contents will be added to the clipboard.

LINKS

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com