在 Windows 上安裝 PowerShell
有多種方法可以在 Windows 中安裝 PowerShell。 每個安裝方法的設計都支援不同的案例和工作流程。 選擇最符合您需求的方法。
- Winget - 在 Windows 用戶端上安裝 PowerShell 的建議方式
- MSI 套件 - Windows Server 和企業部署案例的最佳選擇
- ZIP 套件 - 最簡單的「側載」或安裝多個版本的方式
- 針對 Windows Nano Server、Windows IoT 和 Arm 型系統使用此方法
- .NET 全域工具 - 適合安裝及使用其他全域工具的 .NET 開發人員
- Microsoft Store 套件 - 輕鬆安裝 PowerShell 的非正式使用者,但有限制
注意
本文中的安裝命令適用于最新穩定版本的 PowerShell。 若要安裝不同版本的 PowerShell,請調整 命令以符合您需要的版本。 下列連結會引導您前往 GitHub 上 PowerShell 存放庫中每個版本的發行頁面。
- 穩定版本: https://aka.ms/powershell-release?tag=stable
- LTS 版本: https://aka.ms/powershell-release?tag=lts
- 預覽版本: https://aka.ms/powershell-release?tag=preview
每個套件的下載連結都可以在 [發行] 頁面的 [ 資產 ] 區段中找到。 [資產] 區段可能會摺疊,因此您可能需要按一下加以展開。
使用 Winget 安裝 PowerShell (建議)
Winget 是 Windows 封裝管理員一種命令列工具,可讓使用者探索、安裝、升級、移除及設定 Windows 用戶端電腦上的應用程式。 此工具是 Windows 封裝管理員服務的用戶端介面。 winget
命令列工具預設會與Windows 11和新式版本的 Windows 10 搭配使用,作為應用程式安裝程式。
注意
如需系統需求和安裝指示的清單,請參閱 winget 檔 。
Winget
目前不會在 Windows 伺服器上執行。
您可以透過下列命令使用已發佈的 winget
套件來安裝 PowerShell:
搜尋最新版的 PowerShell
winget search Microsoft.PowerShell
Name Id Version Source
--------------------------------------------------------------
PowerShell Microsoft.PowerShell 7.3.2.0 winget
PowerShell Preview Microsoft.PowerShell.Preview 7.4.1.0 winget
使用 id
參數安裝 PowerShell 或 PowerShell Preview
winget install --id Microsoft.Powershell --source winget
winget install --id Microsoft.Powershell.Preview --source winget
注意
在使用 X86 或 X64 處理器的 Windows 系統上, winget
安裝 MSI 套件。 在使用 Arm64 處理器的系統上, winget
安裝 Microsoft Store (MSIX) 套件。 如需詳細資訊,請參閱 從 Microsoft Store 安裝。
安裝 MSI 套件
若要在 Windows 上安裝 PowerShell,請使用下列連結從 GitHub 下載安裝套件。
下載之後,按兩下安裝程式檔案,並遵循提示。
安裝程式會在 Windows [開始] 功能表中建立捷徑。
- 套件預設安裝在
$env:ProgramFiles\PowerShell\<version>
- 您可以透過 [開始] 功能表或
$env:ProgramFiles\PowerShell\<version>\pwsh.exe
啟動 PowerShell
注意
PowerShell 7.3 會安裝到新的目錄,並與 Windows PowerShell 5.1 並存執行。 PowerShell 7.3 是就地升級,可取代 PowerShell 7.0 和更低版本。
- PowerShell 7.3 已安裝至
$env:ProgramFiles\PowerShell\7
- 系統會在
$env:PATH
中新增$env:ProgramFiles\PowerShell\7
資料夾 - 刪除先前發行版本本的資料夾
如果您需要與其他版本並存執行 PowerShell 7.3,請使用 ZIP 安裝 方法將另一個版本安裝至不同的資料夾。
PowerShell 7.2 和更新版本中的 Microsoft Update 支援
PowerShell 7.2 和更新版本支援 Microsoft Update。 當您啟用此功能時,您會在傳統 Microsoft Update (MU) 管理流程中取得最新的 PowerShell 7 更新,無論是搭配商務用Windows Update、WSUS、Microsoft 端點Configuration Manager或 [設定] 中的互動式 MU 對話方塊。
PowerShell 7.3 MSI 套件包含下列命令列選項:
USE_MU
- 此屬性有兩個可能的值:1
(預設) - 選擇透過 Microsoft Update、WSUS 或 Configuration Manager 進行更新0
- 請勿選擇透過 Microsoft Update、WSUS 或 Configuration Manager 進行更新
ENABLE_MU
1
(預設) - 選擇使用 Microsoft Update 進行自動更新0
- 不要選擇使用 Microsoft Update
注意
啟用更新可能已在先前的安裝或手動設定中設定。 使用 ENABLE_MU=0
不會移除現有的設定。 此外,此設定也可以由系統管理員所控制的群組原則設定覆寫。
如需詳細資訊,請參閱 PowerShell Microsoft Update 常見問題。
從命令列安裝 MSI 套件
您可以從命令列安裝 MSI 套件,讓系統管理員不需要使用者互動即可部署套件。 MSI 套件包含下列屬性以控制安裝選項:
ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL
- 此屬性會控制在 Windows 檔案總管中將Open PowerShell
專案新增至操作功能表的選項。ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL
- 此屬性會控制在 Windows 檔案總管中將Run with PowerShell
專案新增至操作功能表的選項。ENABLE_PSREMOTING
- 此屬性控制在安裝期間啟用 PowerShell 遠端處理的選項。REGISTER_MANIFEST
- 此屬性會控制註冊 Windows 事件記錄資訊清單的選項。ADD_PATH
- 此屬性會控制將 PowerShell 新增至 Windows PATH 環境變數的選項。
下列範例示範如何在啟用所有安裝選項的情況下,以無訊息方式安裝 PowerShell。
msiexec.exe /package PowerShell-7.3.2-win-x64.msi /quiet ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ENABLE_PSREMOTING=1 REGISTER_MANIFEST=1 USE_MU=1 ENABLE_MU=1 ADD_PATH=1
如需 Msiexec.exe
的命令列選項完整清單,請參閱命令列選項。
安裝 ZIP 套件
有 PowerShell 二進位 ZIP 封存,以啟用進階的部署案例。 從 目前的發行 頁面下載下列其中一個 ZIP 封存。
依據您下載檔案的方式,可能需要使用 Unblock-File
Cmdlet 以將檔案解除封鎖。 將內容解壓縮至您選擇的位置,並從該處執行 pwsh.exe
。 與安裝 MSI 套件不同,安裝 ZIP 封存不會檢查先決條件。 若要使遠端功能能透過 WSMan 正常運作,請確定您已符合必要條件。
使用此方法,在 Microsoft Surface Pro X 之類的電腦上安裝 ARM 型 PowerShell 版本。為獲得最佳結果,請將 PowerShell 安裝到 $env:ProgramFiles\PowerShell\7
資料夾。
安裝為 .NET 全域工具
如果您已安裝 .NET Core SDK ,您可以將 PowerShell 安裝為 .NET 全域工具。
dotnet tool install --global PowerShell
Dotnet 工具安裝程式會將 $HOME\.dotnet\tools
新增至您的 $env:PATH
環境變數。
不過,目前執行的殼層沒有更新的 $env:PATH
。 您可以透過輸入 pwsh
,以從新的殼層啟動 PowerShell。
從 Microsoft Store 安裝
您可以從 Microsoft Store 安裝 PowerShell 7.3。 您可以在 Microsoft Store 網站或 Windows 的市集應用程式中找到 PowerShell 版本。
Microsoft Store 套件的優點:
- Windows 內建的自動更新
- 與其他軟體發佈機制整合,例如Intune和Configuration Manager
- 可以使用 x86、x64 或 Arm64 處理器在 Windows 系統上安裝
已知限制
根據預設,Windows 市集套件會在應用程式沙箱中執行,以虛擬化存取某些檔案系統和登錄位置。 虛擬化檔案和登錄位置的變更不會保存在應用程式沙箱外部。
此沙箱全都會封鎖對應用程式根資料夾所做的任何變更。 任何儲存在 中的 $PSHOME
系統層級組態設定都無法修改。 這包含 WSMAN 設定。 這會防止遠端工作階段連線到 PowerShell 的 Store 型安裝。 支援使用者層級設定與 SSH 遠端功能。
下列命令需要寫入 。 $PSHOME
PowerShell 的 Microsoft Store 實例不支援這些命令。
Register-PSSessionConfiguration
Update-Help -Scope AllUsers
Enable-ExperimentalFeature -Scope AllUsers
Set-ExecutionPolicy -Scope LocalMachine
如需詳細資訊,請參閱了解封裝的傳統型應用程式在 Windows 上執行的方式。
PowerShell 7.2 的變更
從 PowerShell 7.2 開始,PowerShell 套件現在會豁免檔案和登錄虛擬化。 虛擬化檔案和登錄位置的變更現在會保存在應用程式沙箱外部。 不過,對應用程式的根資料夾所做的變更仍會遭到封鎖。
重要
您必須在 Windows 組建 1903 或更高版本上執行,此豁免才能運作。
安裝預覽版本
PowerShell 7 的預覽版本安裝為 $env:ProgramFiles\PowerShell\7-preview
,使其可與 PowerShell 的非預覽版本並存執行。 PowerShell 7.4 是下一個預覽版本。
升級現有的安裝
為了在升級時獲得最佳結果,您應該使用第一次安裝 PowerShell 時所使用的相同安裝方法。 每個安裝方法都會將 PowerShell 安裝於不同的位置。 如果您不確定 PowerShell 的安裝方式,您可以比較已安裝的位置與本文中的套件資訊。 如果您已透過 MSI 套件安裝,該資訊便會顯示於 [程式和功能] 控制台中。
注意
升級時,PowerShell 不會從 LTS 版本升級至非 LTS 版本。 它只會升級至最新版的 LTS,例如,從 7.2.3 升級到 7.2.9。 若要從 LTS 版本升級到較新的穩定版本或下一個 LTS,您必須使用該版本的 MSI 安裝新版本。
當已安裝的版本不是 LTS 版本時,PowerShell 會升級至最新的穩定版本。
在 Windows 10 IoT 企業版上部署
Windows 10 IoT 企業版隨附 Windows PowerShell,我們可以將其用來部署 PowerShell 7。
# Replace the placeholder information for the following variables:
$deviceip = '<device ip address'
$zipfile = 'PowerShell-7.3.2-win-arm64.zip'
$downloadfolder = 'u:\users\administrator\Downloads' # The download location is local to the device.
# There should be enough space for the zip file and the unzipped contents.
# Create PowerShell session to target device
Set-Item -Path WSMan:\localhost\Client\TrustedHosts $deviceip
$S = New-PSSession -ComputerName $deviceIp -Credential Administrator
# Copy the ZIP package to the device
Copy-Item $zipfile -Destination $downloadfolder -ToSession $S
#Connect to the device and expand the archive
Enter-PSSession $S
Set-Location u:\users\administrator\Downloads
Expand-Archive .\PowerShell-7.3.2-win-arm64.zip
# Set up remoting to PowerShell 7
Set-Location .\PowerShell-7.3.2-win-arm64
# Be sure to use the -PowerShellHome parameter otherwise it tries to create a new
# endpoint with Windows PowerShell 5.1
.\Install-PowerShellRemoting.ps1 -PowerShellHome .
當您設定 PowerShell 遠端時,會收到錯誤訊息,且與裝置中斷連線。 PowerShell 必須重新開機 WinRM。 現在,您可以連線到裝置上的 PowerShell 7 端點。
# Be sure to use the -Configuration parameter. If you omit it, you connect to Windows PowerShell 5.1
Enter-PSSession -ComputerName $deviceIp -Credential Administrator -Configuration PowerShell.7.3.2
在 Windows 10 IoT 核心版上部署
當您包含 IOT_POWERSHELL 功能 (可供我們用來部署 PowerShell 7) 時,Windows 10 IoT 核心版會新增 Windows PowerShell。 針對 Windows 10 IoT 企業版所定義的步驟也可以用於 IoT 核心版。
若要在出貨映像中新增最新的 PowerShell,請使用 Import-PSCoreRelease 命令以在工作區包括套件,並將 OPENSRC_POWERSHELL 功能新增到您的映像。
注意
針對 ARM64 架構,當您包含IOT_POWERSHELL時,不會新增Windows PowerShell。 因此,ZIP 型安裝無法運作。 您必須使用 Import-PSCoreRelease
命令在映射中新增它。
在 Nano Server 上部署
這些指示假設 Nano Server 是一個 PowerShell 版本已在其上執行的「無周邊」作業系統。 如需詳細資訊,請參閱 Nano Server 映像建立器 文件。
目前可以使用兩種方法來部署 PowerShell 二進位檔。
- 離線 - 掛接 Nano Server VHD,並將 ZIP 檔案內容解壓縮至您在掛接映像中選擇的位置。
- 線上 - 透過 PowerShell 工作階段傳輸 ZIP 檔案,並將它解壓縮至您選擇的位置。
在這兩種情況下,您需要 Windows x64 ZIP 版本套件。 在 PowerShell 的「系統管理員」執行個體中執行命令。
PowerShell 的離線部署
- 使用您最愛的 ZIP 公用程式將套件解壓縮至已掛接 Nano Server 映像的目錄。
- 取消掛接映像和開機映像。
- 連線到 Windows PowerShell 的內建執行個體。
- 請遵循下列指示來建立使用另一個執行個體技術的遠端端點。
PowerShell 的線上部署
使用下列步驟,將 PowerShell 部署到 Nano Server。
# Replace the placeholder information for the following variables:
$ipaddr = '<Nano Server IP address>'
$credential = Get-Credential # <An Administrator account on the system>
$zipfile = 'PowerShell-7.3.2-win-x64.zip'
# Connect to the built-in instance of Windows PowerShell
$session = New-PSSession -ComputerName $ipaddr -Credential $credential
# Copy the file to the Nano Server instance
Copy-Item $zipfile c:\ -ToSession $session
# Enter the interactive remote session
Enter-PSSession $session
# Extract the ZIP file
Expand-Archive -Path C:\PowerShell-7.3.2-win-x64.zip -DestinationPath 'C:\Program Files\PowerShell 7'
如需 WSMan 型的遠端功能,請遵循下列指示來建立使用另一個執行個體技術的遠端端點。
PowerShell 遠端
PowerShell 支援透過 WSMan 與 SSH 的 PowerShell 遠端通訊協定 (PSRP)。 如需詳細資訊,請參閱
必須符合下列必要條件,才能在舊版 Windows 上透過 WSMan 啟用 PowerShell 遠端處理。
- 視) 需要安裝 Windows Management Framework (WMF) 5.1 (。 如需 WMF 的詳細資訊,請參閱 WMF 概觀。
- 在 Windows 10 以前的 Windows 版本中安裝通用 C 執行階段。 透過直接下載或 Windows Update 即可取得。 已完整修補的系統已安裝此套件。
支援的 Windows 版本
下表列出 PowerShell 版本及其支援的 Windows 版本。 支援這些版本,直到 PowerShell 版本達到終止支援 ,或 Windows 版本達到終止支援為止。
圖示表示仍支援 OS 或 PowerShell 的版本
- [
] 圖示表示該作業系統版本不再支援 PowerShell 版本
圖示表示我們尚未在該 OS 上完成 PowerShell 測試
的圖示表示不支援 OS 或 PowerShell 的版本
- 當作業系統版本和 PowerShell 版本都有
圖示時,支援該組合
Windows | 7.2 (LTS 目前) | 7.3 | 7.4 (preview) |
---|---|---|---|
Windows 支援下列處理器架構的 PowerShell。
Windows | 7.2 (LTS-current) | 7.3 | 7.4 (preview) |
---|---|---|---|
Nano Server 版本 1803+ | x64 | x64 | x64 |
Windows Server 2012 R2+ | x64、x86 | x64、x86 | x64、x86 |
Windows Server Core 2012 R2+ | x64、x86 | x64、x86 | x64、x86 |
Windows 10或 11 用戶端 | x64、x86、Arm64 | x64、x86、Arm64 | x64、x86、Arm64 |
您可以執行 winver.exe
來檢查您使用的版本。
安裝支援
Microsoft 支援此文件中的安裝方法。 可能有其他協力廠商安裝方法可從其他來源取得。 雖然這些工具和方法可能正常運作,但 Microsoft 無法支援這些方法。