共用方式為


在 Red Hat Enterprise Linux 上安裝 PowerShell (RHEL)

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

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

RHEL 7 使用 yum ,RHEL 8 和更新版本會使用 dnf 套件管理員。

注意

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

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

透過套件存放庫安裝

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

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

注意

此指令碼僅適用於發佈至 PMC 的受支援 RHEL 版本。

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

# Get version of RHEL
source /etc/os-release
if [ ${VERSION_ID%.*} -lt 8 ]
then majorver=7
elif [ ${VERSION_ID%.*} -lt 9 ]
then majorver=8
else majorver=9
fi

# Download the Microsoft RedHat repository package
curl -sSL -O https://packages.microsoft.com/config/rhel/$majorver/packages-microsoft-prod.rpm

# Register the Microsoft RedHat repository
sudo rpm -i packages-microsoft-prod.rpm

# Delete the downloaded package after installing
rm packages-microsoft-prod.rpm

# Update package index files
sudo dnf update
# Install PowerShell
sudo dnf install powershell -y

透過直接下載安裝

PowerShell 7.2 引進了通用套件,讓安裝更容易。 將通用套件從 發行 頁面下載到您的 RHEL 計算機。 使用此方法可在執行 RHEL 10 的系統上安裝 PowerShell。

目前版本的連結如下:

  • 適用於支援 RHEL 版本的 PowerShell 7.4.13 通用套件
    • https://github.com/PowerShell/PowerShell/releases/download/v7.4.13/powershell-7.4.13-1.rh.x86_64.rpm
  • 適用於支援 RHEL 版本的 PowerShell 7.5.4 通用套件
    • https://github.com/PowerShell/PowerShell/releases/download/v7.5.4/powershell-7.5.4-1.rh.x86_64.rpm

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

在 RHEL 8 或 9 上:

sudo dnf install https://github.com/PowerShell/PowerShell/releases/download/v7.5.4/powershell-7.5.4-1.rh.x86_64.rpm

卸載 PowerShell

在 RHEL 8 或 9 上:

sudo dnf remove powershell

支援 Arm 處理器

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

PowerShell 路徑

  • $PSHOME/opt/microsoft/powershell/7/
  • 設定檔案文稿會儲存在下列位置:
    • 所有使用者-所有主機 - $PSHOME/profile.ps1
    • 所有用戶當前主機 - $PSHOME/Microsoft.PowerShell_profile.ps1
    • 當前使用者所有主機 - ~/.config/powershell/profile.ps1
    • 當前用戶當前主機 - ~/.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 基底目錄規格

支援的版本

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

安裝套件檔案 (.rpm) 也可從 https://packages.microsoft.com/取得。

  • RHEL 10 - OS 支援終止於 2035-05-31
  • RHEL 9 - OS 支援終止於 2032-05-31
  • RHEL 8 - OS 支援終止於 2029-05-31

PowerShell 已在 Red Hat 通用基底映射 (UBI) 上進行測試。 如需詳細資訊,請參閱 UBI 資訊頁面

安裝支援

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