# Encode long command in unique command

**URL:** <https://forums.powershell.org/t/encode-long-command-in-unique-command/6828>\
**Category:** PowerShell Help\
**Created:** [July 15, 2016, 3:21pm UTC](https://forums.powershell.org/t/encode-long-command-in-unique-command/6828 "2016-07-15T15:21:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![doddy-hackman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/doddy-hackman/32/622_2.png) [@doddy-hackman](https://forums.powershell.org/u/doddy-hackman)\
**Post date:** [July 15, 2016, 3:21pm UTC](https://forums.powershell.org/t/encode-long-command-in-unique-command/6828/1 "2016-07-15T15:21:39Z")

</div>

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](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](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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 15, 2016, 3:26pm UTC](https://forums.powershell.org/t/encode-long-command-in-unique-command/6828/2 "2016-07-15T15:26:15Z")

</div>

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. 🙂

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==
```

---

<div class="post-metadata">

**Author:** ![doddy-hackman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/doddy-hackman/32/622_2.png) [@doddy-hackman](https://forums.powershell.org/u/doddy-hackman)\
**Post date:** [July 15, 2016, 4:06pm UTC](https://forums.powershell.org/t/encode-long-command-in-unique-command/6828/3 "2016-07-15T16:06:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [July 16, 2016, 11:55am UTC](https://forums.powershell.org/t/encode-long-command-in-unique-command/6828/4 "2016-07-16T11:55:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![doddy-hackman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/doddy-hackman/32/622_2.png) [@doddy-hackman](https://forums.powershell.org/u/doddy-hackman)\
**Post date:** [July 16, 2016, 5:24pm UTC](https://forums.powershell.org/t/encode-long-command-in-unique-command/6828/5 "2016-07-16T17:24:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peter-jurgens-2](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@peter-jurgens-2](https://forums.powershell.org/u/peter-jurgens-2)\
**Post date:** [July 17, 2016, 5:34pm UTC](https://forums.powershell.org/t/encode-long-command-in-unique-command/6828/6 "2016-07-17T17:34:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![doddy-hackman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/doddy-hackman/32/622_2.png) [@doddy-hackman](https://forums.powershell.org/u/doddy-hackman)\
**Post date:** [July 18, 2016, 10:22am UTC](https://forums.powershell.org/t/encode-long-command-in-unique-command/6828/7 "2016-07-18T10:22:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/encode-long-command-in-unique-command/6828/8 "2024-05-16T20:40:50Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


