Coding

PowerShell

Avoid common PowerShell mistakes — output behavior, array traps, and comparison operator gotchas.

What it does

Output Behavior Everything not captured goes to output — even without or doesn't stop output — previous uncaptured expressions still output bypasses pipeline — use for display only, not data Assign to to suppress — cast also suppresses —

The skill document

Output Behavior

  • Everything not captured goes to output — even without return or Write-Output
  • return doesn't stop output — previous uncaptured expressions still output
  • Write-Host bypasses pipeline — use for display only, not data
  • Assign to $null to suppress — $null = SomeFunction
  • [void] cast also suppresses — [void](SomeFunction)

Array Gotchas

  • Single item result is scalar, not array — @(Get-Item .) forces array
  • Empty result is $null, not empty array — check with if ($result) carefully
  • Array unrolling in pipeline — @(1,2,3) | ForEach sends items one by one
  • += on array creates new array — slow in loops, use [System.Collections.ArrayList]
  • , is array operator — ,$item wraps single item in array

Comparison Operators

  • -eq, -ne, -gt, -lt — not ==, !=, >, <
  • -like with wildcards, -match with regex — both return bool
  • -contains for array membership — $arr -contains $item, not $item -in $arr (though -in works too)
  • Case-insensitive by default — -ceq, -cmatch for case-sensitive
  • $null on left side — $null -eq $var prevents array comparison issues

String Handling

  • Double quotes interpolate — "Hello $name" expands variable
  • Single quotes literal — '$name' stays as literal text
  • Subexpression for complex — "Count: $($arr.Count)" for properties/methods
  • Here-strings for multiline — @" ... "@ or @' ... '@
  • Backtick escapes — `n for newline, `t for tab

Pipeline

  • $_ or $PSItem is current object — same thing, $_ more common
  • ForEach-Object for pipeline — foreach statement doesn't take pipeline
  • -PipelineVariable saves intermediate — Get-Service -PV svc | Where ...
  • Pipeline processes one at a time — unless function doesn't support streaming

Error Handling

  • $ErrorActionPreference sets default — Stop, Continue, SilentlyContinue
  • -ErrorAction Stop per command — makes non-terminating errors terminating
  • try/catch only catches terminating — set ErrorAction Stop first
  • $? is last command success — $LASTEXITCODE for native commands

Common Mistakes

  • No space before { in ifif($x){ works but if ($x) { preferred
  • = is assignment in conditions — use -eq for comparison
  • Function return array unrolls — return ,@($arr) to keep array
  • Get-Content returns lines array — -Raw for single string
  • Select-Object creates new object — properties are copies, not references

Cross-Platform

  • pwsh is PowerShell 7+ — powershell is Windows PowerShell 5.1
  • Paths use / or \Join-Path for portable
  • Environment vars: $env:VAR — works on all platforms
  • Aliases differ across platforms — ls, cat may not exist, use full cmdlet names

Related skills

Produces correct, hardened Bash scripts — quoting, strict mode, traps, and macOS/Linux portability built in.

153 installs3 stars

Use Python for practical project setup, dependency install, script execution, and environment troubleshooting with safe defaults. Use when tasks involve pypr...

68 installs

Runs a coaching session with a contracted opening, short questions, dated commitments, and an exit designed at intake.

58 installs3 stars

Write and debug PHP with strict types, context-aware escaping, and right-sized FPM and OPcache.

85 installs4 stars

Diagnose and fix Visual Studio Code at the editor layer: settings scopes, debug, formatters, extensions, keybindings, remotes.

103 installs3 stars

Execute terminal commands safely and reliably with clear pre-checks, output validation, and recovery steps. Use when users ask to run shell/CLI commands, ins...

115 installs1 stars

More from Iván

Browse all skills

Run Git operations — commits, branches, merges, rebases, conflict resolution, and recovery — with safety rules enforced.

by Iván527 installs31 stars

Create and critique visual artifacts with quantified rules for hierarchy, spacing, type scale, color, and layout.

by Iván137 installs5 stars

Debug CSS mechanics and write component stylesheets grounded in named mechanisms, not trial-and-error.

by Iván97 installs5 stars

Plans and runs self-directed learning as a system: exit test, spaced review, deliberate practice, and transfer proof.

by Iván93 installs3 stars

Get Azure architecture, debugging, security, and cost reviews grounded in a live inventory of your subscription.

by Iván86 installs2 stars

Diagnoses Java and JVM issues from exception messages to container OOM-kills, and writes Java code matching the configured JDK.

by Iván130 installs9 stars