Creating Encoded PowerShell Commands on the Fly

by Oct 12, 2015

When you run PowerShell code outside a PowerShell console, you need to submit the code to powershell.exe. To make sure your code does not conflict with special characters, commands can be sent encoded to powershell.exe.

One of the easiest ways of converting a plain text command line to an encoded command goes like this:

PS C:\> cmd /c echo powershell { Get-Service | Where-Object Status -eq Running }
BjAHQAIABTAHQAYQB0AHUAcwAgAC0AZQBxACAAUgB1AG4AbgBpAG4AZwAgAA== -inputFormat xml -outputFormat

PS C:\>  

Here you'd find out that you can run the Get-Service | Where-Object statement as an encoded command like this:

powershell.exe -encodedCommand

When you run this statement in a cmd.exe (or PowerShell console) you get all running services. Just make sure you remove the -inputFormat and –outputFormat parameters, and remove any line breaks that may have been inserted. The encoded command is one long string.

Twitter This Tip! ReTweet this Tip!