PowerShell :: Convert text file to Base64
So this script will read the text from a plain text file and then encode it as Base64. It is useful for Base64 encoding PowerShell scripts if you are looking to pass the script as an encoded command line argument. You will need to set the $inputFilePath and $outputFilePath and then execute. You could enter the text into the variable but as you start to get into bother with special characters that you just won’t have to worry about if you read from a file.
$inputFilePath = "C:\PowerShellScript.ps1" $outputFilePath = "C:\Output.txt" $powerShellScript = [IO.File]::ReadAllText($inputFilePath) $bytes = [System.Text.Encoding]::Unicode.GetBytes($powerShellScript) [Convert]::ToBase64String($bytes) > $outputFilePath