Powershell: Executing Base64 commands returns error

powershell

I need help with a simple test case of executing a CLI command through PowerShell which has already been encoded in Base64.

Suppose Get-ChildItem has been converted in advance to Base64 string R2V0LUNoaWxkSXRlbQ==.

Further suppose I have a DOS CLI instance open, and I want to test executing this string in powershell:

C:>\ powershell.exe -enc R2V0LUNoaWxkSXRlbQ==

However, I receive the following error:

The term '???????' is not recognized as the name of a cmdlet, function, script file, or operable program.  Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 
At line:1 char:8
*??????? <<<<
    + CategoryInfo        :ObjectNotFound: (???????:String) [], CommandNotFoundException
    + FullyQualifiedErrorID: CommandNotFoundException

I understand there's a way that works which introduces the use of variables, and even includes the encoding process itself. But what I want to know is, is this method salvageable? What might I be doing wrong? I'm running PS version 2.0 in Win7.

Best Answer

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=