共用方式為


在 macOS 上安裝 PowerShell 7

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

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

選擇安裝方法

有數種方式可以在macOS上安裝PowerShell。

手動下載並安裝套件

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

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

使用 Finder 安裝套件

使用 Finder 安裝 PowerShell:

  1. 開啟 Finder

  2. 找出下載的套件

  3. 對檔案按兩下

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

    「powershell-7.5.5-osx-arm64.pkg」未開啟

    蘋果無法驗證「powershell-7.5.5-osx-arm64.pkg」是否沒有可能傷害 Mac 或侵犯隱私的惡意軟體。

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

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

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

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

從指令殼安裝套件

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

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

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

    • 執行 sudo xattr -rd com.apple.quarantine ./Downloads/powershell-7.5.5-osx-arm64.pkg
    • 若您使用 PowerShell,請使用 Unblock-File cmdlet。 請包含 .pkg 檔案的完整路徑。

安裝為 .NET 全域工具

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

dotnet tool install --global PowerShell

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

從二進位壓縮檔安裝 PowerShell 7

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

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

使用下列命令從二進位封存安裝 PowerShell。 變更下載 URL 以符合您想要安裝的版本。

# Download the powershell '.tar.gz' archive
curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.5.5/powershell-7.5.5-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 基底目錄規格

更新PowerShell 7

要更新 PowerShell,請下載新版本的套件或二進位壓縮檔並安裝。

卸載PowerShell 7

要解除安裝 PowerShell,你需要刪除應用程式資料夾和其他支援檔案。 以下指令會移除符號連結和 PowerShell 檔案。

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

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

支援的 macOS 版本

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

支援的 macOS 版本如下:

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

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

支援的安裝方法

Microsoft支援本檔中的安裝方法。 可能有其他第三方安裝方法可從其他來源取得。 雖然這些工具和方法可能正常運作,但Microsoft無法支持這些方法。 欲了解更多資訊,請參閱 「安裝 PowerShell 的替代方式」。