- 在 macOS 上安装 PowerShell CoreInstalling PowerShell Core on macOS
- 本文内容
- 关于 BrewAbout Brew
- 通过 Homebrew 在 macOS 10.12 或更高版本上安装最新的稳定版本Installation of latest stable release via Homebrew on macOS 10.12 or higher
- 通过 Homebrew 在 macOS 10.12 或更高版本上安装最新的预览版Installation of latest preview release via Homebrew on macOS 10.12 or higher
- 通过直接下载安装Installation via Direct Download
- 二进制存档Binary Archives
- 安装依赖关系Installing dependencies
- 卸载 PowerShell CoreUninstalling PowerShell Core
- 路径Paths
- 其他资源Additional Resources
在 macOS 上安装 PowerShell CoreInstalling PowerShell Core on macOS
本文内容
PowerShell Core 支持 macOS 10.12 和更高版本。GitHub 版本页面上提供有所有可用包。安装此包后,运行pwsh从终端。
关于 BrewAbout Brew
Homebrew 是 macOS 的首选包管理器。如果未找到 brew 命令,则需要按照说明安装 Homebrew。
通过 Homebrew 在 macOS 10.12 或更高版本上安装最新的稳定版本Installation of latest stable release via Homebrew on macOS 10.12 or higher
有关 Brew 的信息,请参阅关于 Brew。
现在,可以开始安装 PowerShell:
1. brew cask install powershell
最后,验证安装是否正常运行:
1. pwsh
当发布新版本的 PowerShell 时,更新 Homebrew 的公式并升级 PowerShell:
1. brew update
2. brew cask upgrade powershell
备注
上面的命令可以从调用 PowerShell (pwsh) 主机,但 PowerShell shell 必须退出并重新启动才能完成升级和刷新中显示的值然后$PSVersionTable。
通过 Homebrew 在 macOS 10.12 或更高版本上安装最新的预览版Installation of latest preview release via Homebrew on macOS 10.12 or higher
有关 Brew 的信息,请参阅关于 Brew。
已安装 Homebrew 后,可以安装 PowerShell。首先,安装Cask 版本可让你安装的 cask 包的替代版本的包:
1. brew tap homebrew/cask-versions
现在,可以开始安装 PowerShell:
1. brew cask install powershell-preview
最后,验证安装是否正常运行:
1. pwsh-preview
当发布新版本的 PowerShell 时,更新 Homebrew 的公式并升级 PowerShell:
1. brew update
2. brew cask upgrade powershell-preview
备注
可能会从 PowerShell (pwsh) 主机调用上面的命令,但是调用后必须退出 PowerShell 并重新启动以完成升级。刷新中显示的值和$PSVersionTable。
通过直接下载安装Installation via Direct Download
下载 PKG 包powershell-6.1.0-osx-x64.pkg(从版本页下载)到 macOS 计算机上。
可以双击文件并按照提示操作,或者从终端安装:
1. sudo installer -pkg powershell-6.1.0-osx-x64.pkg -target /
安装 OpenSSL.PowerShell 远程处理和 CIM 操作均需要 OpenSSL。
二进制存档Binary Archives
已对 macOS 平台提供 PowerShell 二进制 tar.gz 存档,以启用高级部署方案。
在 macOS 上安装二进制存档Installing binary archives on macOS
1. # Download the powershell '.tar.gz' archive
2. curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v6.1.0/powershell-6.1.0-osx-x64.tar.gz
4. # Create the target folder where powershell will be placed
5. sudo mkdir -p /usr/local/microsoft/powershell/6.1.0
7. # Expand powershell to the target folder
8. sudo tar zxf /tmp/powershell.tar.gz -C /usr/local/microsoft/powershell/6.1.0
10. # Set execute permissions
11. sudo chmod +x /usr/local/microsoft/powershell/6.1.0/pwsh
13. # Create the symbolic link that points to pwsh
14. sudo ln -s /usr/local/microsoft/powershell/6.1.0/pwsh /usr/local/bin/pwsh
安装 OpenSSL.PowerShell 远程处理和 CIM 操作均需要 OpenSSL。
安装依赖关系Installing dependencies
安装 XCode 命令行工具Install XCode command-line tools
1. xcode-select --install
安装 OpenSSLInstall OpenSSL
PowerShell 远程处理和 CIM 操作均需要 OpenSSL。可以通过 MacPorts 或 Brew 进行安装。
通过 Brew 安装 OpenSSLInstall OpenSSL via Brew
有关 Brew 的信息,请参阅关于 Brew。
若要安装 OpenSSL,请运行brew install openssl。
通过 MacPorts 安装 OpenSSLInstall OpenSSL via MacPorts
- 安装XCode 命令行工具。
- 安装 MacPorts。如果需要说明,请参阅安装指南。
- 通过运行
sudo port selfupdate更新 MacPorts。 - 通过运行
sudo port upgrade outdated升级 MacPorts 包。 - 通过运行安装 OpenSSL
sudo port install openssl。 - 链接的库,以使其可供 PowerShell:
1. sudo mkdir -p /usr/local/opt/openssl
2. sudo ln -s /opt/local/lib /usr/local/opt/openssl/lib
卸载 PowerShell CoreUninstalling PowerShell Core
如果使用 Homebrew 安装 PowerShell,使用以下命令卸载:
1. brew cask uninstall powershell
如果通过直接下载安装 PowerShell,则必须手动删除 PowerShell:
1. sudo rm -rf /usr/local/bin/pwsh /usr/local/microsoft/powershell
若要删除其他 PowerShell 路径,请参阅路径本文档中部分,并删除使用路径sudo rm。
备注
如果使用 Homebrew 安装,则此步骤并非必要步骤。
路径Paths
$PSHOME是/usr/local/microsoft/powershell/6.1.0/- 将从
~/.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 的每个主机配置。因此默认特定于宿主的配置文件位于Microsoft.PowerShell_profile.ps1所在的位置。
PowerShell 采用 macOS 上的 XDG Base Directory 规范。
由于 macOS 派生自 BSD,因此前缀为 /usr/local而不是 /opt。因此,$PSHOME是/usr/local/microsoft/powershell/6.1.0/,和符号链接放置在/usr/local/bin/pwsh。
