Search code examples
windowspowershellbase64ascii

How to run base64 code in powershell with ascii?


I would like to run command mkdir C:\Temp\555 in Powershell via a Base64-encoded string.

This code should work

$4 = "bWtkaXIgQzpcVGVtcFw1NTU="

$code = [Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($4))

powershell $code

but if i convert $code to ASCII, how can I run it like that, because this one won't work:

$4 = "bWtkaXIgQzpcVGVtcFw1NTU="

$code = [char]091+[char]084+[char]101+[char]120+[char]116+[char]046+[char]069+[char]110+[char]099+[char]111+[char]100+[char]105+[char]110+[char]103+[char]093+[char]058+[char]058+[char]085+[char]116+[char]102+[char]056+[char]046+[char]071+[char]101+[char]116+[char]083+[char]116+[char]114+[char]105+[char]110+[char]103+[char]040+[char]091+[char]067+[char]111+[char]110+[char]118+[char]101+[char]114+[char]116+[char]093+[char]058+[char]058+[char]070+[char]114+[char]111+[char]109+[char]066+[char]097+[char]115+[char]101+[char]054+[char]052+[char]083+[char]116+[char]114+[char]105+[char]110+[char]103+[char]040+[char]036+[char]052+[char]041+[char]041

powershell $code

because the last code doesn't work while the first one does, any help please?


Solution

  • A string can be executed as a script through the CommandInvocationIntrinsics class method InvokeScript.

    powershell $executioncontext.InvokeCommand.InvokeScript($code)
    

    See InvokeScript Method for more information.