在 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 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.1-win-x64.msi
  • PowerShell-7.0.1-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: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 并行运行 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 - 此属性控制向 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.0.1-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.

安装 MSIX 包Installing the MSIX package

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

备注

MSIX 包尚未发布。The MSIX package has not been released yet. 发布后,此包将位于 Microsoft Store 和 GitHub“发布”页中。When released, the package will be available in the Microsoft Store and from the GitHub releases page.

安装 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-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. 为了让使用 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 功能后,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 inbox 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 inbox 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.

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