在 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 存放庫中每個版本的發行頁面。

每個套件的下載連結都可以在 [發行] 頁面的 [資產 ] 區段中找到。 [資產] 區段可能會折疊,因此您可能需要按兩下以展開它。

winget,Windows 封裝管理員,是命令行工具,可讓使用者探索、安裝、升級、移除及設定 Windows 用戶端電腦上的應用程式。 此工具是 Windows 封裝管理員服務的用戶端介面。 winget命令行工具預設會搭配 Windows 11 和新式 Windows 10 版本作為應用程式安裝程式

注意

如需系統需求和安裝指示的清單,請參閱 winget 檔Winget 無法在 Windows Server 2022 或舊版上使用。

Windows Server 2025 Preview Build 26085 和更新版本僅包含 winget Windows Server 的桌面體驗 。 如需詳細資訊,請參閱 宣佈 Windows Server Preview 組建 26085

下列命令可用來使用已發佈 winget 的套件來安裝 PowerShell:

搜尋最新版的 PowerShell

winget search Microsoft.PowerShell
Name               Id                           Version   Source
-----------------------------------------------------------------
PowerShell         Microsoft.PowerShell         7.4.2.0   winget
PowerShell Preview Microsoft.PowerShell.Preview 7.5.0.2   winget

使用 id 參數安裝 PowerShell 或 PowerShell 預覽

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>
  • 您可以透過 [開始] 功能表啟動 PowerShell 或 $env:ProgramFiles\PowerShell\<version>\pwsh.exe

注意

PowerShell 7.4 會安裝到新的目錄,並與 Windows PowerShell 5.1 並排執行。 PowerShell 7.4 是移除舊版 PowerShell 7 的就地升級。 PowerShell 的預覽版本可以與其他 PowerShell 版本並存安裝。

  • PowerShell 7.4 已安裝至 $env:ProgramFiles\PowerShell\7
  • 資料夾 $env:ProgramFiles\PowerShell\7 已新增至 $env:PATH

如果您需要與其他版本並存執行 PowerShell 7.4,請使用 ZIP 安裝 方法將另一個版本安裝到不同的資料夾。

PowerShell 7.2 和更新版本中的 Microsoft Update 支援

PowerShell 7.2 和更新版本支援 Microsoft Update。 當您啟用此功能時,您會在傳統 Microsoft Update (MU) 管理流程中取得最新的 PowerShell 7 更新,無論是使用商務用 Windows Update、WSUS、Microsoft Endpoint Configuration Manager,還是 設定 中的互動式 MU 對話框。

PowerShell 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 - 此屬性會控制將專案新增 Open PowerShell 至 Windows 檔案總管中操作功能表的選項。
  • ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL - 此屬性會控制將專案新增 Run with PowerShell 至 Windows 檔案總管中操作功能表的選項。
  • ENABLE_PSREMOTING - 此屬性控制在安裝期間啟用 PowerShell 遠端的選項。
  • REGISTER_MANIFEST - 這個屬性會控制註冊 Windows 事件記錄指令清單的選項。
  • ADD_PATH - 此屬性控制將 PowerShell 新增至 Windows PATH 環境變數的選項。
  • DISABLE_TELEMETRY - 此屬性會藉由設定 POWERSHELL_TELEMETRY_OPTOUT 環境變數,控制停用PowerShell遙測的選項。
  • INSTALLFOLDER - 此屬性會控制安裝目錄。 預設值為 $env:ProgramFiles\PowerShell\。 這是安裝程式建立已建立版本化子資料夾的位置。 您無法變更版本化子資料夾的名稱。
    • 針對目前的版本,已建立版本的子資料夾為 7
    • 針對預覽版本,已建立版本的子資料夾為 7-preview

下列範例示範如何以無訊息方式安裝已啟用所有安裝選項的PowerShell。

msiexec.exe /package PowerShell-7.4.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。 您可以在 Microsoft Store 網站或 Windows 的市集應用程式中找到 PowerShell 版本

Microsoft Store 套件的優點:

  • Windows 中內建的自動更新
  • 與其他軟體發佈機制整合,例如 Intune 和 Configuration Manager
  • 可以使用 x86、x64 或 Arm64 處理器在 Windows 系統上安裝

已知的限制

根據預設,Windows 市集套件會在應用程式沙箱中執行,以虛擬化存取某些檔案系統和登錄位置。 虛擬化檔案和登錄位置的變更不會保存在應用程式沙箱外部。

此沙箱會封鎖應用程式根資料夾的所有變更。 任何儲存在中的 $PSHOME 系統層級組態設定都無法修改。 這包括 WSMAN 組態。 這可防止遠程會話連線到以市集為基礎的PowerShell安裝。 支援用戶層級設定和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 的安裝方式,您可以檢查變數的值 $PSHOME ,其一律會指向包含目前會話執行之 PowerShell 的目錄。

  • 如果值為 $HOME\.dotnet\tools,則 PowerShell 是隨 .NET Global 工具一起安裝。
  • 如果值為 $Env:ProgramFiles\PowerShell\7,PowerShell 會安裝為 MSI 套件 ,或在 X86 或 x64 處理器的電腦上使用 Winget 安裝。
  • 如果值以 開頭 $Env:ProgramFiles\WindowsApps\,則PowerShell會安裝為 Microsoft Store 套件 ,或在具有ARM處理器的電腦上使用 Winget 安裝。
  • 如果值是任何其他值,可能是 PowerShell 已安裝為 ZIP 套件

如果您透過 MSI 套件安裝,該資訊也會出現在 [程式和功能] 控制台 中。

若要判斷是否可以使用 Winget 升級 PowerShell,請執行下列命令:

winget list --name PowerShell --upgrade-available

如果有可用的升級,輸出會指出最新的可用版本。

注意

升級時,PowerShell 不會從 LTS 版本升級至非 LTS 版本。 它只會升級至最新版本的 LTS,例如,從 7.2.3 升級至 7.2.19。 若要從 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.4.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.4.2-win-arm64.zip

# Set up remoting to PowerShell 7
Set-Location .\PowerShell-7.4.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.4.2

在 Windows 10 IoT 核心版 上部署

當您包含 Windows PowerShell 時,Windows 10 IoT 核心版 新增 Windows PowerShell IOT_POWERSHELL功能,我們可用來部署PowerShell 7。 針對IoT核心版,也可以遵循上述針對 Windows 10 IoT 企業版所定義的步驟。

若要在出貨映射中新增最新的 PowerShell,請使用 Import-PSCoreRelease 命令將套件包含在 workarea 中,並將OPENSRC_POWERSHELL功能新增至您的映射。

注意

針對 ARM64 架構,當您包含 IOT_POWERSHELL 時,不會新增 Windows PowerShell。 因此,ZIP 型安裝無法運作。 您必須使用 Import-PSCoreRelease 命令在映像中新增它。

在 Nano Server 上部署

這些指示假設 Nano Server 是具有 PowerShell 版本已在其上執行的「無頭部」操作系統。 如需詳細資訊,請參閱 Nano Server Image Builder 檔。

您可以使用兩種不同的方法來部署PowerShell二進位檔。

  1. 離線 - 掛接 Nano Server VHD,並將 zip 檔案的內容解壓縮到所掛接映像內的所選位置。
  2. 在線 - 透過 PowerShell 工作階段傳輸 zip 檔案,並將它解壓縮到您選擇的位置。

在這兩種情況下,您需要 Windows x64 ZIP 版本套件。 在 PowerShell 的 「管理員 istrator」 實例內執行命令。

PowerShell 的離線部署

  1. 使用您慣用的 zip 公用程式,將套件解壓縮到掛接 Nano Server 映射內的目錄。
  2. 將映像取消掛接並開機。
  3. 連線 至 Windows PowerShell 的內建實例。
  4. 請遵循指示,使用 「另一個實例技術」建立遠端端端點。

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.4.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.4.2-win-x64.zip -DestinationPath 'C:\Program Files\PowerShell 7'

如果您想要以 WSMan 為基礎的遠端處理,請遵循指示,使用 「另一個實例技術」來建立遠端端點。

PowerShell 遠端

PowerShell 支援透過 WSMan 和 SSH 的 PowerShell 遠端通訊協定 (PSRP)。 如需詳細資訊,請參閱

必須符合下列必要條件,才能在舊版 Windows 上啟用 WSMan 上的 PowerShell 遠端功能。

支援的 Windows 版本

下表列出 PowerShell 版本及其支援的 Windows 版本。 支援這些版本,直到 PowerShell 版本達到終止支援或 Windows 版本達到終止支持為止

  • 圖示 支援 表示仍支援 OS 或 PowerShell 版本
  • 圖示 不受支援 指出該操作系統版本不再支援PowerShell版本
  • 圖示 在測試中 表示我們尚未在該 OS 上完成 PowerShell 測試
  • 圖示 不支援 表示不支援操作系統或PowerShell版本
  • 當操作系統版本和 PowerShell 版本都有 支援 圖示時,支援該組合
Windows 7.2 (LTS-previous) 7.3 7.4 (LTS-current)
支援 Windows Server 2016、2019 或 2022 支援 支援 支援
支援 Windows Server Nano (1809 或更高版本) 支援 支援 支援
支援 Windows 11 支援 支援 支援
支援 Windows 10 1607+ 支援 支援 支援

注意

特定 Windows 版本的支持取決於 Microsoft 支援服務 生命周期原則。 如需詳細資訊,請參閱

Windows 支援下列處理器架構的 PowerShell。

Windows 7.2 (LTS-current) 7.3 7.4 (LTS-current)
Windows Server 2016+ x64、x86 x64、x86 x64、x86
Windows Server Core 2016+ x64、x86 x64、x86 x64、x86
Nano Server 版本 1803+ x64 x64 x64
Windows 11 用戶端 x64、Arm64 x64、Arm64 x64、Arm64
Windows 10 用戶端 x64、x86、Arm64 x64、x86、Arm64 x64、x86、Arm64

您可以執行 winver.exe來檢查您使用的版本。

安裝支援

Microsoft 支援本檔中的安裝方法。 可能有其他第三方方法可從其他來源取得安裝。 雖然這些工具和方法可能正常運作,但 Microsoft 無法支援這些方法。