← 返回
效率工具 中文

PowerShell

Avoid common PowerShell mistakes — output behavior, array traps, and comparison operator gotchas.
避免常见的 PowerShell 错误 —— 输出行为、数组陷阱及比较运算符易错点。
ivangdavila
效率工具 clawhub v1.0.0 1 版本 99644 Key: 无需
★ 2
Stars
📥 2,199
下载
💾 213
安装
1
版本
#latest

概述

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

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 ifif($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

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-28 20:31 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

ai-intelligence

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,351 📥 317,807
productivity

Baidu web search

ide-rea
使用百度AI搜索引擎(BDSE)进行网络搜索。适用于获取实时信息、文档资料或研究课题。
★ 237 📥 105,357
productivity

Weather

steipete
获取当前天气和预报(无需API密钥)
★ 445 📥 226,138