PowerShell Genie

Inspect PowerShell command lines

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

A WQL filter restricts Win32_Process to the two PowerShell image names so the selection happens inside the CIM provider rather than in the pipeline.

The script

1PS> Get-CimInstance Win32_Process -Filter "Name='powershell.exe' OR Name='pwsh.exe'" | Select-Object ProcessId,ParentProcessId,CommandLine

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 ProcessId ParentProcessId CommandLine
    2 --------- --------------- -----------
    3 11268 1480 pwsh.exe -NoLogo
    4 13844 11268 powershell.exe -NoProfile -File C:\Scripts\Audit.ps1
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-CimInstance Win32_Process -Filter "Name='powershell.exe' OR Name='pwsh.exe'" | Select-Object ProcessId,ParentProcessId,CommandLine
    1. Stage 1Get-CimInstance-Filter
    2. Stage 2Select-ObjectPositional values or no named parameters

      How it works

      A WQL filter restricts Win32_Process to the two PowerShell image names so the selection happens inside the CIM provider rather than in the pipeline. The projection keeps the PID with ParentProcessId, which is what identifies whatever launched it, plus the full CommandLine.

      Adapting it

      Decode any value passed to -EncodedCommand by converting it from base64 and reading the bytes back as Unicode, since that is where obfuscated payloads are normally carried.

      Requirements

      Elevation to read command lines belonging to other users' processes.

      If the result is unexpected

      Process command-line fields are blank or incomplete.

      Protected processes and permissions can restrict visibility, and processes can exit during collection.

      1. Inspect one accessible process and retain process IDs for entries with unavailable command lines.

      Validate the result

      Unavailable fields are labeled rather than interpreted as commands with no arguments.

      Keep the check reliable

      Limit exposure of command lines because arguments can contain credentials or other secrets.

      Primary documentation

      Get-CimInstance reference

      Continue
      Genie guideInspect PowerShell command lines

      A WQL filter restricts Win32_Process to the two PowerShell image names so the selection happens inside the CIM provider rather than in the pipeline.