全部速查表

PowerShell 命令速查

PowerShell cmdlet 快速参考:命令发现、文件/系统、管道过滤与筛选、网络、远程会话与模块,并附带常用参数和实操示例。

34 条命令

发现与帮助

Get-Command [<name>]

按名称、别名或通配符发现命令——堪称 PowerShell 里的 man。

-Module <module>;-Noun / -Verb;-ParameterType;-All

Get-Command -Noun Service | Format-Table Name, Module
Get-Help <cmdlet> [-Examples|-Detailed|-Full]

查看 cmdlet 帮助:简介、语法、参数、示例。

-Examples;-Detailed;-Full;-Online 打开浏览器;-ShowWindow 图形窗口

Get-Help Get-Process -Examples
Get-Module [-ListAvailable]

列出当前会话已加载(或可用)的模块。

-ListAvailable;-All;-Name 过滤

Get-Module -ListAvailable | Sort-Object Name

导航与文件系统

Set-Location / Get-Location / pwd

切换或打印当前工作目录。

Set-Location C:\projects\app; Get-Location
Get-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 10
New-Item -ItemType File/Directory <path>

新建文件或目录(默认:文件)。

-Force 覆盖/创建父目录;-ItemType File|Directory|SymbolicLink;-Value 写入内容

New-Item -ItemType Directory -Path logs/2026 | Out-Null
Remove-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 -Wait
Test-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-Table
ForEach-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 5
Group-Object (alias group)

按某个属性的值分组;-NoElement 只输出每组的计数。

-Property;-NoElement;-CaseSensitive

Get-Content app.log | Group-Object | Sort-Object Count -Descending
Measure-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)}} -AutoSize
Out-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:\$false
Set-Variable (alias set/sv)

定义会话级或环境变量。

-Name;-Value;-Scope Global|Local|Script;-PassThru

Set-Variable -Name regions -Value @('eastus','westus') -Scope Global
Get-Variable / $env:

列出或读取变量;通过 $env: 驱动器读写进程环境。

$env:DATABASE_URL = 'postgres://localhost/dev'; Get-Variable | Out-GridView
Get-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 5
Get-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 443
Invoke-WebRequest / Invoke-RestMethod

HTTP 客户端(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_url
Resolve-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.ps1
Import-Module / Find-Module / Install-Module

把模块加载到当前会话,或从仓库获取模块。

-Force 重新加载;-Scope Global/CurrentUser;-AcceptLicense;-AllowClobber

Install-Module -Name Az -Scope CurrentUser -AcceptLicense
Get-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

速查页版本 1.0.0

适用于 PowerShell 7+