在 macOS 上安装 PowerShellInstalling PowerShell on macOS

PowerShell 7.0 或更高版本需要 macOS 10.13 或更高版本。PowerShell 7.0 or higher require macOS 10.13 and higher. GitHub 版本页面上提供有所有可用包。All packages are available on our GitHub releases page. 安装包以后,从终端运行 pwshAfter the package is installed, run pwsh from a terminal.

备注

PowerShell 7.1 是就地升级,升级后会删除 PowerShell Core 6.x 和 7.0。PowerShell 7.1 is an in-place upgrade that removes PowerShell Core 6.x and 7.0.

/usr/local/microsoft/powershell/6 文件夹被替换为 /usr/local/microsoft/powershell/7The /usr/local/microsoft/powershell/6 folder is replaced by /usr/local/microsoft/powershell/7.

如果需要使用 PowerShell 7.1 并行运行 PowerShell Core 的更早版本,请使用二进制存档方法安装所需的版本。If you need to run and older version of PowerShell core side-by-side with PowerShell 7.1, install the version you want using the binary archive method.

可采用多种方法在 macOS 上安装 PowerShell。There are several ways to install PowerShell on macOS. 选择下列方法之一:Choose one of the following methods:

安装 PowerShell 后,应安装 OpenSSLAfter installing PowerShell, you should install OpenSSL. PowerShell 远程处理和 CIM 操作均需要 OpenSSL。OpenSSL is needed for PowerShell remoting and CIM operations.

通过 Homebrew 在 macOS 10.13 或更高版本上安装最新的稳定版本Installation of latest stable release via Homebrew on macOS 10.13 or higher

如果未找到 brew 命令,则需要按照说明安装 Homebrew。If the brew command is not found, you need to install Homebrew following their instructions.

现在,可以开始安装 PowerShell:Now, you can install PowerShell:

brew install --cask powershell

最后,验证安装是否正常运行:Finally, verify that your install is working properly:

pwsh

PowerShell 新版本发布后,更新 Homebrew 公式并升级 PowerShell:When new versions of PowerShell are released, update Homebrew's formulae and upgrade PowerShell:

brew update
brew upgrade powershell --cask

备注

可从 PowerShell (pwsh) 主机调用上面的命令,但是调用后必须退出 PowerShell 并重启以完成升级,并刷新 $PSVersionTable 中显示的值。The commands above can be called from within a PowerShell (pwsh) host, but then the PowerShell shell must be exited and restarted to complete the upgrade and refresh the values shown in $PSVersionTable.

通过 Homebrew 在 macOS 10.13 或更高版本上安装最新的预览版Installation of latest preview release via Homebrew on macOS 10.13 or higher

安装 Homebrew 后,可以安装 PowerShell。After you've installed Homebrew, you can install PowerShell. 首先,安装 Cask-Versions 包,通过它可安装替代版本的 cask 包:First, install the Cask-Versions package that lets you install alternative versions of cask packages:

brew tap homebrew/cask-versions

现在,可以开始安装 PowerShell:Now, you can install PowerShell:

brew install --cask powershell-preview

最后,验证安装是否正常运行:Finally, verify that your install is working properly:

pwsh-preview

PowerShell 新版本发布后,更新 Homebrew 公式并升级 PowerShell:When new versions of PowerShell are released, update Homebrew's formulae and upgrade PowerShell:

brew update
brew upgrade powershell-preview --cask

备注

可能会从 PowerShell (pwsh) 主机调用上面的命令,但是调用后必须退出 PowerShell 并重新启动以完成升级。The commands above can be called from within a PowerShell (pwsh) host, but then the PowerShell shell must be exited and restarted to complete the upgrade. 然后刷新 $PSVersionTable 中显示的值。and refresh the values shown in $PSVersionTable.

稳定版本和 LTS 版本也支持使用 Homebrew tap 方法安装 PowerShell。Installing PowerShell using the Homebrew tap method is also supported for stable and LTS versions.

brew install powershell/tap/powershell

现在可以验证你的安装You can now verify your install

pwsh

发布新版本的 PowerShell 时,只需运行以下命令即可。When new versions of PowerShell are released, simply run the following command.

brew upgrade powershell

备注

无论使用 cask 还是 tap 方法,在更新到较新版本的 PowerShell 时,请使用最初安装 PowerShell 所使用的相同方法。Whether you use the cask or the tap method, when updating to a newer version of PowerShell, use the same method you used to initially install PowerShell. 如果使用其他方法,则打开新的 pwsh 会话时将继续使用较旧版本的 PowerShell。If you use a different method, opening a new pwsh session will continue to use the older version of PowerShell.

如果决定使用其他方法,可以使用 Homebrew link 方法来解决此问题。If you do decide to use different methods, there are ways to correct the issue using the Homebrew link method.

通过直接下载安装Installation via Direct Download

请从版本页中将 PKG 包 powershell-7.1.0-osx-x64.pkg 下载到 CentOS 计算机。Download the PKG package powershell-7.1.0-osx-x64.pkg from the releases page onto your macOS machine.

可以双击文件并按照提示操作,或者从终端安装:You can double-click the file and follow the prompts, or install it from the terminal:

sudo installer -pkg powershell-7.1.0-osx-x64.pkg -target /

安装 OpenSSL.Install OpenSSL. PowerShell 远程处理和 CIM 操作均需要 OpenSSL。OpenSSL is needed for PowerShell remoting and CIM operations.

作为 .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 工具安装程序将 ~/.dotnet/tools 添加到 PATH 环境变量中。The dotnet tool installer adds ~/.dotnet/tools to your PATH environment variable. 但是,当前运行的 shell 没有更新的 PATHHowever, the currently running shell does not have the updated PATH. 应该可以通过键入 pwsh 从新 shell 启动 PowerShell。You should be able to start PowerShell from a new shell by typing pwsh.

安装 OpenSSL.Install OpenSSL. PowerShell 远程处理和 CIM 操作均需要 OpenSSL。OpenSSL is needed for PowerShell remoting and CIM operations.

二进制存档Binary Archives

已对 macOS 平台提供 PowerShell 二进制 tar.gz 存档,以启用高级部署方案。PowerShell binary tar.gz archives are provided for the macOS platform to enable advanced deployment scenarios. 使用此方法安装时,还必须手动安装所有依赖项。When you install using this method you must also manually install any dependencies.

安装 OpenSSL.Install OpenSSL. PowerShell 远程处理和 CIM 操作均需要 OpenSSL。OpenSSL is needed for PowerShell remoting and CIM operations.

在 macOS 上安装二进制存档Installing binary archives on macOS

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

# Create the target folder where powershell will be placed
sudo mkdir -p /usr/local/microsoft/powershell/7.1.0

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

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

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

安装依赖关系Installing dependencies

PowerShell 远程处理和 CIM 操作均需要 OpenSSL。OpenSSL is required for PowerShell remoting and CIM operations. 如有需要,可通过 MacPorts 安装 OpenSSL。You can install OpenSSL via MacPorts if needed.

备注

在同一系统上使用 MacPorts 和 Homebrew 时可能会出现问题。MacPorts and Homebrew can have problems when used to together on the same system. 但是,Homebrew 没有适用于 OpenSSL 1.0 的包。However, Homebrew does not have a package for OpenSSL 1.0. 有关详细信息,请参阅 MacPorts 常见问题解答For more information, see the MacPorts FAQ.

  1. 安装 Xcode 命令行工具。Install the Xcode command-line tools. MacPorts 需要 Xcode 工具。The Xcode tools are required by MacPorts.

    xcode-select --install
    
  2. 安装 MacPorts。Install MacPorts. 如需说明,请参阅安装指南If you need instructions, refer to the installation guide.

  3. 通过运行 sudo port selfupdate 更新 MacPorts。Update MacPorts by running sudo port selfupdate.

  4. 通过运行 sudo port upgrade outdated 升级 MacPorts 包。Upgrade MacPorts packages by running sudo port upgrade outdated.

  5. 通过运行 sudo port install openssl10 安装 OpenSSL。Install OpenSSL by running sudo port install openssl10.

  6. 链接库,使其可供 PowerShell 使用:Link the libraries to make them available to PowerShell:

    sudo mkdir -p /usr/local/opt/openssl
    sudo ln -s /opt/local/lib/openssl-1.0 /usr/local/opt/openssl/lib
    

卸载 PowerShellUninstalling PowerShell

如果使用 Homebrew 安装 PowerShell,请使用以下命令进行卸载:If you installed PowerShell with Homebrew, use the following command to uninstall:

brew cask uninstall powershell

如果通过直接下载安装 PowerShell,则必须手动删除 PowerShell:If you installed PowerShell via direct download, PowerShell must be removed manually:

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

若要删除其他 PowerShell 路径,请参阅本文档的路径一节,并使用 sudo rm 删除路径。To remove the additional PowerShell paths, refer to the paths section in this document and remove the paths using sudo rm.

备注

如果使用 Homebrew 安装,则此步骤并非必要步骤。This is not necessary if you installed with Homebrew.

路径Paths

  • $PSHOME/usr/local/microsoft/powershell/7.1.0/$PSHOME is /usr/local/microsoft/powershell/7.1.0/
  • 将从 ~/.config/powershell/profile.ps1 中读取用户配置文件User profiles will be read from ~/.config/powershell/profile.ps1
  • 将从 $PSHOME/profile.ps1 中读取默认配置文件Default profiles will be read from $PSHOME/profile.ps1
  • 将从 ~/.local/share/powershell/Modules 中读取用户模块User modules will be read from ~/.local/share/powershell/Modules
  • 将从 /usr/local/share/powershell/Modules 中读取共享模块Shared modules will be read from /usr/local/share/powershell/Modules
  • 将从 $PSHOME/Modules 中读取默认模块Default modules will be read from $PSHOME/Modules
  • PSReadline 历史记录将记录到 ~/.local/share/powershell/PSReadLine/ConsoleHost_history.txtPSReadline history will be recorded to ~/.local/share/powershell/PSReadLine/ConsoleHost_history.txt

配置文件采用 PowerShell 的每个主机配置。The profiles respect PowerShell's per-host configuration. 因此主机特定的默认配置文件位于相同位置的 Microsoft.PowerShell_profile.ps1So the default host-specific profile exists at Microsoft.PowerShell_profile.ps1 in the same locations.

PowerShell 采用 macOS 上的 XDG Base Directory 规范PowerShell respects the XDG Base Directory Specification on macOS.

由于 macOS 派生自 BSD,因此前缀为 /usr/local而不是 /optBecause macOS is a derivation of BSD, the prefix /usr/local is used instead of /opt. 因此,$PSHOME/usr/local/microsoft/powershell/7.1.0/,且符号链接位于 /usr/local/bin/pwsh 中。So, $PSHOME is /usr/local/microsoft/powershell/7.1.0/, and the symbolic link is placed at /usr/local/bin/pwsh.

安装支持Installation support

Microsoft 支持本文档中的安装方法。Microsoft supports the installation methods in this document. 其他源可能会提供其他安装方法。There may be other methods of installation available from other sources. 尽管这些工具和方法可能有效,但 Microsoft 无法支持这些方法。While those tools and methods may work, Microsoft cannot support those methods.

其他资源Additional Resources