1---2name: avoid-common-powershell-mistakes3description: "Avoid common PowerShell mistakes — output behavior, array traps, and comparison operator gotchas."4category: "Community"5author: community6version: "1.0.0"7icon: puzzle8metadata:9 source: https://skillmd.com/skills/rondoflow/avoid-common-powershell-mistakes10---1112## Output Behavior13- Everything not captured goes to output — even without `return` or `Write-Output`14- `return` doesn't stop output — previous uncaptured expressions still output15- `Write-Host` bypasses pipeline — use for display only, not data16- Assign to `$null` to suppress — `$null = SomeFunction`17- `[void]` cast also suppresses — `[void](SomeFunction)`1819## Array Gotchas20- Single item result is scalar, not array — `@(Get-Item .)` forces array21- Empty result is `$null`, not empty array — check with `if ($result)` carefully22- Array unrolling in pipeline — `@(1,2,3) | ForEach` sends items one by one23- `+=` on array creates new array — slow in loops, use `[System.Collections.ArrayList]`24- `,` is array operator — `,$item` wraps single item in array2526## Comparison Operators27- `-eq`, `-ne`, `-gt`, `-lt` — not `==`, `!=`, `>`, `<`28- `-like` with wildcards, `-match` with regex — both return bool29- `-contains` for array membership — `$arr -contains $item`, not `$item -in $arr` (though `-in` works too)30- Case-insensitive by default — `-ceq`, `-cmatch` for case-sensitive31- `$null` on left side — `$null -eq $var` prevents array comparison issues3233## String Handling34- Double quotes interpolate — `"Hello $name"` expands variable35- Single quotes literal — `'$name'` stays as literal text36- Subexpression for complex — `"Count: $($arr.Count)"` for properties/methods37- Here-strings for multiline — `@" ... "@` or `@' ... '@`38- Backtick escapes — `` `n `` for newline, `` `t `` for tab3940## Pipeline41- `$_` or `$PSItem` is current object — same thing, `$_` more common42- `ForEach-Object` for pipeline — `foreach` statement doesn't take pipeline43- `-PipelineVariable` saves intermediate — `Get-Service -PV svc | Where ...`44- Pipeline processes one at a time — unless function doesn't support streaming4546## Error Handling47- `$ErrorActionPreference` sets default — `Stop`, `Continue`, `SilentlyContinue`48- `-ErrorAction Stop` per command — makes non-terminating errors terminating49- `try/catch` only catches terminating — set `ErrorAction Stop` first50- `$?` is last command success — `$LASTEXITCODE` for native commands5152## Common Mistakes53- No space before `{` in `if` — `if($x){` works but `if ($x) {` preferred54- `=` is assignment in conditions — use `-eq` for comparison55- Function return array unrolls — `return ,@($arr)` to keep array56- `Get-Content` returns lines array — `-Raw` for single string57- `Select-Object` creates new object — properties are copies, not references5859## Cross-Platform60- `pwsh` is PowerShell 7+ — `powershell` is Windows PowerShell 5.161- Paths use `/` or `\` — `Join-Path` for portable62- Environment vars: `$env:VAR` — works on all platforms63- Aliases differ across platforms — `ls`, `cat` may not exist, use full cmdlet names