Powershell: Executing Base64 commands returns error?

Solution 1:

If it must be encoded, use the following to get your Base64 string:

[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes('Get-ChildItem'))

Trying this worked for me:

powershell.exe -encodedCommand RwBlAHQALQBDAGgAaQBsAGQASQB0AGUAbQA=

Solution 2:

To add to @SomethingElse's answer, the difference between your Base64 string that doesn't work, and their Base64 string that does work, is the character encoding used when converting the original string to byte values.

It needs to be encoded as UTF-16-LE, then converted to Base64 for PowerShell to like it, and yours was encoded as UTF8/plain ASCII.

# Your version, with 1-byte-per-character encoding:
PS> [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Get-ChildItem'))
R2V0LUNoaWxkSXRlbQ==

# Working version, with 2-bytes-per-character encoding:
PS> [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes('Get-ChildItem'))
RwBlAHQALQBDAGgAaQBsAGQASQB0AGUAbQA=