powershell
v1.0.0Avoid common PowerShell mistakes — output behavior, array traps, and comparison operator gotchas.
Installation
Please help me install the skill `powershell` from SkillHub official store.
npx skills add ivangdavila/powershell
Output Behavior
- Everything not captured goes to output — even without
returnorWrite-Output returndoesn't stop output — previous uncaptured expressions still outputWrite-Hostbypasses pipeline — use for display only, not data- Assign to
$nullto suppress —$null = SomeFunction [void]cast also suppresses —void
Array Gotchas
- Single item result is scalar, not array —
@(Get-Item .)forces array - Empty result is
$null, not empty array — check withif ($result)carefully - Array unrolling in pipeline —
@(1,2,3) | ForEachsends items one by one +=on array creates new array — slow in loops, use[System.Collections.ArrayList],is array operator —,$itemwraps single item in array
Comparison Operators
-eq,-ne,-gt,-lt— not==,!=,>,<-likewith wildcards,-matchwith regex — both return bool-containsfor array membership —$arr -contains $item, not$item -in $arr(though-inworks too)- Case-insensitive by default —
-ceq,-cmatchfor case-sensitive $nullon left side —$null -eq $varprevents 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 —
`nfor newline,`tfor tab
Pipeline
$_or$PSItemis current object — same thing,$_more commonForEach-Objectfor pipeline —foreachstatement doesn't take pipeline-PipelineVariablesaves intermediate —Get-Service -PV svc | Where ...- Pipeline processes one at a time — unless function doesn't support streaming
Error Handling
$ErrorActionPreferencesets default —Stop,Continue,SilentlyContinue-ErrorAction Stopper command — makes non-terminating errors terminatingtry/catchonly catches terminating — setErrorAction Stopfirst$?is last command success —$LASTEXITCODEfor native commands
Common Mistakes
- No space before
{inif—if($x){works butif ($x) {preferred =is assignment in conditions — use-eqfor comparison- Function return array unrolls —
return ,@($arr)to keep array Get-Contentreturns lines array —-Rawfor single stringSelect-Objectcreates new object — properties are copies, not references
Cross-Platform
pwshis PowerShell 7+ —powershellis Windows PowerShell 5.1- Paths use
/or—Join-Pathfor portable - Environment vars:
$env:VAR— works on all platforms - Aliases differ across platforms —
ls,catmay not exist, use full cmdlet names