powershell
PowerShell 7+PowerShell cmdlet 快速参考:命令发现、文件/系统、管道过滤与筛选、网络、远程会话与模块,并附带常用参数和实操示例。
34 条命令
发现与帮助
Get-Command [<name>]按名称、别名或通配符发现命令——堪称 PowerShell 里的 man。
-Module <module>;-Noun / -Verb;-ParameterType;-All
Get-Command -Noun Service | Format-Table Name, ModuleGet-Help <cmdlet> [-Examples|-Detailed|-Full]查看 cmdlet 帮助:简介、语法、参数、示例。
-Examples;-Detailed;-Full;-Online 打开浏览器;-ShowWindow 图形窗口
Get-Help Get-Process -ExamplesGet-Module [-ListAvailable]列出当前会话已加载(或可用)的模块。
-ListAvailable;-All;-Name 过滤
Get-Module -ListAvailable | Sort-Object Name导航与文件系统
Set-Location / Get-Location / pwd切换或打印当前工作目录。
Set-Location C:\projects\app; Get-LocationGet-ChildItem (alias ls/dir)列出文件与子目录(-Recurse 递归)。
-Recurse;-Force 显示隐藏;-Filter <pattern>;-File / -Directory;-Depth
Get-ChildItem -Recurse -File -Filter *.log | Sort-Object Length -Descending | Select-Object -First 10New-Item -ItemType File/Directory <path>新建文件或目录(默认:文件)。
-Force 覆盖/创建父目录;-ItemType File|Directory|SymbolicLink;-Value 写入内容
New-Item -ItemType Directory -Path logs/2026 | Out-NullRemove-Item (alias rm/del)删除文件、目录、注册表键或变量。
-Recurse;-Force;-Confirm:\$false 跳过确认提示
Remove-Item -Recurse -Force build/, node_modules/Copy-Item / Move-Item / Rename-Item复制、移动或重命名文件系统项。
-Recurse;-Force;-To;-Exclude / -Include 过滤
Copy-Item -Recurse dist\* \\deploy\shares\app\Get-Content / Set-Content / Add-Content读取或写入文件内容(Get-Content + -Tail 类似 Unix 的 tail)。
-Tail N;-Wait 跟踪;-TotalCount N;-Encoding utf8/ASCII
Get-Content -Path app.log -Tail 50 -WaitTest-Path / Resolve-Path检查路径是否存在(文件、目录、注册表),或解析通配符。
-PathType Container|Leaf;-IsValid
if (Test-Path .\node_modules) { Remove-Item -Recurse .\node_modules }管道与对象
Where-Object (alias ?)按属性或脚本块过滤管道中的对象。
-Property / -Value 模式匹配;-Like / -EQ / -GT ...;-CContains / -In
Get-Process | Where-Object { $_.WorkingSet64 -gt 200MB }Select-Object (alias select)挑选指定属性、去重,或取前 N 条。
-Property 属性名;-First / -Last N;-Skip N;-Unique;-ExpandProperty
Get-Service | Select-Object Name, Status, StartType | Format-TableForEach-Object (alias %)对管道中流入的每个对象执行一次操作。
-Begin / -Process / -End;PS7+ 支持 -Parallel
Get-ChildItem *.csv | ForEach-Object { Import-Csv $_ | Group-Object Level }Sort-Object (alias sort)按一个或多个属性对管道对象排序。
-Property;-Descending
Get-ChildItem | Sort-Object -Property Length -Descending | Select -First 5Group-Object (alias group)按某个属性的值分组;-NoElement 只输出每组的计数。
-Property;-NoElement;-CaseSensitive
Get-Content app.log | Group-Object | Sort-Object Count -DescendingMeasure-Object (alias measure)对某个属性计算数值统计(count、sum、min/max/avg)。
-Sum / -Average / -Minimum / -Maximum;-Line / -Word / -Character 用于文本
Get-ChildItem -File -Recurse | Measure-Object -Property Length -Sum格式化
Format-Table / Format-List (ft / fl)将管道对象渲染为表格或列表——仅用于展示。
-AutoSize;-Wrap;-GroupBy;-Property 子集
Get-Process | Format-Table Name, Id, @{n='Mem(MB)';e={[Math]::Round($_.WorkingSet64/1MB,1)}} -AutoSizeOut-File / Set-Content / Tee-Object将输出写入文件;Tee-Object 同时写入文件和管道。
Get-Service | Tee-Object -FilePath services.txt | Where-Object { $_.Status -eq 'Running' }字符串与变量
Select-String (alias sls)相当于 grep——查找匹配模式的行(支持正则)。
-Path;-Pattern;-SimpleMatch;-CaseSensitive;-Context N
Select-String -Path src\**\*.ts -Pattern 'TODO' -CaseSensitive:\$falseSet-Variable (alias set/sv)定义会话级或环境变量。
-Name;-Value;-Scope Global|Local|Script;-PassThru
Set-Variable -Name regions -Value @('eastus','westus') -Scope GlobalGet-Variable / $env:列出或读取变量;通过 $env: 驱动器读写进程环境。
$env:DATABASE_URL = 'postgres://localhost/dev'; Get-Variable | Out-GridViewGet-Alias / Set-Alias列出或自定义 cmdlet 快捷方式。
Set-Alias -Name grep -Value Select-String -Option ReadOnly -Scope Global进程与服务
Get-Process / Start-Process / Stop-Process查看、启动或停止本地进程(Stop-Process 即 kill)。
-Name;-Id;-FileVersionInfo;-PassThru;-Force 用于强制 kill
Get-Process node | Sort-Object -Property WorkingSet64 -Descending | Select -First 5Get-Service / Start-Service / Stop-Service查看、启动或停止 Windows 服务。
-Name;-DisplayName;-Status Running|Stopped
Get-Service | Where-Object Status -eq Stopped | Format-Table Name, StartupType网络
Test-NetConnection <host>诊断 TCP 连通性、延迟,并可配合 -Port 检查端口可达性。
-Port;-InformationLevel Detailed;-DiagnoseRouting
Test-NetConnection api.example.com -Port 443Invoke-WebRequest / Invoke-RestMethodHTTP 客户端(RestMethod 自动把 JSON 解析为对象)。
-Method GET/POST/PUT/DELETE;-Headers @{...};-Body (json);-OutFile path;-SkipHttpRedirect;-UseBasicParsing
Invoke-RestMethod -Uri https://api.github.com/repos/powershell/powershell/releases/latest | Select-Object tag_name, html_urlResolve-DnsName / Get-NetIPAddress解析 DNS 记录,查看本机 IP 配置。
-Type A/AAAA/MX/CNAME;-Server 8.8.8.8;-AddressFamily IPv4/IPv6
Resolve-DnsName example.com -Type MX远程与模块
Enter-PSSession / Invoke-Command在远程机器上启动会话或执行命令(WinRM / SSH)。
-ComputerName / -HostName;-Credential;-ConfigurationName;-FilePath
Invoke-Command -ComputerName web01,web02 -FilePath .\deploy.ps1Import-Module / Find-Module / Install-Module把模块加载到当前会话,或从仓库获取模块。
-Force 重新加载;-Scope Global/CurrentUser;-AcceptLicense;-AllowClobber
Install-Module -Name Az -Scope CurrentUser -AcceptLicenseGet-Module / Remove-Module列出已加载的模块,或从当前会话卸载一个模块。
Get-Module | Format-Table Name, Version, ExportedCommands.Count系统
Get-Date / Set-Date读取或设置当前日期/时间,支持丰富的格式化选项。
-Format;-UFormat;-DisplayHint;-Culture
Get-Date -Format 'yyyy-MM-dd HH:mm:ss'Get-CimInstance Win32_OperatingSystem通过 CIM/WMI 获取硬件/系统信息(内存、磁盘、OS、BIOS 等)。
-ClassName;-ComputerName;-Filter 查询
Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'" | Select DeviceID, @{n='FreeGB';e={[Math]::Round($_.FreeSpace/1GB,2)}}Get-History / Invoke-History (alias h/r)在当前会话中重放最近执行过的命令。
-Count N;-Id N 重新执行指定条目
Get-History | Where-Object CommandLine -like 'Get-*' | Invoke-History$PROFILE / Update-Help编辑你的 profile(启动脚本)并刷新 cmdlet 帮助。
notepad $PROFILE; Update-Help -Force相关命令速查
商标与使用声明
本批命令行工具速查手册为开源社区参考资料。文中涉及的各工具的名称、标识、商标(如 Git™、Docker™、Kubernetes®、kubectl、PostgreSQL®、MySQL®、Redis™、MongoDB®、Linux™、PowerShell™、Vim™ 等)均归对应权利人所有,此处仅为识别与参考之目的使用。
本速查手册的全部内容均为独立编写的简化自包含参考,**与任何官方文档不存在隶属、被认可或引用关系**。命令列表、参数说明、示例均为作者原创简化表达;如需权威信息,请参阅各工具的官方文档。
命令语法与参数可能在不同工具版本中有所差异。所示版本标签对应稳定发布版本;其他版本行为可能有所不同。本速查手册**不代表**商标权利人的任何偏好或推荐。
如商标或版权方认为本站部分内容存在权益侵害,请发送邮件至[email protected],我们在收到有效权属证明后,会在合理期限内修改或移除相关内容。
本网站不对因使用本站信息而产生的任何直接或间接损失承担法律责任。
关于 PowerShell
PowerShell 是微软于 2006 年为 Windows 管理发布的跨平台命令行 Shell 与脚本语言。从 2016 年的开源重写(PowerShell Core)与 2018 年的统一,当前稳定版是 PowerShell 7+,可在 Windows、macOS 与大多数 Linux 发行版上运行。传统 Shell 传文本,PowerShell 传 .NET 对象——`Get-Process | Where-Object { $_.CPU -gt 100 }`——这让对象结构沿管道保留下来,每个 cmdlet 提供可发现的 `-MemberName` Tab 补全。默认的 动-宾 命名(`Get-Item`、`Set-Variable`、`New-Item`)和帮助系统(`Get-Help`、`-Examples`、`-Online`)让语言自文档化。PowerShell 自带数千 cmdlet(模块如 `Microsoft.PowerShell.Management`、`Microsoft.PowerShell.Utility`、`PSReadLine`),并支持基于 WinRM / SSH 的远程、最小特权委托(JEA)、声明式配置(DSC)。Windows 自动化、Azure / M365 管理、以及任何"面向对象管道胜过 awk 体操"的跨平台任务都该用它。执行策略和 AMSI 扫描可拦截恶意脚本,记得 `Get-ExecutionPolicy` 查一下,永远不要粘贴看不懂的编码命令。
速查页版本 1.0.0