dotnet tool install --global PowerShell
dotnet 工具安装程序将 $HOME\.dotnet\tools
添加到 $Env:PATH
环境变量中。
不过,当前运行的 shell 没有更新后的 $Env:PATH
。 若要从新 shell 启动 PowerShell,可以键入“pwsh
”。
可在 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 包不再受文件和注册表虚拟化的影响。 现在,对虚拟化文件和注册表位置的更改会在应用程序沙盒外部保留。 不过,仍会阻止对应用程序根文件夹所做的更改。
要使此例外生效,你必须在 Windows 1903 或更高版本上运行。
安装预览版本
PowerShell 7 的预览版安装到 $Env:ProgramFiles\PowerShell\7-preview
,这样它们可以与 PowerShell 的非预览版并行运行。 PowerShell 7.6 是下一个预览版。
升级现有安装
若要在升级时获得最佳结果,应使用首次安装 PowerShell 时使用的相同安装方法。 如果不确定 PowerShell 的安装方式,可以检查 $PSHOME
变量的值,该变量始终指向包含当前会话运行的 PowerShell 的目录。
如果值为 $HOME\.dotnet\tools
,则 PowerShell 随 .NET Global 工具一起安装。
如果值是 $Env:ProgramFiles\PowerShell\7
,则 PowerShell 作为 MSI 包安装,或用 WinGet 在具有 X86 或 x64 处理器的计算机上安装。
如果该值以 $Env:ProgramFiles\WindowsApps\
开头,PowerShell 被作为 Microsoft 应用商店包 或通过 WinGet 安装在具有 ARM 处理器的计算机上。
如果该值是其他任何值,则可能表明 PowerShell 是作为 ZIP 包 安装的。
如果是通过 MSI 包安装的,则该信息还会显示在“程序和功能”控制面板中。
若要确定是否可以使用 WinGet 升级 PowerShell,请运行以下命令:
winget list --id Microsoft.PowerShell --upgrade-available
如果有可用的升级,输出会指示最新的可用版本。 使用以下命令通过 WinGet 升级 PowerShell:
winget upgrade --id Microsoft.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.5.1-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.5.1-win-arm64.zip
# Set up remoting to PowerShell 7
Set-Location .\PowerShell-7.5.1-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.5.1
Windows 10 IoT 核心版部署
当你添加 IOT_POWERSHELL 功能后,Windows 10 IoT 核心版便会添加 Windows PowerShell,我们可以使用它来部署 PowerShell 7。 对于 IoT 核心版,还可以遵循为 Windows 10 IoT 企业版定义的步骤。
若要在随附映像中添加最新的 PowerShell,请使用 Import-PSCoreRelease 命令在工作区域中添加包,然后将 OPENSRC_POWERSHELL 功能添加到映像中。
对于 ARM64 体系结构,在你添加 IOT_POWERSHELL 功能后,它不会添加 Windows PowerShell。 因此,基于 zip 的安装将不起作用。 需要使用 Import-PSCoreRelease
命令将其添加到映像中。
在 Nano Server 上进行部署
为了更好地理解这些说明,假定 Nano Server 是已运行 PowerShell 版本的“无外设”操作系统。 有关详细信息,请参阅 Nano Server 映像生成器文档。
可以使用两种不同的方法来部署 PowerShell 二进制文件。
脱机 - 安装 Nano Server VHD,并将 zip 文件的内容解压到安装映像中的所选位置。
联机 - 通过 PowerShell 会话传输 zip 文件,并在所需位置中将其解压。
在这两种情况下,都需要 Windows x64 ZIP 版本包。 在 PowerShell 的“管理员”实例中运行命令。
PowerShell 脱机部署
使用常用 zip 实用工具将包解压到已安装的 Nano Server 映像中的目录。
卸载映像并启动。
连接到 Windows PowerShell 的内置实例。
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.5.1-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.5.1-win-x64.zip -DestinationPath 'C:\Program Files\PowerShell 7'
PowerShell 远程处理
PowerShell 支持通过 WSMan 和 SSH 的 PowerShell 远程处理协议 (PSRP)。 有关详细信息,请参阅:
在 PowerShell 中进行 SSH 远程处理
在 PowerShell 中进行 WSMan 远程处理
支持的 Windows 版本
Microsoft 在 PowerShell 到达支持终止日期或 Windows 版本到达支持终止日期之前仍然支持 PowerShell。
Microsoft 工件注册表中提供包含 Windows Server 2022、Windows Server Core 2022 和 Windows Server Nano 内部版本 1809 的 PowerShell 7.4 和 PowerShell 7.5 x64 预览版的 Docker 映像。
PowerShell 7.4 及更高版本可安装在 Windows 10 内部版本 1607 及更高版本、Windows 11、Windows Server 2016 及更高版本上。
对特定版本的 Windows 的支持由 Microsoft 支持部门生命周期策略决定。 有关详细信息,请参阅:
Windows 客户端生命周期常见问题解答
新式生命周期策略常见问题解答
可通过运行 winver.exe
来检查所使用的版本。
Microsoft 支持本文档中的安装方法。 其他源可能会提供其他第三方安装方法。 尽管这些工具和方法可能有效,但 Microsoft 无法支持这些方法。