PowerShell 速查表
可搜尋、可列印的 PowerShell 參考手冊——cmdlet、管線、變數、檔案、迴圈、函式、物件以及系統管理。免費。
取得說明與探索
12Get-Help Get-Process
顯示 cmdlet 的說明
Get-Help Get-Process -Examples
顯示 cmdlet 的使用範例
Get-Help Get-Process -Full
顯示含參數的完整說明
Get-Help about_Functions
閱讀概念性的 about_ 主題
Update-Help
下載最新的說明檔
Get-Command -Noun Process
列出作用於某名詞的 cmdlet
Get-Command -Verb Get
列出使用某動詞的 cmdlet
Get-Command *service*
以萬用字元名稱搜尋指令
Get-Process | Get-Member
列出物件的屬性與方法
Get-Alias ls
顯示別名對應的指令
Get-Alias -Definition Get-ChildItem
列出 cmdlet 的所有別名
$PSVersionTable
顯示 PowerShell 版本與版別
變數與型別
13$x = 5
將值指派給變數
[int]$n = '42'
宣告強型別變數
$null
null / 空值
$true; $false
布林字面值
$arr = @(1, 2, 3)
建立陣列
$arr += 4
在陣列附加一個元素
$arr[0]; $arr[-1]
索引陣列的首末元素
$hash = @{ Name = 'Ann'; Age = 30 }
建立雜湊表
$hash['Name']; $hash.Name
依鍵讀取雜湊表值
$env:PATH
讀取環境變數
$env:MY_VAR = 'value'
為工作階段設定環境變數
Get-Variable
列出所有已定義變數
[math]::Round(3.14159, 2)
呼叫靜態 .NET 方法
管線與篩選
12Get-Process | Where-Object { $_.CPU -gt 10 }
依條件篩選物件
Get-Process | ? CPU -gt 10
以 ? 別名與簡化語法篩選
Get-Process | ForEach-Object { $_.Name }
為每個物件執行指令碼區塊
Get-Process | % { $_.Kill() }
以 % 別名走訪
Get-Process | Select-Object Name, Id
僅保留特定屬性
Get-Process | Select-Object -First 5
取前 N 個物件
Get-Process | Sort-Object CPU -Descending
依屬性排序物件
Get-Process | Group-Object Company
依屬性分組物件
Get-Process | Measure-Object CPU -Sum
彙總數值 (總和、平均)
Get-ChildItem | Select-Object -Unique
回傳不重複的物件
1..5 | ForEach-Object { $_ * 2 }
將範圍透過轉換管線處理
Get-Process | Out-GridView
將結果送至互動式格線
檔案與路徑
13Get-ChildItem -Path C:\Logs
列出目錄項目 (別名 ls/dir)
Get-ChildItem -Recurse -Filter *.log
遞迴列出符合的檔案
Get-Content .\file.txt
逐行讀取檔案
Get-Content .\file.txt -Tail 20
讀取檔案最後 N 行
Set-Content .\file.txt -Value 'Hi'
將文字寫入 (覆寫) 檔案
Add-Content .\file.txt -Value 'Hi'
將文字附加到檔案
Copy-Item a.txt b.txt
複製檔案或目錄
Move-Item a.txt .\dir\
移動或重新命名項目
Remove-Item a.txt -Force
刪除檔案或目錄
New-Item -ItemType Directory -Path .\dir
建立新檔案或目錄
Test-Path .\file.txt
檢查路徑是否存在
Get-Location
顯示目前目錄 (別名 pwd)
Set-Location C:\Temp
變更目前目錄 (別名 cd)
字串與文字
12'Hello World' -match 'World'
以正規表示式測試字串
'Hello' -replace 'l', 'L'
以正規表示式取代文字
'a,b,c' -split ','
以正規表示式將字串分割為陣列
'a-b-c'.Split('-')
以 .NET String 方法分割
'a', 'b' -join ', '
將陣列元素串接成字串
Select-String -Path *.log -Pattern 'error'
在多檔中以 grep 風格搜尋
'{0} of {1}' -f 3, 10
以佔位符格式化字串
' hi '.Trim()
去除前後空白
'Hello'.ToUpper()
將字串轉為大寫
'Hello'.Substring(0, 3)
依索引與長度擷取子字串
'Hello'.Contains('ell')
測試字串是否含某文字
'Hello'.Length
取得字元數
迴圈與條件
12if ($x -gt 0) { 'pos' } elseif ($x -eq 0) { 'zero' } else { 'neg' }
以 if / elseif / else 分支
switch ($x) { 1 { 'one' } default { 'other' } }
將值與各 case 比對
foreach ($i in $arr) { $i }
走訪集合
for ($i = 0; $i -lt 5; $i++) { $i }
帶索引的計數迴圈
while ($x -lt 10) { $x++ }
條件為真時迴圈
do { $x++ } while ($x -lt 10)
至少執行一次再測試
$x -eq 5; $x -ne 5
等於與不等於比較
$x -gt 5; $x -lt 5
大於與小於比較
$x -ge 5; $x -le 5
大於等於/小於等於比較
'abc' -like 'a*'
萬用字元比對比較
@(1, 2, 3) -contains 2
測試陣列成員資格
break; continue
離開或略過目前迴圈反覆
函式與指令碼
12function Get-Sum { param($a, $b) $a + $b }
定義帶參數的函式
Get-Sum -a 2 -b 3
以具名引數呼叫函式
function Test { param([int]$n = 1) }
帶預設值的型別參數
param([Parameter(Mandatory)]$Name)
要求必須提供某參數
return $value
從函式回傳值
function F { [CmdletBinding()] param() }
建立進階函式 (通用參數)
Write-Verbose 'msg' -Verbose
輸出 verbose 訊息串流
Write-Output $result
將物件送至管線
Write-Error 'failed'
寫入錯誤串流
.\script.ps1 -Arg value
以引數執行指令碼檔
. .\functions.ps1
將指令碼點來源載入工作階段
try { } catch { $_ } finally { }
處理終止性錯誤
物件與屬性
12Get-Process | Select-Object -ExpandProperty Name
回傳原始屬性值而非物件
(Get-Process).Name
存取每個物件的屬性
Get-Process | Select-Object Name, @{ Name = 'MB'; Expression = { $_.WS / 1MB } }
新增計算屬性
[PSCustomObject]@{ Name = 'Ann'; Age = 30 }
建立自訂物件
$obj | Add-Member -NotePropertyName Tag -NotePropertyValue 'x'
為物件附加屬性
$obj.PSObject.Properties.Name
列出物件的屬性名稱
$obj | Format-Table -AutoSize
以對齊表格顯示物件
$obj | Format-List *
以清單顯示所有屬性
$obj | ConvertTo-Json -Depth 5
將物件序列化為 JSON
Get-Content data.json | ConvertFrom-Json
將 JSON 解析為物件
$obj | Export-Csv out.csv -NoTypeInformation
將物件匯出為 CSV 檔
Import-Csv data.csv
將 CSV 檔讀取為物件
處理程序與服務
12Get-Process
列出執行中的處理程序 (別名 ps)
Get-Process -Name chrome
依名稱取得處理程序
Stop-Process -Name notepad -Force
終止處理程序
Start-Process notepad.exe
啟動新處理程序
Get-Service
列出 Windows 服務
Get-Service -Name wuauserv
依名稱取得服務
Start-Service -Name wuauserv
啟動已停止的服務
Stop-Service -Name wuauserv
停止執行中的服務
Restart-Service -Name wuauserv
重新啟動服務
Set-Service -Name wuauserv -StartupType Manual
變更服務的啟動模式
Get-EventLog -LogName System -Newest 20
讀取近期的傳統事件記錄項目
Get-WinEvent -LogName Application -MaxEvents 20
查詢現代事件記錄
遠端與工作
12Enter-PSSession -ComputerName Srv01
開始互動式遠端工作階段
Exit-PSSession
離開互動式遠端工作階段
Invoke-Command -ComputerName Srv01 -ScriptBlock { Get-Service }
在遠端主機上執行指令
$s = New-PSSession -ComputerName Srv01
建立持續性遠端工作階段
Invoke-Command -Session $s -ScriptBlock { hostname }
重用現有遠端工作階段
Remove-PSSession $s
關閉並移除遠端工作階段
Start-Job -ScriptBlock { Get-Process }
以背景工作執行指令
Get-Job
列出背景工作及其狀態
Receive-Job -Id 1 -Keep
取得工作的輸出
Wait-Job -Id 1
阻塞直到工作完成
Stop-Job -Id 1; Remove-Job -Id 1
停止並移除背景工作
Get-Process | Start-ThreadJob { $input }
執行輕量的處理程序內執行緒工作
模組與套件
12Get-Module
列出目前載入的模組
Get-Module -ListAvailable
列出所有已安裝模組
Import-Module Pester
將模組載入工作階段
Remove-Module Pester
從工作階段卸載模組
Find-Module -Name PSReadLine
在 gallery 搜尋模組
Install-Module PSReadLine -Scope CurrentUser
從 gallery 安裝模組
Update-Module PSReadLine
更新已安裝的模組
Uninstall-Module PSReadLine
移除已安裝的模組
Get-ExecutionPolicy
顯示目前的指令碼執行原則
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
允許執行已簽署與本機指令碼
Get-PSRepository
列出已設定的套件儲存庫
$PROFILE
目前使用者設定檔指令碼的路徑
沒有條目符合「:q」。
需要協助?
使用此工具時遇到問題?請告訴我們的團隊。