在macOS上安裝PowerShell

PowerShell 7 或更高版本需要macOS 11和更新版本。 所有套件都可在我們的 GitHub 版本 頁面上取得。 安裝套件之後,請從終端機執行 pwsh 。 安裝之前,請先檢查下方支援的版本清單

注意

PowerShell 7.4 是移除舊版 PowerShell 7 的就地升級。 PowerShell 的預覽版本可以與其他 PowerShell 版本並存安裝。 如果您需要與舊版並存執行 PowerShell 7.4,請使用 二進位封存 方法重新安裝舊版。

安裝最新穩定版本的PowerShell

有數種方式可以在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 殼層,才能完成升級並重新整理 中顯示的 $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殼層,才能完成升級。 並重新整理 中顯示的 $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 處理器。 將安裝套件從 發行 頁面下載到您的電腦。 目前版本的連結如下:

您可以按兩下列命令從終端機安裝檔案。 變更檔名以符合您下載的檔案。

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

如果您在 macOS Big Sur 11.5 或更新版本上執行,則安裝套件時可能會收到下列錯誤訊息:

無法開啟「powershell-7.4.2-osx-x64.pkg」,因為Apple無法檢查是否有惡意軟體。

有兩種方式可以解決此問題:

使用 Finder

  1. 在 Finder 中尋找套件。
  2. 在封裝上按 Ctrl 鍵時,按下 [控制] 。。
  3. 從操作功能表中選取 [ 開啟 ]。

從命令列中

  1. 執行 sudo xattr -rd com.apple.quarantine ./Downloads/powershell-7.4.2-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 環境變數。 不過,目前執行中的殼層沒有更新 PATH的 。 輸入 ,您應該能夠從新的殼層 pwsh啟動PowerShell。

二進位封存

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

注意

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

在macOS上安裝二進位封存

將安裝套件從 發行 頁面下載到您的電腦。 目前版本的連結如下:

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

# Download the powershell '.tar.gz' archive
curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.4.2/powershell-7.4.2-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指向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 版本清單,以及 macOS 版本的支持狀態。 這些版本會維持支援,直到 PowerShell 版本 達到終止支援 或 macOS 版本達到終止支持為止。

  • 圖示 支援 表示仍支援 OS 或 PowerShell 版本
  • 圖示 不受支援 指出該操作系統版本不再支援PowerShell版本
  • 圖示 在測試中 表示我們尚未在該 OS 上完成 PowerShell 測試
  • 圖示 不支援 表示不支援操作系統或PowerShell版本
  • 當操作系統版本和 PowerShell 版本都有 支援 圖示時,支援該組合
macOS 7.2 (LTS-previous) 7.3 7.4 (LTS-current)
支援 14 (索諾瑪) 在測試中 在測試中 在測試中
支援 13 (文圖拉) 在測試中 在測試中 在測試中
支援 12 (蒙特利) 在測試中 在測試中 在測試中
支援 11 (大蘇爾) 支援 支援 支援

macOS 的支援是由 Apple 所定義。 如需詳細資訊,請參閱下列:

macOS 支援下列處理器架構的 PowerShell:

macOS 7.2 (LTS-current) 7.3 7.4 (LTS-current)
macOS Big Sur 11.5 x64、Arm64 x64、Arm64 x64、Arm64

安裝支援

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

其他資源