共用方式為


在 macOS 上安裝 PowerShell

PowerShell 7 或更高版本需要macOS 13和更新版本。 PowerShell 的 GitHub 版本 頁面上提供所有套件。 安裝套件之後,請從終端機執行 pwsh 。 安裝之前,請先檢查 支援的版本清單。

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

備註

本文中的安裝命令適用於最新穩定版本的 PowerShell。 若要安裝不同版本的 PowerShell,請調整 命令以符合您需要的版本。 下列連結會引導您前往 GitHub 上每個版本的最新版本頁面。

每個套件的下載連結都可以在 [發行] 頁面的 [ 資產 ] 區段中找到。 [資產] 區段可能會折疊,因此您可能需要按兩下以展開它。

安裝最新穩定版本的PowerShell

有數種方式可以在macOS上安裝PowerShell。 請選擇下列其中一個方法:

使用 Homebrew 安裝

brew如果找不到命令,您必須依照其指示安裝 Homebrew。

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

安裝brew之後,安裝 PowerShell。

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

brew install --cask powershell

確認安裝正常運作:

pwsh

發行新版本的 PowerShell 時,請更新 Homebrew 的公式並升級 PowerShell:

brew update
brew upgrade powershell

備註

您可以從 PowerShell (pwsh) 工作階段內呼叫先前的命令,但您必須重新啟動 PowerShell 工作階段,才能完成升級並重新整理 中顯示的 $PSVersionTable值。

安裝 PowerShell 的最新預覽版本

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

brew install powershell/tap/powershell-preview

執行下列命令以啟動 PowerShell 的預覽版本:

pwsh-preview

發行新的 PowerShell 預覽版本時,請更新 Homebrew 的公式,並升級至 PowerShell 的最新預覽版本:

brew update
brew upgrade powershell-preview

備註

您可以從 PowerShell (pwsh) 工作階段內呼叫先前的命令,但您必須重新啟動 PowerShell 工作階段,才能完成升級並重新整理 中顯示的 $PSVersionTable值。

安裝 PowerShell 的最新 LTS 版本

下列命令會安裝最新版的 PowerShell LTS:

brew install powershell/tap/powershell-lts

確認您的安裝:

pwsh-lts

發行新的 PowerShell LTS 版本時,請執行下列命令來更新 Homebrew 的公式,並升級至最新的 PowerShell LTS 版本:

brew update
brew upgrade powershell-lts

備註

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

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

透過直接下載安裝套件

從 7.2 版開始,PowerShell 支援 Apple M 系列 Arm 處理器。 將安裝套件從 版本 頁面下載到 Mac。 目前版本的連結如下:

有兩種方式可以使用直接下載方法來安裝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 檢查。 使用下列其中一種方法來安裝套件:

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

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

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

安裝為 .NET 全域工具

如果您已安裝 .NET Core SDK ,很容易就能將 PowerShell 安裝為 .NET 全域工具

dotnet tool install --global PowerShell

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

二進位檔案

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

備註

您可以使用此方法來安裝任何版本的 PowerShell,包括最新版本:

在 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.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

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

brew uninstall --cask powershell

如果您透過直接下載安裝PowerShell,則必須手動移除PowerShell:

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

若要移除額外的 PowerShell 路徑,請參閱本檔中 的路徑 一節,並使用 sudo rm移除路徑。

備註

如果您使用 Homebrew 安裝,則不需要此程式。

Paths

  • $PSHOME/usr/local/microsoft/powershell/7
    • 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 基底目錄規格

支援的版本

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

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

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

安裝支援

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

其他資源