All guides

How to use an encoded command in PowerShell?

How to encode and decode a PowerShell command with Base64, for example to store it in a scheduled task, and run it with -EncodedCommand.

Sometimes you want to store a PowerShell command not readable at first read. This is so called an obfuscation. It is an action to make something unreadable for humans. For example, this could be used when storing a PowerShell command in a task schedular job or something similar.

How to encode your command? #

First you must store your command in a variable, for example $PlainCommand. After that you encode that variable with the following command:

[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($string))
Screenshot: After that you encode that variable with the following command

How to run the encoded command? #

To run the encoded command simple, execute powershell.exe with the parameter -EncodedCommand followed by your variable.

Screenshot: To run the encoded command simple, execute powershell.exe with the parameter -EncodedCommand…

How to decode the encoded command? #

To decode the encoded variable, you can use:

[Text.Encoding]::Utf8.GetString([Convert]::FromBase64String($EncodedCommand))
Screenshot: To decode the encoded variable, you can use

Another method to encode/ decode! #

A nice way to also decode and encode your command is via the Base64 Decode and Encode website:

Base64 Decode and Encode – Online

Screenshot: Base64 Decode and Encode – Online

And of course, the same for encoding your command!

Screenshot: And of course, the same for encoding your command!

How to decode a PowerShell command from a running process? #

Microsoft has a nice write up about how to decode running process, this because PowerShell stores running commands in also as an encoded command:

https://learn.microsoft.com/en-us/powershell/scripting/samples/decode-powershell-command-from-a-running-process?view=powershell-7.4

Thank for reading this short blog, hopefully it was informational and helpful when you need to encode your commands!

Portrait of Jeroen Ebus

Jeroen Ebus

Modern workspace & cloud consultant

Modern workspace and cloud consultant at PeopleRock . Working in IT since 2007, with a focus on end user computing: Microsoft Intune, Windows, Azure and security.

Comments

Comments are powered by GitHub Discussions (Giscus). Nothing is loaded from GitHub until you click the button.