在 Windows 上安装 PowerShell CoreInstalling PowerShell Core on Windows

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

必备条件Prerequisites

若要通过 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 prior to Windows 10. 可通过直接下载或 Windows 更新进行安装。It is available via direct download or Windows Update. 经完全修补(含可选包)且受支持的系统中已安装有通用 C 运行时。Fully patched (including optional packages), supported systems will already have this 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.

安装 MSI 包Installing the MSI package

若要在 Windows 客户端或 Windows Server(适用于 Windows 7 SP1、Server 2008 R2 以及更高版本)上安装 PowerShell,请从 GitHub 版本页面下载 MSI 包。To install PowerShell on a Windows client or Windows Server (works on Windows 7 SP1, Server 2008 R2, and later), download the MSI 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.

MSI 文件类似于 PowerShell-<version>-win-<os-arch>.msiThe MSI file looks like this - PowerShell-<version>-win-<os-arch>.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

通过命令行进行管理安装Administrative install from the command line

可以通过命令行安装 MSI 包。MSI packages can be installed from the command line. 这样,管理员可以部署包而无需用户交互。This allows administrators to deploy packages without user interaction. 适用于 PowerShell 的 MSI 包包含下列属性以控制安装选项:The MSI package for PowerShell 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 Core。The following examples shows how to silently install PowerShell Core with all the install options enabled.

msiexec.exe /package PowerShell-<version>-win-<os-arch>.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.

安装 ZIP 包Installing the ZIP package

提供有 PowerShell 二进制 ZIP 存档,从而支持高级部署方案。PowerShell binary ZIP archives are provided to enable advanced deployment scenarios. 请注意,使用 ZIP 存档时,不同于使用 MSI,你无法获取 MSI 先决条件检查。Be noted that when using the ZIP archive, you won't get the prerequisites check as in the MSI package. 要使 WSMan 远程处理正常工作,请确保您已满足先决条件For remoting over WSMan to work properly, ensure that you have met the prerequisites.

在 Windows IoT 上部署Deploying on Windows IoT

Windows IoT 已经附带了 Windows PowerShell,我们将使用它来部署 PowerShell Core 6。Windows IoT already comes with Windows PowerShell which we will use to deploy PowerShell Core 6.

  1. 在目标设备中创建 PSSessionCreate PSSession to target device

    $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 Core 6 中设置远程处理Setup remoting to PowerShell Core 6

    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 Core 6 终结点Connect to PowerShell Core 6 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>
    

在 Nano Server 上进行部署Deploying on Nano Server

这些说明假定某个 PowerShell 版本已在 Nano Server 映像上运行,并且其已经由 Nano Server 映像生成器生成。These instructions assume that a version of PowerShell is already running on the Nano Server image and that it has been generated by the Nano Server Image Builder. Nano Server 是“无外设”OS。Nano Server is a "headless" OS. 可以使用两种不同的方法部署核心二进制文件。Core binaries can be deploy 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 发布包,且需要在“管理员”PowerShell 实例中运行命令。In both cases, you will need the Windows 10 x64 ZIP release package and will need to run the commands within an "Administrator" PowerShell instance.

PowerShell Core 脱机部署Offline Deployment of PowerShell Core

  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 Core 联机部署Online Deployment of PowerShell Core

以下步骤将指导你向 Nano Server 运行实例部署 PowerShell Core,并配置其远程终结点。The following steps guide you through the deployment of PowerShell Core to a running instance of Nano Server and the configuration of its remote endpoint.

  • 连接到 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:\PowerShellCore_<version>"
    
  • 如果需要基于 WSMan 的远程处理,请按照说明使用“另一种实例技术”创建远程处理终结点。If you want WSMan-based remoting, follow the instructions to create a remoting endpoint using the "another instance technique".

如何创建远程处理终结点How to create a remoting endpoint

PowerShell Core 同时支持采用 WSMan 和 SSH 的 PowerShell 远程处理协议 (PSRP)。PowerShell Core supports the PowerShell Remoting Protocol (PSRP) over both WSMan and SSH. 有关更多信息,请参阅:For more information, see: