在 macOS 上安装 PowerShell

PowerShell 7 或更高版本需要 macOS 13 及更高版本。 PowerShell 的 GitHub 发布 页上提供了所有包。 安装包以后,从终端运行 pwsh。 在安装之前,请检查 受支持的版本列表。

注意

PowerShell 7.4 是一种就地升级,会移除之前版本的 PowerShell 7。 可以与其他版本的 PowerShell 并行安装 PowerShell 预览版。 如果需要与以前的版本并行运行 PowerShell 7.4,请使用二进制存档方法重新安装以前的版本。

注意

本文中的安装命令适用于 PowerShell 的最新稳定版本。 若要安装其他版本的 PowerShell,请调整命令以匹配所需的版本。 以下链接将带您到 GitHub 上每个版本的最新发布页面。

在“发布”页的“资产”部分中可以找到每个包的下载链接。 由于“资产”部分可能处于折叠状态,因此可能需要单击展开它。

安装 PowerShell 的最新稳定版本

可采用多种方法在 macOS 上安装 PowerShell。 选择下列方法之一:

使用 Homebrew 进行安装

如果未找到 brew 命令,则需要按照说明安装 Homebrew。

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

首先安装 brew,然后安装 PowerShell。

以下命令将安装 PowerShell 的最新稳定版本:

brew install --cask powershell

验证安装是否正常工作:

pwsh

PowerShell 新版本发布后,更新 Homebrew 公式并升级 PowerShell:

brew update
brew upgrade powershell

注意

可以从 PowerShell (pwsh) 会话中调用上述命令,但随后必须重启 PowerShell 会话才能完成升级并刷新中显示的 $PSVersionTable值。

安装 PowerShell 的最新预览版

以下命令安装 PowerShell 的最新预览版:

brew install powershell/tap/powershell-preview

运行以下命令以启动 PowerShell 预览版:

pwsh-preview

发布 PowerShell 的新预览版时,请更新 Homebrew 的公式并升级到 PowerShell 的最新预览版本:

brew update
brew upgrade powershell-preview

注意

可以从 PowerShell (pwsh) 会话中调用上述命令,但随后必须重启 PowerShell 会话才能完成升级并刷新中显示的 $PSVersionTable值。

安装 PowerShell 的最新 LTS 版本

以下命令安装 PowerShell 的最新 LTS 版本:

brew install powershell/tap/powershell-lts

验证安装:

pwsh-lts

发布 PowerShell 的新 LTS 版本后,运行以下命令以更新 Homebrew 的公式并升级到 PowerShell 的最新 LTS 版本:

brew update
brew upgrade powershell-lts

注意

在更新到较新版本的 PowerShell 时,请使用与初始安装相同的方法,即 cask 或 tap。 如果使用其他方法,则打开新的 pwsh 会话将继续使用旧版 PowerShell。

如果决定使用不同的方法,可通过多种方式使用 Homebrew 链接方法更正问题。

通过直接下载安装

从版本 7.2 开始,PowerShell 支持 Apple M 系列基于 Arm 的处理器。 将安装包从 发布 页下载到 Mac。 指向当前版本的链接包括:

可通过两种方法使用直接下载方法安装 PowerShell。

使用 Finder

使用 Finder 安装 PowerShell:

  1. 打开查找器
  2. 找到下载的包
  3. 双击该文件
  4. 按照提示操作

安装包时,可能会收到以下错误消息:

无法打开“powershell-7.5.1-osx-arm64.pkg”,因为 Apple 无法检查是否存在恶意软件。

若要使用 Finder 解决此问题,请执行以下操作:

  1. 在 Finder 中找到下载的包
  2. 按住 Ctrl 键并单击(在按住 Control(或 Ctrl) 键时单击包
  3. 从上下文菜单中选择 “打开

使用终端

从终端安装 PowerShell。 更改文件名以匹配下载的包。

sudo installer -pkg ./Downloads/powershell-7.5.1-osx-arm64.pkg -target /

安装包时,可能会收到以下错误消息:

无法打开“powershell-7.5.1-osx-arm64.pkg”,因为 Apple 无法检查是否存在恶意软件。

可以通过命令行通过几种不同的方法来解决此问题:

  • 运行命令 installer,并加上 allowUntrusted 标志:

    `sudo installer -allowUntrusted -pkg ./Downloads/powershell-7.5.1-osx-arm64.pkg -target /`
    
  • 或者像平常一样安装包,运行以下命令之一:

    • 运行 sudo xattr -rd com.apple.quarantine ./Downloads/powershell-7.5.1-osx-arm64.pkg
    • 如果您正在使用 PowerShell,请使用 Unblock-File cmdlet。 包括 .pkg 文件的完整路径。

作为 .NET 全局工具安装

如果你已安装 .NET Core SDK,则可以轻松地安装 PowerShell 作为 .NET 全局工具

dotnet tool install --global PowerShell

dotnet 工具安装程序将 ~/.dotnet/tools 添加到 PATH 环境变量中。 不过,当前运行的 shell 没有更新后的 PATH。 通过键入 pwsh从新 shell 启动 PowerShell。

二进制存档

已对 macOS 平台提供 PowerShell 二进制 tar.gz 存档,以启用高级部署方案。 使用此方法进行安装时,还必须手动安装任何依赖项。

注意

你可以使用此方法安装任何版本的 PowerShell,包括最新版本:

在 macOS 上安装二进制存档

将安装包从 发布 页下载到 Mac。 指向当前版本的链接包括:

使用以下命令从二进制存档安装 PowerShell。 更改下载 URL 以匹配要安装的版本。

# Download the powershell '.tar.gz' archive
curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.5.1/powershell-7.5.1-osx-arm64.tar.gz

# Create the target folder where powershell is placed
sudo mkdir -p /usr/local/microsoft/powershell/7

# Expand powershell to the target folder
sudo tar zxf /tmp/powershell.tar.gz -C /usr/local/microsoft/powershell/7

# Set execute permissions
sudo chmod +x /usr/local/microsoft/powershell/7/pwsh

# Create the symbolic link that points to pwsh
sudo ln -s /usr/local/microsoft/powershell/7/pwsh /usr/local/bin/pwsh

卸载 PowerShell

如果使用 Homebrew 安装 PowerShell,请使用以下命令进行卸载:

brew uninstall --cask powershell

如果通过直接下载安装 PowerShell,则必须手动删除 PowerShell:

sudo rm -rf /usr/local/bin/pwsh /usr/local/microsoft/powershell

若要删除额外的 PowerShell 路径,请参阅本文档中的 路径 部分,并使用 删除路径 sudo rm

注意

如果使用 Homebrew 进行安装,则不需要此过程。

路径

  • $PSHOME/usr/local/microsoft/powershell/7
    • macOS 安装包会创建一个符号链接 /usr/local/bin/pwsh,该链接指向 pwsh 位置中的 $PSHOME
  • 用户配置文件是从 ~/.config/powershell/profile.ps1 中读取的
  • 默认配置文件是从 $PSHOME/profile.ps1 中读取的
  • 用户模块是从 ~/.local/share/powershell/Modules 中读取的
  • 共享模块是从 /usr/local/share/powershell/Modules 中读取的
  • 默认模块是从 $PSHOME/Modules 中读取的
  • PSReadLine 的历史记录保存到 ~/.local/share/powershell/PSReadLine/ConsoleHost_history.txt

PowerShell 采用 macOS 上的 XDG Base Directory 规范

支持的版本

Microsoft 在 PowerShell 到达支持终止日期或 macOS 版本到达支持终止日期之前仍然支持 PowerShell。

  • macOS 15 (Sequoia) x64 和 Arm64
  • macOS 14 (Sonoma) x64 和 Arm64
  • macOS 13 (Ventura) x64 和 Arm64

Apple 确定 macOS 的支持生命周期。 有关详细信息,请参阅以下部分:

安装支持

Microsoft 支持本文档中的安装方法。 其他源可能还有其他安装方法。 虽然这些工具和方法可能正常工作,但Microsoft不支持这些方法。

其他资源