全部速查表

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 GUI 視窗

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 (別名 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 (別名 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 (別名 ?)

依屬性或腳本區塊篩選管線中的物件。

-Property / -Value 模式比對;-Like / -EQ / -GT …;-CContains / -In

Get-Process | Where-Object { $_.WorkingSet64 -gt 200MB }
Select-Object (別名 select)

選取特定屬性、去除重複、或取前 N 筆。

-Property 屬性名;-First / -Last N;-Skip N;-Unique;-ExpandProperty

Get-Service | Select-Object Name, Status, StartType | Format-Table
ForEach-Object (別名 %)

對管線中傳入的每個物件執行動作。

-Begin / -Process / -End;-Parallel(PS7+)

Get-ChildItem *.csv | ForEach-Object { Import-Csv $_ | Group-Object Level }
Sort-Object (別名 sort)

依一或多個屬性排序管線物件。

-Property;-Descending

Get-ChildItem | Sort-Object -Property Length -Descending | Select -First 5
Group-Object (別名 group)

依屬性值分組;`-NoElement` 只回傳計數。

-Property;-NoElement;-CaseSensitive

Get-Content app.log | Group-Object | Sort-Object Count -Descending
Measure-Object (別名 measure)

計算某屬性的數值統計(個數、總和、最小/最大/平均)。

-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 (別名 sls)

對等於 grep——找出符合模式的行(支援正則)。

-Path;-Pattern;-SimpleMatch;-CaseSensitive;-Context N

Select-String -Path src\**\*.ts -Pattern 'TODO' -CaseSensitive:\$false
Set-Variable (別名 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 路徑;-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 (別名 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+