当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 macOS 上安装 PowerShell

作者:精品下载站 日期:2024-12-14 03:06:31 浏览:14 分类:玩电脑

在 macOS 上安装 PowerShell


PowerShell 7 或更高版本需要 macOS 11 及更高版本。所有包都可以在我们的 GitHub 发布页面上找到。安装软件包后,从终端运行 pwsh。安装之前,请检查下面支持的版本列表。

笔记

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

安装最新稳定版本的 PowerShell

在 macOS 上安装 PowerShell 有多种方法。选择以下方法之一:

  • 使用自制程序安装。 Homebrew 是 macOS 的首选包管理器。
  • 通过直接下载安装 PowerShell
  • 从二进制档案安装。

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

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

安装 brew 后,您就可以安装 PowerShell。

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

brew install powershell/tap/powershell

最后,验证您的安装是否正常工作:

pwsh

当新版本的PowerShell发布时,更新Homebrew的公式并升级PowerShell:

brew update
brew upgrade powershell

笔记

可以从 PowerShell (pwsh) 主机内调用上述命令,但必须退出并重新启动 PowerShell shell 才能完成升级并刷新 $PSVersionTable 中显示的值。

安装 PowerShell 的最新预览版

安装 Homebrew 后,您可以安装 PowerShell。

brew install powershell/tap/powershell-preview

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

pwsh-preview

当新版本的PowerShell发布时,更新Homebrew的公式并升级PowerShell:

brew update
brew upgrade powershell-preview

笔记

可以从 PowerShell (pwsh) 主机内调用上述命令,但必须退出并重新启动 PowerShell shell 才能完成升级。并刷新 $PSVersionTable 中显示的值。

安装 PowerShell 的最新 LTS 版本

brew install powershell/tap/powershell-lts

您现在可以验证您的安装

pwsh

当新版本的 PowerShell 发布时,运行以下命令。

brew upgrade powershell-lts

笔记

无论您使用 cask 还是 tap 方法,在更新到较新版本的 PowerShell 时,请使用与最初安装 PowerShell 时所用的相同方法。如果您使用不同的方法,打开新的 pwsh 会话将继续使用旧版本的 PowerShell。

如果您决定使用不同的方法,可以使用 Homebrew 链接方法来纠正问题。

通过直接下载安装

从版本 7.2 开始,PowerShell 支持 Apple M1 处理器。从发布页面将安装包下载到您的计算机上。当前版本的链接是:

  • 电源外壳 7.4

    • x64 处理器 - powershell-7.4.4-osx-x64.pkg
  • M1 处理器 - powershell-7.4.4-osx-arm64.pkg
  • PowerShell 7.2(LTS)

    • x64 处理器 - powershell-7.2.22-osx-x64.pkg
  • M1 处理器 - powershell-7.2.22-osx-arm64.pkg
  • PowerShell 7.5 预览版

    • x64 处理器 - powershell-7.5.0-preview.3-osx-x64.pkg
  • M1 处理器 - powershell-7.5.0-preview.3-arm64.pkg
  • 您可以双击该文件并按照提示操作,或者使用以下命令从终端安装它。更改文件名以匹配您下载的文件。

    sudo installer -pkg ./Downloads/powershell-7.4.4-osx-x64.pkg -target /
    

    如果您在 macOS Big Sur 11.5 或更高版本上运行,则在安装软件包时可能会收到以下错误消息:

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

    有两种方法可以解决此问题:

    使用取景器

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

    从命令行

    1. 运行 sudo xattr -rd com.apple.quarantine ./Downloads/powershell-7.4.4-osx-x64.pkg 。如果您使用的是 PowerShell 7 或更高版本,则可以使用 Unblock-File cmdlet。包含 .pkg 文件的完整路径。
    2. 像平常一样安装该软件包。

    笔记

    这是与包公证相关的已知问题,将在将来解决。

    作为 .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,包括最新版本:

    • 稳定版本:https://aka.ms/powershell-release?tag=stable
    • LTS 版本:https://aka.ms/powershell-release?tag=lts
    • 预览版:https://aka.ms/powershell-release?tag=preview

    在 macOS 上安装二进制存档

    从发布页面将安装包下载到您的计算机上。当前版本的链接是:

    • PowerShell 7.4(LTS)

      • x64 处理器 - powershell-7.4.4-osx-x64.tar.gz
    • M1 处理器 - powershell-7.4.4-osx-arm64.tar.gz
  • PowerShell 7.2(LTS)

    • x64 处理器 - powershell-7.2.22-osx-x64.tar.gz
  • M1 处理器 - powershell-7.2.22-osx-arm64.tar.gz
  • PowerShell 7.5 预览版

    • x64 处理器 - powershell-7.5.0-preview.3-osx-x64.tar.gz
  • M1 处理器 - powershell-7.5.0-preview.3-osx-arm64.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.4.4/powershell-7.4.4-osx-x64.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 ,该链接指向 $PSHOME 位置中的 pwsh
  • ~/.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 基本目录规范。

    支持的版本

    Microsoft 支持 PowerShell,直到 PowerShell 达到支持终止或 macOS 版本达到支持终止为止。

    • macOS 12(蒙特利)x64 和 Arm64
    • macOS 13 (Ventura) x64 和 Arm64
    • macOS 14(索诺玛)x64 和 Arm64

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

    • macOS 发行说明
    • 苹果安全更新

    安装支持

    Microsoft 支持本文档中的安装方法。可能还有其他来源提供的其他安装方法。虽然这些工具和方法可能有效,但 Microsoft 无法支持这些方法。

    其他资源

    • 自制网站
    • 自制 GitHub 存储库
    • Homebrew-Cask

    您需要 登录账户 后才能发表评论

    取消回复欢迎 发表评论:

    关灯