共用方式為


在 macOS 上安裝 PowerShell 7

PowerShell 7 有多個套件版本可安裝。 本文重點介紹安裝最新的穩定版套件。 欲了解更多套件版本資訊,請參閱 PowerShell 支援生命週期 文章。

PowerShell 7 的新版本取代了先前的 PowerShell 7 版本。 PowerShell 的預覽版本可以與其他 PowerShell 版本並存安裝。 較新的預覽版本取代了先前的預覽版本。 如果你需要將 PowerShell 7.5 與先前版本並行執行,請使用二進位壓縮方法重新安裝先前版本。

選擇安裝方法

有數種方式可以在macOS上安裝PowerShell。 自製軟體是首選的安裝方式。

使用 Homebrew 安裝 PowerShell 7

Homebrew 是 macOS 慣用的套件管理員。 如果找不到命令,您必須依照其指示安裝 Homebrew。

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

安裝之後,安裝 PowerShell。

下列命令會安裝最新穩定版本的 PowerShell:

brew install --cask powershell

如果你想要 LTS 或 Preview 版本的 PowerShell,可以用 Homebrew 的 tap 方法安裝。 選擇你想安裝的點擊版本:

  • powershell/tap/powershell-lts
  • powershell/tap/powershell-preview

例如,請使用以下指令安裝預覽版:

brew install powershell/tap/powershell-preview

手動下載並安裝套件

從發行頁面下載安裝套件。 選擇你想安裝的套件版本。

  • PowerShell 7.5
    • Arm64 處理器 - powershell-7.5.4-arm64.pkg
    • x64 處理器 - powershell-7.5.4-osx-x64.pkg
  • PowerShell 7.4 (LTS)
    • Arm64 處理器 - powershell-7.4.13-osx-arm64.pkg
    • x64 處理器 - powershell-7.4.13-osx-x64.pkg
  • PowerShell 7.6-preview
    • Arm64 處理器 - powershell-7.6.0-rc1-osx-arm64.pkg
    • x64 處理器 - powershell-7.6.0-rc1-osx-x64.pkg

使用下載的套件安裝 PowerShell 有兩種方式。

使用 Finder 安裝套件

使用 Finder 安裝 PowerShell:

  1. 開啟 Finder

  2. 找出下載的套件

  3. 對檔案按兩下

    安裝套件時,您將收到以下錯誤訊息:

    “powershell-7.5.4-osx-arm64.pkg”未打開

    Apple 無法驗證“powershell-7.5.4-osx-arm64.pkg”是否不含可能損害您的 Mac 或損害您的隱私的惡意軟件。

  4. 選取 [完成] 按鈕以關閉提示。

此錯誤消息來自 macOS 的 Gatekeeper 功能。 如需更多資訊,請參閱:在 Mac 上安全地開啟 App - Apple 支援。

嘗試開啟套件之後,請遵循下列步驟:

  1. 開啟 系統設定。
  2. 選擇 隱私和安全 並向下滾動到 安全 部分。
  3. 選取 [ 仍然開啟 ] 按鈕,以確認您要安裝 PowerShell。
  4. 當警告提示再次出現時,請選取 [無論如何開啟]。
  5. 輸入使用者名稱和密碼以允許安裝繼續進行。

從指令殼安裝套件

若要從命令列安裝 PowerShell 套件,您必須略過 Gatekeeper 檢查。 使用下列其中一種方法來安裝套件:

  • 執行 命令並使用 allowUntrusted 旗標:

    sudo installer -allowUntrusted -pkg ./Downloads/powershell-7.5.4-osx-arm64.pkg -target /
    
  • 或者,執行下列其中一個命令之後,如平常一樣安裝套件:

    • 執行 。
    • 若您使用 PowerShell,請使用 cmdlet。 請包含 檔案的完整路徑。

安裝為 .NET 全域工具

如果你已經安裝了 .NET Core SDK,可以使用 .NET Global 工具來安裝 PowerShell 7。

dotnet tool install --global PowerShell

dotnet 工具安裝程式會新增 至您的 環境變數。 不過,目前執行中的 shell 沒有更新 。 從新的命令提示字元中輸入 以啟動 PowerShell。

從二進位壓縮檔安裝 PowerShell 7

PowerShell 二進位檔案 提供予 macOS 平台,以啟用進階部署方案。 當您使用此方法安裝時,也必須手動安裝任何相依性。

將安裝套件從 版本 頁面下載到 Mac。 選擇你想安裝的壓縮檔版本。

  • PowerShell 7.4 (LTS)
    • Arm64 處理器 - powershell-7.4.13-osx-arm64.tar.gz
    • x64 處理器 - powershell-7.4.13-osx-x64.tar.gz
  • PowerShell 7.5
    • Arm64 處理器 - powershell-7.5.4-osx-arm64.tar.gz
    • x64 處理器 - powershell-7.5.4-osx-x64.tar.gz
  • PowerShell 7.6-preview
    • Arm64 處理器 - powershell-7.6.0-rc1-osx-arm64.tar.gz
    • x64 處理器 - powershell-7.6.0-rc1-osx-x64.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.5.4/powershell-7.5.4-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

安裝套件之後,請從終端機執行 。 如果你安裝了預覽套件,請執行 。

  • 該 的位置 會依你安裝的套件而異。
    • 針對穩定版 與 LTS 套件:
    • 預覽套件:
    • macOS 安裝套件會建立一個符號連結, 指向位於 的 。
  • 使用者設定檔會從 讀取
  • 預設配置檔從 讀入
  • 用戶模組讀取自
  • 共用模組從 讀取
  • 默認模組是讀取自
  • PSReadLine 歷史將記錄到

PowerShell 遵循 macOS 上的 XDG 基底目錄規格 。

更新PowerShell 7

執行以下指令將已安裝的 PowerShell 版本更新為最新版本。


brew update
brew upgrade powershell

備註

更新至較新版本的 PowerShell 時,請使用相同的安裝方法,即使用您用來執行初始安裝的 cask 或 tap。 如果您使用不同的方法,開啟新的 pwsh 工作階段會繼續使用舊版本的 PowerShell。

如果您決定使用不同的方法,您可以使用 Homebrew 連結方法來修正問題。

卸載PowerShell 7

如果您使用 Homebrew 安裝 PowerShell,請使用下列命令來卸載:

brew uninstall --cask powershell

如果你手動安裝了 PowerShell 7,就必須手動移除它。 以下指令會移除符號連結和 PowerShell 檔案。

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

用 來移除其他剩餘的 PowerShell 檔案和資料夾。

支援的 macOS 版本

Microsoft支援 PowerShell,直到 PowerShell 到達終止支援 或 macOS 版本達到終止支持為止。

支援的 macOS 版本如下:

  • macOS 26 (Tahoe) x64 和 Arm64
  • macOS 15 (Sequoia) x64 和 Arm64
  • macOS 14 (Sonoma) x64 和 Arm64

Apple 決定 macOS 的支援生命週期。 如需詳細資訊,請參閱下列:

  • macOS 版本資訊
  • Apple 安全性更新

支援的安裝方法

Microsoft支援本檔中的安裝方法。 可能有其他第三方安裝方法可從其他來源取得。 雖然這些工具和方法可能正常運作,但Microsoft無法支持這些方法。

其他資源