PowerShell 用于自动化任务和配置管理的常用命令的备忘清单,可帮助系统管理员快速参考常用操作。
常用操作
辅助命令
PowerShell 的命令遵循动词-名词格式 一些常见的动词:
| 动词 |
描述 |
| Get |
用于检索信息 |
| Set |
用于配置或更改设置 |
| New |
用于创建新对象实例 |
| Remove |
用于删除或移除项目 |
| Invoke |
用于执行特定的操作或动作 |
| Start |
用于启动进程或操作 |
| Stop |
用于停止或终止进程或操作 |
| Enable |
用于激活或启用功能 |
| Disable |
用于停用或禁用功能 |
| Test |
用于执行测试或检查 |
| Update |
用于更新或刷新数据或配置 |
列出可用模块
POWERSHELL
1
| Get-Module --ListAvailable
|
列出可用的 cmdlet 和函数
POWERSHELL
1
| Get-Command -Module ActiveDirectory
|
列出别名及其对应的 cmdlet 名称
POWERSHELL
1
| Get-Alias | Select-Object Name, Definition
|
获取帮助
POWERSHELL
1 2 3
| Get-Help <cmd> Get-Help <cmd> -Examples Get-Help -Name Get-Process -Parameter Id
|
Get-Member: 显示对象的属性和方法
POWERSHELL
1
| Get-Process | Get-Member
|
对象操作
Select-Object: 选择对象的特定属性或自定义其显示
POWERSHELL
1
| Get-Process | Select-Object Name, CPU
|
Where-Object: 根据指定条件过滤对象
POWERSHELL
1 2 3
| Get-Service | Where-Object { $PSItem.Status -eq 'Running' }
Get-Service | ? { $_.Status -eq 'Running' }
|
Measure-Object: 计算对象属性的统计信息,如总和、平均值和计数
POWERSHELL
1
| Get-Process | Measure-Object -Property WorkingSet -Sum
|
ForEach-Object: 对集合中的每个对象执行操作(注意:以下命令将为当前目录中的文件/文件夹添加前缀)
POWERSHELL
1
| Get-ChildItem | ForEach-Object { Rename-Item $_ -NewName "Prefix_$_" }
|
Sort-Object: 按指定属性对对象进行排序
POWERSHELL
1
| Get-ChildItem | Sort-Object Length -Descending
|
Format-Table: 将输出格式化为带有指定列的表格
POWERSHELL
1
| Get-Service | Format-Table -AutoSize
|
Format-List: 将输出格式化为属性和值的列表
POWERSHELL
1
| Get-Process | Format-List -Property Name, CPU
|
文件系统
POWERSHELL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| New-Item -path file.txt -type 'file' -value 'contents' New-Item -path file.txt -type 'dir' Copy-Item <src> -destination <dest> Move-Item -path <src> -destination <dest> Remove-Item <file> Test-Path <path> Rename-Item -path <path> -newname <newname>
[System.IO.File]::WriteAllText('test.txt', '') [System.IO.File]::Delete('test.txt')
Get-Content -Path "test.txt" Get-Process | Out-File -FilePath "processes.txt" Get-Process | Export-Csv -Path "processes.csv" $data = Import-Csv -Path "data.csv"
|
系统管理
获取信息
POWERSHELL
1 2 3 4 5 6 7 8 9 10
| Get-CimInstance -ClassName Win32_BIOS
Get-CimInstance -ClassName Win32_DiskDrive
Get-CimInstance -ClassName Win32_PhysicalMemory
Get-CimInstance -ClassName Win32_NetworkAdapter
Get-CimInstance -ClassName Win32_VideoController
|
命名空间 & 类
列出所有类名
POWERSHELL
1
| Get-CimClass | Select-Object -ExpandProperty CimClassName
|
探索 root\cimv2 命名空间中的各种 WMI 类
POWERSHELL
1
| Get-CimClass -Namespace root\cimv2
|
探索 root\cimv2 命名空间下的子 WMI 命名空间
POWERSHELL
1
| Get-CimInstance -Namespace root -ClassName __NAMESPACE
|
网络管理
POWERSHELL
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Test-Connection -ComputerName google.com
Get-NetAdapter
Get-NetIPAddress
Get-NetRoute
Test-NetConnection google.com -Port 80
|
用户和组管理
POWERSHELL
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Get-LocalUser
New-LocalUser -Name NewUser -Password (ConvertTo-SecureString "Password123" -AsPlainText -Force)
Remove-LocalUser -Name UserToRemove
Get-LocalGroup
Add-LocalGroupMember -Group Administrators -Member UserToAdd
|
安全性和权限
获取文件/目录的访问控制列表
POWERSHELL
1
| Get-Acl C:\Path\To\File.txt
|
设置文件/目录的访问控制列表
POWERSHELL
1
| Set-Acl -Path C:\Path\To\File.txt -AclObject $aclObject
|
注册表管理
POWERSHELL
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Select DisplayName, DisplayVersion
Set-ItemProperty -Path "HKLM:\Software\MyApp" -Name "SettingName" -Value "NewValue"
New-ItemProperty -Path "HKCU:\Software\MyApp" -Name "NewSetting" -Value "NewValue" -PropertyType String
Remove-ItemProperty -Path "HKCU:\Software\MyApp" -Name "SettingToRemove"
Test-Path "HKLM:\Software\MyApp"
|
脚本
变量
初始化变量,指定或不指定类型:
POWERSHELL
1 2 3 4 5 6 7 8 9 10 11 12 13
| $var = 0 [int] $var = 'Trevor' [string] $var = 'Trevor' $var.GetType()
$a,$b,$c = 'a','b','c'
$arrayvar = @('va1','va2')
$dict = @{k1 = 'test'; k2 = 'best'}
|
变量命令
POWERSHELL
1 2 3 4 5 6 7 8 9 10
| New-Variable -Name FirstName -Value Trevor New-Variable FirstName -Value Trevor -Option <ReadOnly/Constant>
Get-Variable Get-Variable | ? { $PSItem.Options -contains 'constant' } Get-Variable | ? { $PSItem.Options -contains 'readonly' }
Remove-Variable -Name firstname
Remove-Variable -Name firstname -Force
|
变量类型:int32, int64, string, bool
运算符
POWERSHELL
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
|
= , += / -= , ++ / -- -eq / -ne , -lt / -gt , -le / -ge
$FirstName = 'Trevor' $FirstName -like 'T*' $true; $false
$FoodToEat = $BaconIsYummy ? 'bacon' : 'beets'
'Celery' -in @('Bacon', 'Sausage', 'Steak')
5 -is [int32]
'Trevor' -match '^T\w*'
$regex = [regex]'(\w*)' $regex.Matches('this is test').Value
|
Structure
输入输出操作
POWERSHELL
1 2 3 4 5 6 7 8 9
| "This displays a string"
Write-Host "color" -ForegroundColor Red
$age = Read-host "Enter age"
$pwd = Read-host "password" -asSecureString
Clear-Host
|
流控制
POWERSHELL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| IF(){ }ELSEIF(){}ELSE{}
Switch($var){ "val1"{; break} "val2"{; break} }
For($ct=0;$ct -le 3;$ct++){}
ForEach($var in $arr){}
while($var -ne 0){}
Do{}While()
|
函数 / 模块
示例 1
POWERSHELL
1 2 3 4 5 6 7 8 9 10 11
| function funcname{
[CmdletBinding()] param( [Parameter(Mandatory)] [String]$user ) Write-Host "welcome " $user return "value" } $var = funcname -user pcb
|
示例 2
POWERSHELL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function Get-EvenNumbers { [CmdletBinding()] param ( [Parameter(ValueFromPipeline = $true)] [int] $Number ) begin {} process { if ($Number % 2 -eq 0) { Write-Output $Number } } end {} } 1..10 | Get-EvenNumbers
|
模块
POWERSHELL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| $env:PSModulePath
Get-Module -ListAvailable
Get-Module
Import-Module <moduleName> Remove-Module <moduleName>
Find-Module -Tag cloud Find-Module -Name ps*
New-Module -Name trevor -ScriptBlock { function Add($a,$b) { $a + $b } }
|
注意
- 在大多数语言中,转义字符是反斜杠 \,而在 PowerShell 中是反引号 `
参考