Source Code and Samples

Friday 2 February 2007

Base64 Encode a File

Description

This script will Base64 encode the contents of the file specified and output it to another file called {filename}.b64.

Source Code

$fileName = "File1.txt"
$fileContent = get-content $fileName
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$fileContentEncoded set-content ($fileName + ".b64")

2 comments:

Unknown said...

$fileContentEncoded | set-content ($fileName + ".b64")

it's require pipe to work

DomFilk said...

you can try this free online service to encode image to base64 string online.