返回顶部
P

PowerShell

Avoid common PowerShell mistakes — output behavior, array traps, and comparison operator gotchas.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
1,667
下载量
2
收藏
概述
安装方式
版本历史

PowerShell

## 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

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 powershell-1776328943 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 powershell-1776328943 技能

通过命令行安装

skillhub install powershell-1776328943

下载 Zip 包

⬇ 下载 PowerShell v1.0.0

文件大小: 2.11 KB | 发布时间: 2026-4-17 15:13

v1.0.0 最新 2026-4-17 15:13
Initial release

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部