PowerShell review: executable strings, secrets and error semantics
Review PowerShell Invoke-Expression, downloaded code, plain-text secrets and error handling with native-language examples and exact finding explanations.
PowerShell pipelines carry objects, not simply lines of text, and its error model differs from a Unix shell. A useful review focuses on where strings become executable expressions, how a failure becomes terminating and where credentials enter the script. Bash quoting and set -e advice should not be applied to a PowerShell file just because both languages use dollar-prefixed variables.
Avoid reparsing untrusted strings as code
Invoke-Expression evaluates a string in the current scope. If a user, network response or writable file contributes to that string, data can cross into code execution. Prefer invoking a known command with explicit arguments or splatting a controlled argument collection. The call operator can invoke a selected command, but a user-controlled command path still needs validation. Replacing Invoke-Expression does not make arbitrary executable selection trustworthy; identify both the command source and each argument boundary.
Separate remote retrieval from execution
Invoke-WebRequest or a DownloadString call piped into Invoke-Expression executes remote content without an independent review step. HTTPS protects transport but does not establish that a publisher, endpoint or returned content is trustworthy. Download a known artifact, verify the expected hash or signature and inspect what it requires before execution in an appropriate environment. Static review can identify the flow, but cannot fetch the remote contents or guarantee that a future response matches the reviewed version.
Make error handling match the failure type
Many cmdlets emit non-terminating errors by default, so try/catch alone may not catch the case you expect. Use -ErrorAction Stop for a relevant cmdlet when the failure must reach catch. SilentlyContinue can conceal diagnostic evidence rather than solve the underlying problem. Native executable exit codes require their own review; inspect $LASTEXITCODE and the PowerShell version’s native-command behavior. Set-StrictMode helps expose undefined variables and certain invalid operations, but is not a comprehensive failure-handling policy.
Treat credential representation and policy separately
ConvertTo-SecureString -AsPlainText does not erase the original secret from script text, command history or earlier variables. Prefer a credential prompt or an appropriate secret store instead of a literal password in source. Execution policy is not a security boundary against hostile code; Bypass changes execution checks but does not grant trust. If a workflow needs a scoped policy adjustment, document the actual requirement without describing the policy as a substitute for signing, integrity verification or access control.
Review example
This intentionally unsafe sample accepts executable text and suppresses errors. The checker reports the boundaries; it never evaluates the expression or applies a replacement.
$ErrorActionPreference = "SilentlyContinue"
$command = Read-Host "Command"
Invoke-Expression $command
Open the checkerWhat cannot be verified
The checker reads PowerShell as source text without loading modules, running commands or inspecting a credential vault. It cannot verify module availability, execution-policy scope, remote content or a caller’s privileges. Its instant rules are targeted checks rather than the full PSScriptAnalyzer engine. Confirm the intended PowerShell version and use the native-language rule descriptions before changing behavior.
Finding references
Invoke-Expression on dynamic input
Invoke-Expression runs a string as code. Any untrusted part of it becomes executable — the PowerShell equivalent of eval.
Suggested safeguard: Call the command directly with splatted arguments (& $cmd @args) instead of building a string.
Downloading and executing in one step
Remote content is downloaded and executed immediately, without verification.
Suggested safeguard: Save to a file, verify its hash or signature (Get-FileHash, Get-AuthenticodeSignature), then run it.
ExecutionPolicy Bypass
The script turns off the execution policy, removing a safety net against unsigned scripts.
Suggested safeguard: Sign the script or scope the policy narrowly (-Scope Process) and document why.
Plain-text secret in ConvertTo-SecureString
A secret is converted from plain text, which usually means it is stored in the script or passed on the command line.
Suggested safeguard: Read secrets from a vault, Get-Credential or an environment variable set by your CI secret store.
Errors silenced with SilentlyContinue
Errors are silenced, so failures pass unnoticed.
Suggested safeguard: Use -ErrorAction Stop with try/catch and handle the specific failures you expect.
Set-StrictMode not enabled
Without Set-StrictMode, typos in variable names silently evaluate to $null.
Suggested safeguard: Add Set-StrictMode -Version Latest and $ErrorActionPreference = 'Stop'.