在Ubuntu上安裝PowerShell

所有套件都可在我們的 GitHub 版本 頁面上取得。 安裝之前,請先檢查下方支援的版本清單。 安裝套件之後,請從終端機執行 pwsh 。 如果您已安裝預覽版本,請執行 pwsh-lts

注意

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

Ubuntu 使用 APT (進階套件工具) 作為套件管理員。

透過套件存放庫安裝套件存放庫

Microsoft 為 Linux 系統建置並支援各種軟體產品,並透過 Linux 封裝用戶端提供它們(apt、dnf、yum 等)。 這些 Linux 軟體套件裝載於 Microsoft 產品的 Linux 套件存放庫上https://packages.microsoft.com也稱為 PMC

從 PMC 安裝 PowerShell 是慣用的安裝方法。

注意

此腳本僅適用於支援的Ubuntu版本。

###################################
# Prerequisites

# Update the list of packages
sudo apt-get update

# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common

# Get the version of Ubuntu
source /etc/os-release

# Download the Microsoft repository keys
wget -q https://packages.microsoft.com/config/ubuntu/$VERSION_ID/packages-microsoft-prod.deb

# Register the Microsoft repository keys
sudo dpkg -i packages-microsoft-prod.deb

# Delete the Microsoft repository keys file
rm packages-microsoft-prod.deb

# Update the list of packages after we added packages.microsoft.com
sudo apt-get update

###################################
# Install PowerShell
sudo apt-get install -y powershell

# Start PowerShell
pwsh

重要

Ubuntu 已預先設定包含 .NET 套件的套件存放庫,但不包含 PowerShell。 使用這些指示來安裝PowerShell會將 Microsoft存放庫註冊為套件來源。 您可以從此存放庫安裝 PowerShell 和某些 .NET 版本。 不過,Ubuntu 套件存放庫有不同的 .NET 套件版本。 這可能會導致安裝 .NET 以供其他用途使用時發生問題。 如需這些問題的詳細資訊,請參閱 針對Linux上的 .NET 套件混合問題進行疑難解答。

您必須選擇您要用來安裝 .NET 的摘要。 您可以設定套件存放庫的優先順序,以優先於另一個存放庫。 如需如何設定優先順序的指示,請參閱 我的Linux發行版提供 .NET 套件,以及我想要使用這些套件。

透過直接下載安裝

PowerShell 7.2 引進了通用套件,讓安裝更容易。 將通用套件從 版本 頁面下載到您的Ubuntu電腦。

目前版本的連結如下:

  • 支援的Ubuntu版本PowerShell 7.4(LTS) 通用套件
    • https://github.com/PowerShell/PowerShell/releases/download/v7.4.2/powershell_7.4.2-1.deb_amd64.deb
  • 支援的Ubuntu版本PowerShell 7.3.12 (穩定) 通用套件
    • https://github.com/PowerShell/PowerShell/releases/download/v7.3.12/powershell_7.3.12-1.deb_amd64.deb
  • 支援的Ubuntu版本PowerShell 7.2.19(LTS) 通用套件
    • https://github.com/PowerShell/PowerShell/releases/download/v7.2.19/powershell_7.2.19-1.deb_amd64.deb

下列殼層腳本會下載並安裝PowerShell目前的預覽版本。 您可以變更 URL 以下載您要安裝的 PowerShell 版本。

###################################
# Prerequisites

# Update the list of packages
sudo apt-get update

# Install pre-requisite packages.
sudo apt-get install -y wget

# Download the PowerShell package file
wget https://github.com/PowerShell/PowerShell/releases/download/v7.4.2/powershell_7.4.2-1.deb_amd64.deb

###################################
# Install the PowerShell package
sudo dpkg -i powershell_7.4.2-1.deb_amd64.deb

# Resolve missing dependencies and finish the install (if necessary)
sudo apt-get install -f

# Delete the downloaded package file
rm powershell_7.4.2-1.deb_amd64.deb

# Start PowerShell Preview
pwsh-lts

卸載 PowerShell

sudo apt-get remove powershell

支援 Arm 處理器

PowerShell 7.2 和更新版本支援使用 32 位 Arm 處理器在 Ubuntu 上執行。 使用二進位封存安裝方法來安裝PowerShell,如替代方式在Linux上安裝PowerShell中所述

PowerShell 路徑

  • $PSHOME/opt/microsoft/powershell/7/
  • 設定檔案文稿會儲存在下列位置:
    • AllUsersAllHosts - $PSHOME/profile.ps1
    • AllUsersCurrentHost - $PSHOME/Microsoft.PowerShell_profile.ps1
    • CurrentUserAllHosts - ~/.config/powershell/profile.ps1
    • CurrentUserCurrentHost - ~/.config/powershell/Microsoft.PowerShell_profile.ps1
  • 模組會儲存在下列位置:
    • 使用者模組 - ~/.local/share/powershell/Modules
    • 共用模組 - /usr/local/share/powershell/Modules
    • 預設模組 - $PSHOME/Modules
  • PSReadLine 歷程記錄會記錄於 ~/.local/share/powershell/PSReadLine/ConsoleHost_history.txt

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

支援的版本

下表列出目前支援的 PowerShell 版本,以及支援的 Ubuntu 版本。 在 PowerShell 版本 達到終止支援或 Ubuntu 版本達到終止支援 之前,這些版本 仍會受到支援

  • 圖示 支援 表示仍支援 OS 或 PowerShell 版本
  • 圖示 不受支援 指出該操作系統版本不再支援PowerShell版本
  • 圖示 在測試中 表示我們尚未在該 OS 上完成 PowerShell 測試
  • 圖示 不支援 表示不支援操作系統或PowerShell版本
  • 當操作系統版本和 PowerShell 版本都有 支援 圖示時,支援該組合
Ubuntu 7.2 (LTS-previous) 7.3 7.4 (LTS-current)
支援 22.04 LTS (Jammy) 支援 支援 支援
支援 20.04 LTS (焦點) 支援 支援 支援
不受支援 18.04 LTS (仿生) 不受支援 不受支援 不受支援

僅正式支援Ubuntu的 LTS 版本。 Microsoft 不支援 過渡版本或其對等版本 。 暫時版本受到社群支援。 如需詳細資訊,請參閱 社群支援的散發套件

Ubuntu 支援下列處理器架構的PowerShell。

Ubuntu 7.2 (LTS-current) 7.3 7.4 (LTS-current)
所有支援的版本 x64、Arm32 x64、Arm32 x64、Arm32

安裝支援

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