在 macOS 上安装 PowerShell 7

可以安装多个 PowerShell 7 包版本。 本文重点介绍如何安装最新的稳定发布包。 有关包版本的详细信息,请参阅 PowerShell 支持生命周期 文章。

较新版本的 PowerShell 7 取代了现有 PowerShell 7 版本。 PowerShell 的预览版可以与其他版本的 PowerShell 并行安装。 较新的预览版本将替换现有的以前的预览版本。 如果需要与以前的版本并行运行 PowerShell 7,请使用二进制存档方法重新安装以前的版本。

选择安装方法

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

下载并安装包文件

自 PowerShell 2026 年 5 月版本起,macOS PKG 包已由 Microsoft 完成公证和签名。 若要安装包,请下载 PKG 文件并打开它。

  1. 从 发布 页下载安装包。 选择要安装的包版本。

    注释

    从 macOS 27(金门)开始,macOS 仅在 Apple Silicon(Arm64)处理器上运行。

  2. 打开 查找器

  3. 找到下载的包

  4. 双击该文件

作为.NET全局工具进行安装

如果已安装 .NET Core SDK,则可以使用 .NET 全局工具安装 PowerShell 7。

dotnet tool install --global PowerShell

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

从二进制存档安装 PowerShell 7

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

从 发布 页下载安装包。 选择要安装的存档版本。

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

# Download the powershell '.tar.gz' archive
curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.6.6/powershell-7.6.6-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 7

安装包以后,从终端运行 pwsh。 如果已安装预览包,请运行 pwsh-preview。

  • $PSHOME位置因安装的包而异。
    • 对于稳定版和 LTS 软件包:/usr/local/microsoft/powershell/7/
    • 对于预览版软件包:/usr/local/microsoft/powershell/7-preview/
    • 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 规范。

更新 PowerShell 7

若要更新 PowerShell,请下载包或二进制存档的新版本并将其安装。

卸载 PowerShell 7

若要卸载 PowerShell,需要删除应用程序文件夹和其他支持文件。 以下命令删除符号链接和 PowerShell 文件。

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

用于 sudo rm 删除任何其他剩余的 PowerShell 文件和文件夹。

支持的 macOS 版本

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

支持以下版本的 macOS:

  • macOS 27(Golden Gate)Arm64
  • macOS 26 (Tahoe) x64 和 Arm64
  • macOS 15 (Sequoia) x64 和 Arm64

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

支持的安装方法

Microsoft 支持本文档中的安装方法。 其他源可能会提供其他第三方安装方法。 尽管这些工具和方法可能有效,但 Microsoft 无法支持这些方法。 有关详细信息,请参阅 安装 PowerShell 的备用方法。