在 Windows 上安裝 PowerShellInstalling PowerShell on Windows

有多種方法可以在 Windows 中安裝 PowerShell。There are multiple ways to install PowerShell in Windows.

PrerequisitesPrerequisites

在 Windows 7 SP1、Server 2008 R2 和更新版本上支援 PowerShell 的最新版本。The latest release of PowerShell is supported on Windows 7 SP1, Server 2008 R2, and later versions.

若要透過 WSMan 啟用 PowerShell 遠端執行功能,必須符合下列必要條件:To enable PowerShell remoting over WSMan, the following prerequisites need to be met:

  • 在 Windows 10 以前的 Windows 版本中安裝通用 C 執行階段Install the Universal C Runtime on Windows versions predating Windows 10. 透過直接下載或 Windows Update 即可取得。It's available via direct download or Windows Update. 已完整修補的系統已安裝此套件。Fully patched systems already have this package installed.
  • 在 Windows 7 和 Windows Server 2008 R2 上安裝 Windows Management Framework (WMF) 4.0 或更新版本。Install the Windows Management Framework (WMF) 4.0 or newer on Windows 7 and Windows Server 2008 R2. 如需 WMF 的詳細資訊,請參閱 WMF 概觀For more information about WMF, see WMF Overview.

下載安裝程式套件Download the installer package

若要在 Windows 上安裝 PowerShell,請從我們的 GitHub 版本 頁面下載安裝套件。To install PowerShell on Windows, download the install package from our GitHub releases page. 向下捲動至 [版本] 頁面的 [資產] 區段。Scroll down to the Assets section of the Release page. [資產] 區段可能會摺疊,因此您可能需要按一下加以展開。The Assets section may be collapsed, so you may need to click to expand it.

安裝 MSI 套件Installing the MSI package

MSI 檔案看起來像 PowerShell-<version>-win-<os-arch>.msiThe MSI file looks like PowerShell-<version>-win-<os-arch>.msi. 例如:For example:

  • PowerShell-7.0.3-win-x64.msi
  • PowerShell-7.0.3-win-x86.msi

下載後,按兩下安裝程式,並依提示操作。Once downloaded, double-click the installer and follow the prompts.

安裝程式會在 Windows [開始] 功能表中建立捷徑。The installer creates a shortcut in the Windows Start Menu.

  • 套件預設安裝在 $env:ProgramFiles\PowerShell\<version>By default the package is installed to $env:ProgramFiles\PowerShell\<version>
  • 您可以透過 [開始] 功能表或 $env:ProgramFiles\PowerShell\<version>\pwsh.exe 啟動 PowerShellYou can launch PowerShell via the Start Menu or $env:ProgramFiles\PowerShell\<version>\pwsh.exe

注意

PowerShell 7 會安裝到新目錄,並與 Windows PowerShell 5.1 並存執行。PowerShell 7 installs to a new directory and runs side-by-side with Windows PowerShell 5.1. 針對 PowerShell Core 6.x,PowerShell 7 會就地升級並移除 PowerShell Core 6.x。For PowerShell Core 6.x, PowerShell 7 is an in-place upgrade that removes PowerShell Core 6.x.

  • PowerShell 7 會安裝到 $env:ProgramFiles\PowerShell\7PowerShell 7 is installed to $env:ProgramFiles\PowerShell\7
  • 系統會在 $env:PATH 中新增 $env:ProgramFiles\PowerShell\7 資料夾The $env:ProgramFiles\PowerShell\7 folder is added to $env:PATH
  • 系統會刪除 $env:ProgramFiles\PowerShell\6 資料夾The $env:ProgramFiles\PowerShell\6 folder is deleted

如果您需要與 PowerShell 7 並存執行 PowerShell 6,請使用 ZIP 安裝方法來重新安裝 PowerShell 6。If you need to run PowerShell 6 side-by-side with PowerShell 7, reinstall PowerShell 6 using the ZIP install method.

從命令列進行系統管理安裝Administrative install from the command line

您可以從命令列安裝 MSI 套件,讓系統管理員不需要使用者互動即可部署套件。MSI packages can be installed from the command line allowing administrators to deploy packages without user interaction. MSI 套件包含下列屬性以控制安裝選項:The MSI package includes the following properties to control the installation options:

  • ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL - 此屬性控制將 [開啟 PowerShell] 項目新增至 Windows 檔案總管中的內容功能表的選項。ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL - This property controls the option for adding the Open PowerShell item to the context menu in Windows Explorer.
  • ENABLE_PSREMOTING - 此屬性控制在安裝期間啟用 PowerShell 遠端執行功能的選項。ENABLE_PSREMOTING - This property controls the option for enabling PowerShell remoting during installation.
  • REGISTER_MANIFEST - 此屬性控制用於註冊 Windows 事件記錄資訊清單的選項。REGISTER_MANIFEST - This property controls the option for registering the Windows Event Logging manifest.

下列範例示範如何在啟用所有安裝選項的情況下,以無訊息方式安裝 PowerShell。The following example shows how to silently install PowerShell with all the install options enabled.

msiexec.exe /package PowerShell-7.0.3-win-x64.msi /quiet ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 ENABLE_PSREMOTING=1 REGISTER_MANIFEST=1

如需 Msiexec.exe 的命令列選項完整清單,請參閱命令列選項For a full list of command-line options for Msiexec.exe, see Command line options.

安裝期間建立的登錄機碼Registry keys created during installation

從 PowerShell 7.1 開始,MSI 套件會建立登錄機碼以儲存 PowerShell 的安裝位置與版本。Beginning in PowerShell 7.1, the MSI package creates registry keys that store the installation location and version of PowerShell. 這些值位於 HKLM\Software\Microsoft\PowerShellCore\InstalledVersions\<GUID>These values are located in HKLM\Software\Microsoft\PowerShellCore\InstalledVersions\<GUID>. 針對每個組建類型 (發行版本或預覽版)、主要版本與架構,<GUID> 的值都不重複。The value of <GUID> is unique for each build type (release or preview), major version, and architecture.

版本Release 架構Architecture 登錄金鑰Registry Key
7.1.x 發行版本7.1.x Release x86x86 HKLM\Software\Microsoft\PowerShellCore\InstalledVersions\1d00683b-0f84-4db8-a64f-2f98ad42fe06
7.1.x 發行版本7.1.x Release x64x64 HKLM\Software\Microsoft\PowerShellCore\InstalledVersions\31ab5147-9a97-4452-8443-d9709f0516e1
7.1.x 預覽版7.1.x Preview x86x86 HKLM\Software\Microsoft\PowerShellCore\InstalledVersions\86abcfbd-1ccc-4a88-b8b2-0facfde29094
7.1.x 預覽版7.1.x Preview x64x64 HKLM\Software\Microsoft\PowerShellCore\InstalledVersions\39243d76-adaf-42b1-94fb-16ecf83237c8

系統管理員與開發人員可以加以使用來尋找 PowerShell 的路徑。This can be used by administrators and developers to find the path to PowerShell. 所有預覽版與次要發行版本的 <GUID> 值都相同。The <GUID> values will be the same for all preview and minor version releases. 每個主要發行版本的 <GUID> 值都會變更。The <GUID> values are changed for each major release.

安裝 MSIX 套件Installing the MSIX package

注意

目前尚未正式支援 MSIX 套件。The MSIX package is not officially supported at this time. 我們會繼續建置僅供內部測試之用的套件。We continue to build the package for internal testing purposes only.

若要手動在 Windows 10 用戶端上安裝 MSIX 套件,請從我們的 GitHub 發行 頁面下載 MSIX 套件。To manually install the MSIX package on a Windows 10 client, download the MSIX package from our GitHub releases page. 向下捲動至想安裝版本的 [資產] 區段。Scroll down to the Assets section of the Release you want to install. [資產] 區段可能會摺疊,因此您可能需要按一下以展開它。The Assets section may be collapsed, so you may need to click to expand it.

MSIX 檔案看起來像這樣 - PowerShell-<version>-win-<os-arch>.msixThe MSIX file looks like this - PowerShell-<version>-win-<os-arch>.msix

若要安裝套件,您必須使用 Add-AppxPackage Cmdlet。To install the package, you must use the Add-AppxPackage cmdlet.

Add-AppxPackage PowerShell-<version>-win-<os-arch>.msix

安裝 ZIP 套件Installing the ZIP package

有 PowerShell 二進位 ZIP 封存,以啟用進階的部署案例。PowerShell binary ZIP archives are provided to enable advanced deployment scenarios. 安裝 ZIP 封存並不會像 MSI 套件一樣檢查必要條件。Installing the ZIP archive doesn't check the prerequisites like the MSI packages do. [版本] 頁面下載 ZIP 封存。Download the ZIP archive from the releases page. 依據您下載檔案的方式,可能需要使用 Unblock-FileCmdlet 以將檔案解除封鎖。Depending on how you download the file you may need to unblock the file using the Unblock-File cmdlet. 將內容解壓縮至您選擇的位置,並從該處執行 pwsh.exeUnzip the contents to the location of your choice and run pwsh.exe from there. 若要使遠端功能能透過 WSMan 正常運作,請確定您已符合必要條件For remoting over WSMan to work properly, ensure that you've met the prerequisites.

在 Windows 10 IoT 企業版上部署Deploying on Windows 10 IoT Enterprise

Windows 10 IoT 企業版隨附 Windows PowerShell,我們可以將其用來部署 PowerShell 7。Windows 10 IoT Enterprise comes with Windows PowerShell, which we can use to deploy PowerShell 7.

  1. 針對目標裝置建立 PSSessionCreate PSSession to target device

    Set-Item -Path WSMan:\localhost\Client\TrustedHosts <deviceip>
    $S = New-PSSession -ComputerName <deviceIp> -Credential Administrator
    
  2. 將 ZIP 套件複製到裝置Copy the ZIP package to the device

    # change the destination to however you had partitioned it with sufficient
    # space for the zip and the unzipped contents
    # the path should be local to the device
    Copy-Item .\PowerShell-<version>-win-<os-arch>.zip -Destination u:\users\administrator\Downloads -ToSession $s
    
  3. 連接到裝置並展開封存Connect to the device and expand the archive

    Enter-PSSession $s
    Set-Location u:\users\administrator\downloads
    Expand-Archive .\PowerShell-<version>-win-<os-arch>.zip
    
  4. 設定 PowerShell 7 的遠端功能Set up remoting to PowerShell 7

    Set-Location .\PowerShell-<version>-win-<os-arch>
    # Be sure to use the -PowerShellHome parameter otherwise it'll try to create a new
    # endpoint with Windows PowerShell 5.1
    .\Install-PowerShellRemoting.ps1 -PowerShellHome .
    # You'll get an error message and will be disconnected from the device because it has to restart WinRM
    
  5. 連線到裝置上的 PowerShell 7 端點Connect to PowerShell 7 endpoint on device

    # Be sure to use the -Configuration parameter.  If you omit it, you will connect to Windows PowerShell 5.1
    Enter-PSSession -ComputerName <deviceIp> -Credential Administrator -Configuration powershell.<version>
    

在 Windows 10 IoT 核心版上部署Deploying on Windows 10 IoT Core

當您包含 IOT_POWERSHELL 功能 (可供我們用來部署 PowerShell 7) 時,Windows 10 IoT 核心版會新增 Windows PowerShell。Windows 10 IoT Core adds Windows PowerShell when you include IOT_POWERSHELL feature, which we can use to deploy PowerShell 7. 針對 Windows 10 IoT 企業版所定義的步驟也可以用於 IoT 核心版。The steps defined above for Windows 10 IoT Enterprise can be followed for IoT Core as well.

若要在出貨映像中新增最新的 powershell,請使用 Import-PSCoreRelease 命令在工作區包括套件,並新增 OPENSRC_POWERSHELL 功能到您的映像。For adding the latest powershell in the shipping image, use Import-PSCoreRelease command to include the package in the workarea and add OPENSRC_POWERSHELL feature to your image.

注意

針對 ARM64 架構,當您包括 IOT_POWERSHELL 時,不會新增 Windows Powershell。For ARM64 architecture, Windows Powershell is not added when you include IOT_POWERSHELL. 因此,以 zip 為基礎的安裝將無法使用。So the zip based install will not work. 您將必須使用 Import-PSCoreRelease 命令,將其加入映像中。You will need to use Import-PSCoreRelease command to add it in the image.

在 Nano Server 上部署Deploying on Nano Server

這些指示假設 Nano Server 是一個 PowerShell 版本已在其上執行的「無周邊」作業系統。These instructions assume that the Nano Server is a "headless" OS that has a version of PowerShell is already running on it. 如需詳細資訊,請參閱 Nano Server 映像建立器 文件。For more information, see the Nano Server Image Builder documentation.

目前可以使用兩種方法來部署 PowerShell 二進位檔。PowerShell binaries can be deployed using two different methods.

  1. 離線 - 掛接 Nano Server VHD,並將 ZIP 檔案內容解壓縮至您在掛接映像中選擇的位置。Offline - Mount the Nano Server VHD and unzip the contents of the zip file to your chosen location within the mounted image.
  2. 線上 - 透過 PowerShell 工作階段傳輸 ZIP 檔案,並將它解壓縮至您選擇的位置。Online - Transfer the zip file over a PowerShell Session and unzip it in your chosen location.

在這兩種情況下,您都需要 Windows 10 x64 ZIP 版套件。In both cases, you need the Windows 10 x64 ZIP release package. 在 PowerShell 的「系統管理員」執行個體中執行命令。Run the commands within an "Administrator" instance of PowerShell.

PowerShell 的離線部署Offline Deployment of PowerShell

  1. 使用您最愛的 ZIP 公用程式將套件解壓縮至已掛接 Nano Server 映像的目錄。Use your favorite zip utility to unzip the package to a directory within the mounted Nano Server image.
  2. 取消掛接映像和開機映像。Unmount the image and boot it.
  3. 連線到 Windows PowerShell 的內建執行個體。Connect to the built-in instance of Windows PowerShell.
  4. 請遵循下列指示來建立使用另一個執行個體技術的遠端端點。Follow the instructions to create a remoting endpoint using the "another instance technique".

PowerShell 的線上部署Online Deployment of PowerShell

使用下列步驟,將 PowerShell 部署到 Nano Server。Deploy PowerShell to Nano Server using the following steps.

  • 連線到 Windows PowerShell 的內建執行個體Connect to the built-in instance of Windows PowerShell

    $session = New-PSSession -ComputerName <Nano Server IP address> -Credential <An Administrator account on the system>
    
  • 將檔案複製到 Nano Server 執行個體Copy the file to the Nano Server instance

    Copy-Item <local PS Core download location>\powershell-<version>-win-x64.zip c:\ -ToSession $session
    
  • 輸入工作階段Enter the session

    Enter-PSSession $session
    
  • 壓縮檔 ZIP 檔案Extract the ZIP file

    # Insert the appropriate version.
    Expand-Archive -Path C:\powershell-<version>-win-x64.zip -DestinationPath "C:\PowerShell_<version>"
    
  • 如需 WSMan 型的遠端功能,請遵循下列指示來建立使用另一個執行個體技術的遠端端點。If you want WSMan-based remoting, follow the instructions to create a remoting endpoint using the "another instance technique".

安裝為 .NET 全域工具Install as a .NET Global tool

如果您已安裝 .NET Core SDK,就可以輕鬆地將 PowerShell 安裝為 .NET 全域工具If you already have the .NET Core SDK installed, it's easy to install PowerShell as a .NET Global tool.

dotnet tool install --global PowerShell

Dotnet 工具安裝程式會將 $env:USERPROFILE\dotnet\tools 新增至您的 $env:PATH 環境變數。The dotnet tool installer adds $env:USERPROFILE\dotnet\tools to your $env:PATH environment variable. 不過,目前執行的殼層沒有更新的 $env:PATHHowever, the currently running shell doesn't have the updated $env:PATH. 您可以透過輸入 pwsh,以從新的殼層啟動 PowerShell。You can start PowerShell from a new shell by typing pwsh.

透過 Winget 安裝 PowerShellInstall PowerShell via Winget

winget 命令列工具可讓開發人員探索、安裝、升級、移除及設定 Windows 10 電腦上的應用程式。The winget command-line tool enables developers to discover, install, upgrade, remove and configure applications on Windows 10 computers. 此工具是 Windows 封裝管理員服務的用戶端介面。This tool is the client interface to the Windows Package Manager service.

注意

winget 工具目前處於預覽狀態。The winget tool is currently a preview. 目前並非所有已規劃的功能都可用。Not all planned functionality is available at this time. 工具的選項與功能可能會變更。The tool's options and features are subject to change. 您不應該在生產部署案例中使用此方法。You should not use this method in a production deployment scenario. 如需系統需求清單與安裝指示,請參閱 winget 文件。See the winget documentation for a list of system requirements and install instructions.

您可以透過下列命令使用已發佈的 winget 套件來安裝 PowerShell:The following commands can be used to install PowerShell using the published winget packages:

  1. 搜尋最新版的 PowerShellSearch for the latest version of PowerShell

    winget search Microsoft.PowerShell
    
    Name               Id                           Version
    ---------------------------------------------------------------
    PowerShell         Microsoft.PowerShell         7.0.3
    PowerShell-Preview Microsoft.PowerShell-Preview 7.1.0-preview.5
    
  2. 使用 --exact 參數安裝特定版本的 PowerShellInstall a version of PowerShell using the --exact parameter

    winget install --name PowerShell --exact
    winget install --name PowerShell-Preview --exact
    

如何建立遠端端點How to create a remoting endpoint

PowerShell 支援透過 WSMan 與 SSH 的 PowerShell 遠端通訊協定 (PSRP)。PowerShell supports the PowerShell Remoting Protocol (PSRP) over both WSMan and SSH. 如需詳細資訊,請參閱For more information, see:

安裝支援Installation support

Microsoft 支援此文件中的安裝方法。Microsoft supports the installation methods in this document. 其他來源可能會提供其他安裝方法。There may be other methods of installation available from other sources. 雖然那些工具與方法都有用,但 Microsoft 無法支援那些方法。While those tools and methods may work, Microsoft cannot support those methods.