๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
ๅฐ้พ™่™พๅฐ้พ™่™พAI
๐Ÿค–

PowerShell

้ฟๅ…ๅธธ่ง็š„ PowerShell ้”™่ฏฏโ€”โ€”่พ“ๅ‡บ่กŒไธบใ€ๆ•ฐ็ป„้™ท้˜ฑๅ’Œๆฏ”่พƒ่ฟ็ฎ—็ฌฆ้™ท้˜ฑใ€‚

ไธ‹่ฝฝ2.5k
ๆ˜Ÿๆ ‡3
็‰ˆๆœฌ1.0.0
็ณป็ปŸๅทฅๅ…ท
ๅฎ‰ๅ…จ้€š่ฟ‡

ๆŠ€่ƒฝ่ฏดๆ˜Ž


name: PowerShell description: Avoid common PowerShell mistakes โ€” output behavior, array traps, and comparison operator gotchas. metadata: {"clawdbot":{"emoji":"๐Ÿ”ต","requires":{"bins":["pwsh"]},"os":["linux","darwin","win32"]}}

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 if โ€” if($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

ๅฆ‚ไฝ•ไฝฟ็”จใ€ŒPowerShellใ€๏ผŸ

  1. ๆ‰“ๅผ€ๅฐ้พ™่™พAI๏ผˆWeb ๆˆ– iOS App๏ผ‰
  2. ็‚นๅ‡ปไธŠๆ–นใ€Œ็ซ‹ๅณไฝฟ็”จใ€ๆŒ‰้’ฎ๏ผŒๆˆ–ๅœจๅฏน่ฏๆก†ไธญ่พ“ๅ…ฅไปปๅŠกๆ่ฟฐ
  3. ๅฐ้พ™่™พAI ไผš่‡ชๅŠจๅŒน้…ๅนถ่ฐƒ็”จใ€ŒPowerShellใ€ๆŠ€่ƒฝๅฎŒๆˆไปปๅŠก
  4. ็ป“ๆžœๅณๆ—ถๅ‘ˆ็Žฐ๏ผŒๆ”ฏๆŒ็ปง็ปญๅฏน่ฏไผ˜ๅŒ–

็›ธๅ…ณๆŠ€่ƒฝ