1---
2name: powershell
3description: Avoid common PowerShell mistakes — output behavior, array traps, and comparison operator gotchas.
4metadata:
5 source: https://skillmd.com/skills/modbender/powershell
6 clawdbot:
7 emoji: 🔵
8 requires:
9 bins:
10 - pwsh
11 os:
12 - linux
13 - darwin
14 - win32
15---
16
17## Output Behavior
18- Everything not captured goes to output — even without `return` or `Write-Output`
19- `return` doesn't stop output — previous uncaptured expressions still output
20- `Write-Host` bypasses pipeline — use for display only, not data
21- Assign to `$null` to suppress — `$null = SomeFunction`
22- `[void]` cast also suppresses — `[void](SomeFunction)`
23
24## Array Gotchas
25- Single item result is scalar, not array — `@(Get-Item .)` forces array
26- Empty result is `$null`, not empty array — check with `if ($result)` carefully
27- Array unrolling in pipeline — `@(1,2,3) | ForEach` sends items one by one
28- `+=` on array creates new array — slow in loops, use `[System.Collections.ArrayList]`
29- `,` is array operator — `,$item` wraps single item in array
30
31## Comparison Operators
32- `-eq`, `-ne`, `-gt`, `-lt` — not `==`, `!=`, `>`, `<`
33- `-like` with wildcards, `-match` with regex — both return bool
34- `-contains` for array membership — `$arr -contains $item`, not `$item -in $arr` (though `-in` works too)
35- Case-insensitive by default — `-ceq`, `-cmatch` for case-sensitive
36- `$null` on left side — `$null -eq $var` prevents array comparison issues
37
38## String Handling
39- Double quotes interpolate — `"Hello $name"` expands variable
40- Single quotes literal — `'$name'` stays as literal text
41- Subexpression for complex — `"Count: $($arr.Count)"` for properties/methods
42- Here-strings for multiline — `@" ... "@` or `@' ... '@`
43- Backtick escapes — `` `n `` for newline, `` `t `` for tab
44
45## Pipeline
46- `$_` or `$PSItem` is current object — same thing, `$_` more common
47- `ForEach-Object` for pipeline — `foreach` statement doesn't take pipeline
48- `-PipelineVariable` saves intermediate — `Get-Service -PV svc | Where ...`
49- Pipeline processes one at a time — unless function doesn't support streaming
50
51## Error Handling
52- `$ErrorActionPreference` sets default — `Stop`, `Continue`, `SilentlyContinue`
53- `-ErrorAction Stop` per command — makes non-terminating errors terminating
54- `try/catch` only catches terminating — set `ErrorAction Stop` first
55- `$?` is last command success — `$LASTEXITCODE` for native commands
56
57## Common Mistakes
58- No space before `{` in `if` — `if($x){` works but `if ($x) {` preferred
59- `=` is assignment in conditions — use `-eq` for comparison
60- Function return array unrolls — `return ,@($arr)` to keep array
61- `Get-Content` returns lines array — `-Raw` for single string
62- `Select-Object` creates new object — properties are copies, not references
63
64## Cross-Platform
65- `pwsh` is PowerShell 7+ — `powershell` is Windows PowerShell 5.1
66- Paths use `/` or `\` — `Join-Path` for portable
67- Environment vars: `$env:VAR` — works on all platforms
68- Aliases differ across platforms — `ls`, `cat` may not exist, use full cmdlet names