Decode PowerShell Command from Running Processes

There are times when I've found a PowerShell process running that is taking up a bunch of resources. Sometimes they've even been my own scripts running in the context of a SQL Server Agent Job with a PowerShell job step. Because I can have multiple PowerShell job steps running at a time, sometimes it's difficult to know what the offending job is. The following method can be used to decode a script block that a PowerShell process is currently running.

Create a Long Running Process

To demonstrate this capability, create the following Agent Job. It executes a PowerShell job step that outputs a number every minute for 10 minutes.

USE [msdb]
EXEC msdb.dbo.sp_add_job @job_name=N'PowerShell Job', 
    @category_name=N'[Uncategorized (Local)]',

EXEC msdb.dbo.sp_add_jobserver @job_name=N'PowerShell Job', @server_name = @@SERVERNAME

EXEC msdb.dbo.sp_add_jobstep @job_name=N'PowerShell Job', @step_name=N'PowerShell',
        powershell.exe -Command {
            $i = 1
            while ( $i -le 10 )
                Write-Output -InputObject $i
                Start-Sleep -Seconds 60

EXEC msdb.dbo.sp_update_job @job_name=N'PowerShell Job', 
    @category_name=N'[Uncategorized (Local)]', 

Execute the Agent Job that was just created.

USE msdb;
EXEC dbo.sp_start_job N'PowerShell Job';

View the Process

Using Task Manager

  • Launch the Task Manager and select the Details tab.
  • Scroll down the list to powershell.exe.

  • Right-click the column headers in Task Manager and click Select Columns.

  • Check Command Line in the Select columns dialogue and click OK.

  • Now the parameters that were passed into powershell.exe are visible. However, the command is still obfuscated as an encoded command.

Using PowerShell

  • Start PowerShell as Administrator. It is vital that PowerShell is running as administrator, otherwise no results will be returned when querying the running processes.
  • Execute the following command to obtain all of the PowerShell processes that have an encoded command:
$powerShellProcesses = Get-CimInstance -ClassName Win32_Process -Filter 'CommandLine LIKE "%EncodedCommand%"'
  • The following command creates a custom PowerShell object that contains the process ID and the encoded command.
$commandDetails = $powerShellProcesses | Select-Object -Property ProcessId,
    name       = 'EncodedCommand'
    expression = {
        if ( $_.CommandLine -match 'encodedCommand (.*) -inputFormat' )
            return $matches[1]
  • Now the encoded command can be decoded. The following snippet iterates over the command details object, decodes the encoded command, and adds the decoded command back to the object for further investigation.
$commandDetails | ForEach-Object -Process {
    $currentProcess = $_
    $decodedCommand = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($currentProcess.EncodedCommand))
    $commandDetails |
        Where-Object -FilterScript { $_.ProcessId -eq $_.ProcessId } |
        Add-Member -MemberType NoteProperty -Name DecodedCommand -Value $decodedCommand
  • The decoded command can now be reviewed by selecting the decoded command property.

There it is! Just another neat trick to add to the toolbox.

Comments (0)

Skip to main content