1---2name: powershell3description: Avoid common PowerShell mistakes — output behavior, array traps, and comparison operator gotchas.4---56## Output Behavior7- Everything not captured goes to output — even without `return` or `Write-Output`8- `return` doesn't stop output — previous uncaptured expressions still output9- `Write-Host` bypasses pipeline — use for display only, not data10- Assign to `$null` to suppress — `$null = SomeFunction`11- `[void]` cast also suppresses — `[void](SomeFunction)`1213## Array Gotchas14- Single item result is scalar, not array — `@(Get-Item .)` forces array15- Empty result is `$null`, not empty array — check with `if ($result)` carefully16- Array unrolling in pipeline — `@(1,2,3) | ForEach` sends items one by one17- `+=` on array creates new array — slow in loops, use `[System.Collections.ArrayList]`18- `,` is array operator — `,$item` wraps single item in array1920## Comparison Operators21- `-eq`, `-ne`, `-gt`, `-lt` — not `==`, `!=`, `>`, `<`22- `-like` with wildcards, `-match` with regex — both return bool23- `-contains` for array membership — `$arr -contains $item`, not `$item -in $arr` (though `-in` works too)24- Case-insensitive by default — `-ceq`, `-cmatch` for case-sensitive25- `$null` on left side — `$null -eq $var` prevents array comparison issues2627## String Handling28- Double quotes interpolate — `"Hello $name"` expands variable29- Single quotes literal — `'$name'` stays as literal text30- Subexpression for complex — `"Count: $($arr.Count)"` for properties/methods31- Here-strings for multiline — `@" ... "@` or `@' ... '@`32- Backtick escapes — `` `n `` for newline, `` `t `` for tab3334## Pipeline35- `$_` or `$PSItem` is current object — same thing, `$_` more common36- `ForEach-Object` for pipeline — `foreach` statement doesn't take pipeline37- `-PipelineVariable` saves intermediate — `Get-Service -PV svc | Where ...`38- Pipeline processes one at a time — unless function doesn't support streaming3940## Error Handling41- `$ErrorActionPreference` sets default — `Stop`, `Continue`, `SilentlyContinue`42- `-ErrorAction Stop` per command — makes non-terminating errors terminating43- `try/catch` only catches terminating — set `ErrorAction Stop` first44- `$?` is last command success — `$LASTEXITCODE` for native commands4546## Common Mistakes47- No space before `{` in `if` — `if($x){` works but `if ($x) {` preferred48- `=` is assignment in conditions — use `-eq` for comparison49- Function return array unrolls — `return ,@($arr)` to keep array50- `Get-Content` returns lines array — `-Raw` for single string51- `Select-Object` creates new object — properties are copies, not references5253## Cross-Platform54- `pwsh` is PowerShell 7+ — `powershell` is Windows PowerShell 5.155- Paths use `/` or `\` — `Join-Path` for portable56- Environment vars: `$env:VAR` — works on all platforms57- Aliases differ across platforms — `ls`, `cat` may not exist, use full cmdlet names