PowerShell 是微软开发的任务自动化和配置管理框架,内置命令行 Shell 和脚本语言。它基于 .NET 构建,是 Windows 系统管理和自动化的首选工具,现已跨平台支持 Linux 和 macOS。
一切皆对象
与 Shell 的纯文本管道不同,PowerShell 管道传递的是 .NET 对象:
1 2 3 4 5 6 7 8 9
| Get-Process | Where-Object { $_.CPU -gt 100 } | Select-Object Name, CPU, WorkingSet64 | Sort-Object CPU -Descending
Get-Service | Where-Object Status -eq "Running" | Select-Object Name, DisplayName, StartType
|
Cmdlet 命名规范
PowerShell 使用”动词-名词”的统一命名模式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Get-Process Set-Location New-Item Remove-Item Invoke-WebRequest
Get-Verb | Sort-Object Verb
ls cd cat rm
|
变量与类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| $name = "RustBoy" $version = [version]"1.0.0" $numbers = @(1, 2, 3, 4, 5) $hash = @{ Name = "RustBoy" Language = "Rust" Year = 2026 }
[int]$port = 8080 [string]$url = "https://rustboy.de" [bool]$debug = $false
$list = [System.Collections.Generic.List[string]]::new() $list.Add("hello") $list.Add("world")
|
流程控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| $score = 85
if ($score -ge 90) { Write-Host "优秀" -ForegroundColor Green } elseif ($score -ge 60) { Write-Host "及格" -ForegroundColor Yellow } else { Write-Host "不及格" -ForegroundColor Red }
$os = [System.Environment]::OSVersion.Platform switch -Regex ($os) { "Win" { "Windows 系统" } "Unix" { "Linux/macOS" } default { "未知系统" } }
foreach ($file in Get-ChildItem *.txt) { Write-Host "$($file.Name) - $($file.Length) bytes" }
1..10 | Where-Object { $_ % 2 -eq 0 } | ForEach-Object { $_ * $_ } | Measure-Object -Sum
|
函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| function Get-SystemReport { [CmdletBinding()] param( [Parameter(Mandatory)] [string]$ComputerName,
[ValidateSet("Basic", "Detailed")] [string]$Level = "Basic" )
process { $os = Get-CimInstance Win32_OperatingSystem -ComputerName $ComputerName $cpu = Get-CimInstance Win32_Processor -ComputerName $ComputerName
[PSCustomObject]@{ Computer = $ComputerName OS = $os.Caption CPU = $cpu.Name MemoryGB = [math]::Round($os.TotalVisibleMemorySize / 1MB, 1) Uptime = (Get-Date) - $os.LastBootUpTime } } }
$report = Get-SystemReport -ComputerName "localhost" -Level "Detailed" $report | Format-List
|
实用脚本示例
批量文件处理:
1 2 3 4 5 6 7 8 9 10
| $logPath = "C:\Logs" $cutoff = (Get-Date).AddDays(-30)
Get-ChildItem -Path $logPath -Filter "*.log" -Recurse | Where-Object { $_.LastWriteTime -lt $cutoff } | ForEach-Object { Write-Host "删除: $($_.FullName)" -ForegroundColor Yellow Remove-Item $_.FullName -Force }
|
远程管理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $servers = @("server01", "server02", "server03") $cred = Get-Credential
$results = Invoke-Command -ComputerName $servers -Credential $cred -ScriptBlock { [PSCustomObject]@{ ComputerName = $env:COMPUTERNAME DiskFreeGB = [math]::Round((Get-PSDrive C).Free / 1GB, 1) LastUpdate = (Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 1).InstalledOn } }
$results | Format-Table -AutoSize
|
PowerShell 7+ 与跨平台
PowerShell 7 基于 .NET Core,可在 Linux 和 macOS 上运行:
1 2 3 4 5 6 7 8 9 10
| $PSVersionTable $IsWindows $IsLinux $IsMacOS
Get-ChildItem -Path /home -Recurse -File | Measure-Object -Property Length -Sum | Select-Object @{N="TotalMB"; E={[math]::Round($_.Sum/1MB, 2)}}
|
与 Rust 的配合
PowerShell 是 Windows 平台上编排和管理 Rust 工具的天然选择。用 PowerShell 脚本调用 Rust 编译的 CLI 工具,结合两者的优势:
1 2 3
| $result = & .\my-rust-tool.exe --format json | ConvertFrom-Json $result.items | Where-Object { $_.status -eq "active" }
|
PowerShell 的学习曲线比 Bash 陡峭,但其面向对象的管道、丰富的 .NET 类库和强大的远程管理能力,使其成为 Windows 生态中不可替代的工具。