plugins/languages/powershell/skills/core/SKILL.md
PowerShell core conventions covering PowerShell 7.4 LTS (cross-platform pwsh) vs Windows PowerShell 5.1 (built-in legacy), Verb-Noun cmdlet naming with approved verbs, advanced functions ([CmdletBinding()] / param blocks / SupportsShouldProcess), pipeline semantics (ValueFromPipeline, process/begin/end blocks), strict mode (Set-StrictMode -Version 3.0), $ErrorActionPreference, $PSStyle ANSI formatting, ForEach-Object -Parallel, ConvertFrom-Json -AsHashtable. Use proactively when the user asks to "写 PowerShell 脚本 / pwsh 脚本 / Windows 自动化 / cmdlet 开发". Also triggers on "powershell", "pwsh", "ps1", "cmdlet", "advanced function", "Verb-Noun", "$PSStyle", "Set-StrictMode", "powershell 规范".
npx skillsauth add lazygophers/ccplugin powershell-coreInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
PowerShell 是面向对象的 shell + 脚本语言。本文是其它 powershell skill 的基线。
| 版本 | 平台 | 状态 | 用途 | |------|------|------|------| | PowerShell 7.4 LTS (pwsh) | Windows / Linux / macOS | 2026 主流 | 新项目默认 | | PowerShell 7.5 | 跨平台 | Current channel | 尝鲜 | | Windows PowerShell 5.1 | Windows 内置 | 维护,不演进 | 兼容旧脚本 / 无 pwsh 环境 | | PowerShell 6.x | 跨平台 | EOL | 升 7.x |
默认策略:新代码目标 7.4 LTS;维护脚本若必须 5.1,文件头注明并避开 7+ 独有特性(?? / ?. / 三元 / ForEach-Object -Parallel)。
| 主题 | 跳转 |
|------|------|
| 模块 / PSGallery / manifest | powershell-modules |
| 错误处理 / try-catch / $ErrorActionPreference | powershell-error |
| Pester 测试 / PSScriptAnalyzer | powershell-testing |
| cmd.exe / 批处理兼容 | powershell-windows-shell |
#Requires → Set-StrictMode -Version 3.0 → $ErrorActionPreference = 'Stop'。Get-Foo / Set-Foo / Test-Foo,动词必须在 Get-Verb 输出列表内。$camelCase。[CmdletBinding()] + param() 块。[string]$Name、[int]$Count、[ValidateSet(...)] 等属性。process { } 块 + ValueFromPipeline。Write-* 分级:Write-Verbose / Write-Warning / Write-Error / Write-Information;禁 Write-Host 当数据输出。Set-StrictMode -Version 3.0:拒绝未声明变量、未定义属性、数组越界。Invoke-ScriptAnalyzer。Join-Path / [IO.Path]::Combine,禁硬编码 \。#!/usr/bin/env pwsh
#Requires -Version 7.4
<#
.SYNOPSIS
一句话描述
.DESCRIPTION
详细描述
.PARAMETER Name
参数说明
.EXAMPLE
./script.ps1 -Name foo
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Name,
[ValidateRange(1, 100)]
[int]$Count = 10
)
Set-StrictMode -Version 3.0
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $true # 7.3+: 原生命令非零退出码也 throw
function Get-LargeFile {
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param(
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('FullName', 'PSPath')]
[string[]]$Path,
[ValidateRange(1, [long]::MaxValue)]
[long]$MinBytes = 1MB
)
begin {
Write-Verbose "Scanning for files >= $MinBytes bytes"
$count = 0
}
process {
foreach ($p in $Path) {
Get-ChildItem -LiteralPath $p -File -Recurse -ErrorAction SilentlyContinue |
Where-Object Length -ge $MinBytes |
ForEach-Object {
$count++
$_
}
}
}
end {
Write-Verbose "Total: $count files"
}
}
| 块 | 作用 | 调用次数 |
|----|------|---------|
| begin | 初始化 | 1 |
| process | 处理每个管道项 | N |
| end | 收尾 | 1 |
| clean (7.3+) | 类 finally,即使中断也跑 | 1 |
1..5 | ForEach-Object { $_ * 2 } # 内联
1..1000 | ForEach-Object -Parallel { $_ * 2 } -ThrottleLimit 8 # 7.0+ 并行
function Remove-OldLog {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param([string]$Path)
if ($PSCmdlet.ShouldProcess($Path, 'Remove')) {
Remove-Item -LiteralPath $Path -Force
}
}
Remove-OldLog -Path /tmp/old.log -WhatIf # 仅打印不执行
Remove-OldLog -Path /tmp/old.log -Confirm # 询问确认
$PSStyle(7.2+ ANSI 颜色与渲染)$PSStyle.OutputRendering = 'Host' # 仅 host,重定向时去 ANSI
$PSStyle.Formatting.Error = $PSStyle.Foreground.Red + $PSStyle.Bold
Write-Host "$($PSStyle.Foreground.Cyan)done$($PSStyle.Reset)"
$name = 'world'
"Hello, $name" # 插值
'Hello, $name' # 字面
"Hello, $($obj.Property)" # 子表达式
@"
multi-line
$name
"@ # here-string 双引号(插值)
# 拼接尽量用插值或 -f 操作符,不要 +
'Result: {0} / {1}' -f $a, $b
| 操作符 | 含义 |
|--------|------|
| -eq / -ne | 等 / 不等 |
| -lt / -le / -gt / -ge | 数值比较 |
| -like / -notlike | 通配符 * ? |
| -match / -notmatch | 正则;填充 $Matches |
| -contains / -in | 集合成员 |
| -is / -as | 类型测试 / 转换 |
| ?? / ??= (7.0+) | null 合并 |
| ?. / ?[] (7.0+) | null 条件访问 |
默认大小写不敏感;用
-ceq/-cmatch等-c前缀强制敏感。
# JSON ↔ 对象
$obj = Get-Content config.json | ConvertFrom-Json -AsHashtable -Depth 32
$obj | ConvertTo-Json -Depth 32 -Compress | Set-Content out.json
# 高效集合
$list = [System.Collections.Generic.List[string]]::new()
$list.Add('a')
# 数组追加性能差(每次新建),用 List 或 ArrayList
| 反模式 | 修正 |
|--------|------|
| Write-Host 当数据输出 | Write-Output / 直接 $obj |
| function foo { ... } 无 [CmdletBinding()] | 加 advanced function 框架 |
| += 数组拼接大循环 | [List[T]]::new() + .Add() |
| Invoke-Expression 字符串 | 拆参数 + splat |
| 硬编码 \ 路径 | Join-Path |
| 大写动词不在 Get-Verb | 用近义已批准动词 |
| 直接 throw 字符串 | throw [ArgumentException]::new('msg') |
| 没有 Set-StrictMode | 文件头加上 |
#Requires -Version 声明Set-StrictMode -Version 3.0 + $ErrorActionPreference = 'Stop'Get-Verb 列表[CmdletBinding()] + param() + 类型 + 校验.SYNOPSIS / .PARAMETER / .EXAMPLE)齐全\ 或 C:\Invoke-ScriptAnalyzer 零警告$PSStyle 文档 — https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_ansi_terminalstools
UI/UX 与布局设计——做界面布局/结构/导航/组件/交互的设计决策。触发:做UI/UX/布局/排版/导航/组件/交互/栅格/响应式/图表选型/字体配对。按媒介路由 HTML/Web、原生 App(iOS/Android/桌面)、CLI、TUI。需后端动态系统不适用;配色/主题/色板走姊妹 skill design-color。
tools
主题与配色设计——做颜色搭配/调色板/主题/品牌色阶/暗模式的设计决策。触发:选配色/调色/主题/色板/品牌色/暗模式/对比度/色盲/UI风格。按媒介路由 HTML/Web(CSS变量)、原生App(平台token)、CLI(ANSI)、TUI(真彩/256/16降级)。保证可访问性(对比度/色盲安全)。需后端动态系统不适用;UI/UX 布局/组件/交互走姊妹 skill design-uiux。
tools
跨任意组件(plugin/skill/agent/command)的验证驱动优化循环纪律 skill。当用户要优化某个已有组件却无明确方向、或要防止改了反而更差(自评乐观偏差 / 多维同改归因失效 / 为凑分加废话膨胀)、或要把一套通用「评分→单变量改→改后验证严格更好才留否则回滚→触顶停」的纪律套到任意组件上时使用。管优化过程本身的纪律(validation gate / ratchet / 独立验证 / 触顶停),不评单组件深度(交 skill-dev),不查插件接线(交 plugin-dev)。仅手动 /optimize-any 触发。
data-ai
两层规则记忆 (基于 .skein/spec)。planning 时 recall 召回相关规则、task finish 后 sediment 沉淀学习 + prune 自动精简过期/重复/断链规则。core 常驻硬规 + recall 按需召回, 经判定门自动写盘 (不逐次问用户)。产出 .skein/spec 下 core/recall 规则文件 + index。另支持空仓 bootstrap 播种规则基线、记忆大面积失效 (大重构/换栈) 时 reconstruct 可逆归档后按项目类型分型重建、maintain 手动体检 (超预算/stale/断链/重复/废弃, --apply 自动修复)、auto-fix (Stop hook 写 .pending-fix 标记 → main 派 skein-specer bg 跑 maintain --apply 全自动修, 断链只报告)。