在 Windows 上安装 PowerShellInstalling PowerShell on Windows

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

先决条件Prerequisites

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 更新来获取它。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 latest install package from GitHub. 你也可以在版本页上找到最新的预览版。You can also find the latest preview version on the 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.1.0-win-x64.msi
  • PowerShell-7.1.0-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.1 安装到新目录,并与 Windows PowerShell 5.1 并行运行。PowerShell 7.1 installs to a new directory and runs side-by-side with Windows PowerShell 5.1. PowerShell 7.1 是就地升级,升级后会替换 PowerShell 6.xPowerShell 7.1 is an in-place upgrade that replaces PowerShell 6.x. 或 PowerShell 7.0。or PowerShell 7.0.

  • PowerShell 7.1 安装到 $env:ProgramFiles\PowerShell\7PowerShell 7.1 is installed to $env:ProgramFiles\PowerShell\7
  • $env:ProgramFiles\PowerShell\7 文件夹已添加到 $env:PATHThe $env:ProgramFiles\PowerShell\7 folder is added to $env:PATH
  • $env:ProgramFiles\PowerShell\6 文件夹已删除The $env:ProgramFiles\PowerShell\6 folder is deleted

如果需要与其他版本并行运行 PowerShell 7.1,请使用 ZIP 安装方法将其他版本安装到其他文件夹。If you need to run PowerShell 7.1 side-by-side with other versions, use the ZIP install method to install the other version to a different folder.

通过命令行进行管理安装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 - 此属性控制向 Windows 资源管理器中的上下文菜单添加“打开 PowerShell” 项的选项。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.1.0-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.

安装 ZIP 包Installing the ZIP package

提供有 PowerShell 二进制 ZIP 存档,从而支持高级部署方案。PowerShell binary ZIP archives are provided to enable advanced deployment scenarios. 版本页下载以下 ZIP 存档之一。Download one of the following ZIP archives from the releases page.

  • PowerShell-7.1.0-win-x64.zipPowerShell-7.1.0-win-x64.zip
  • PowerShell-7.1.0-win-x86.zipPowerShell-7.1.0-win-x86.zip
  • PowerShell-7.1.0-win-arm64.zipPowerShell-7.1.0-win-arm64.zip
  • PowerShell-7.1.0-win-arm32.zipPowerShell-7.1.0-win-arm32.zip

根据该文件的下载方式,你可能需要使用 Unblock-File cmdlet 解锁。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. 与安装 MSI 包不一样,安装 ZIP 存档不会检查先决条件。Unlike installing the MSI packages, installing the ZIP archive doesn't check for prerequisites. 为了让使用 WSMan 的远程处理能够正常运行,请确保已满足先决条件For remoting over WSMan to work properly, ensure that you've met the prerequisites.

使用此方法在类似于 Microsoft Surface Pro X 的计算机上安装基于 ARM 的 PowerShell 版本。为获得最佳结果,请将 PowerShell 安装到 $env:ProgramFiles\PowerShell\7 文件夹。Use this method to install the ARM-based version of PowerShell on computers like the Microsoft Surface Pro X. For best results, install PowerShell to the to $env:ProgramFiles\PowerShell\7 folder.

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 功能后,Windows 10 IoT 核心版便会添加 Windows PowerShell,我们可以使用它来部署 PowerShell 7。Windows 10 IoT Core adds Windows PowerShell when you include IOT_POWERSHELL feature, which we can use to deploy PowerShell 7. 对于 IoT 核心版,还可以遵循为 Windows 10 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. 不过,当前运行的 shell 没有更新后的 $env:PATHHowever, the currently running shell doesn't have the updated $env:PATH. 若要从新 shell 启动 PowerShell,可以键入“pwsh”。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. 不应在生产部署方案中使用此方法。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.1.0
    PowerShell-Preview Microsoft.PowerShell-Preview 7.1.0-preview.5
    
  2. 使用 --exact 参数安装 PowerShell 版本Install a version of PowerShell using the --exact parameter

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

从 Microsoft Store 安装Installing from the Microsoft Store

PowerShell 7.1 已发布到 Microsoft Store。PowerShell 7.1 has been published to the Microsoft Store. 你可以在 Microsoft Store 网站上或在 Windows 应用商店应用程序中找到 PowerShell 版本。You can find the PowerShell release on the Microsoft Store website or in the Store application in Windows.

Microsoft Store 包的权益:Benefits of the Microsoft Store package:

  • 直接内置于 Windows 10 的自动更新Automatic updates built right into Windows 10
  • 与其他软件分发机制(如 Intune 和 SCCM)集成Integrates with other software distribution mechanisms like Intune and SCCM

的限制:Limitations:

MSIX 包在应用程序沙盒中运行,后者用于虚拟化对某些文件系统和注册表位置的访问。MSIX packages run in an application sandbox that virtualizes access to some filesystem and registry locations.

  • HKEY_CURRENT_USER 下的所有注册表更改都将在写入到专用、每个用户、每个应用位置时进行复制。All registry changes under HKEY_CURRENT_USER are copied on write to a private, per-user, per-app location. 因此,这些值不能用于其他应用程序。Therefore, those values are not available to other applications.
  • 不能修改存储在 $PSHOME 中的任何系统级配置设置。Any system-level configuration settings stored in $PSHOME cannot be modified. 其中包括 WSMAN 配置。This includes the WSMAN configuration. 这可以防止远程会话连接到 PowerShell 的基于应用商店的安装。This prevents remote sessions from connecting to Store-based installs of PowerShell. 支持用户级配置和 SSH 远程处理。User-level configurations and SSH remoting are supported.

有关详细信息,请参阅了解打包的桌面应用如何在 Windows 上运行For more information, see Understanding how packaged desktop apps run on Windows.

使用 MSIX 包Using the MSIX package

备注

PowerShell 的预览版本包含一个 MSIX 包。The preview builds of PowerShell include an MSIX package. MSIX 包尚未获得正式支持。The MSIX package is not officially supported. 生成此包的目的是为了在预览期间进行测试。The package is built for testing purposes during the preview period.

要在 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

如何创建远程处理终结点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:

升级现有安装Upgrading an existing installation

若要在升级时获得最佳结果,应使用首次安装 PowerShell 时使用的相同安装方法。For best results when upgrading, you should use the same install method you used when you first installed PowerShell. 每个安装方法都会将 PowerShell 安装在不同的位置中。Each installation method installs PowerShell in a different location. 如果你不确定如何安装 PowerShell,可以将安装的位置与本文中的包信息进行比较。If you are not sure how PowerShell was installed, you can compare the installed location with the package information in this article. 如果是通过 MSI 包安装的,则该信息将显示在“程序和功能”控制面板中。If you installed via the MSI package, that information appears in the Programs and Features Control Panel.

安装支持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.