如何在 Linux 上安裝和運行 PowerShell?

PowerShell 是 Windows 的默認“終端”應用程序以及命令提示符,它有自己的一組命令,這與類 UNIX 操作系統(如 GNU/Linux)中使用的命令有很大不同。 它與 Windows 生態系統的 .NET 框架密切相關。

你真的不需要 Linux 上的 Windows 終端,因為沒有什麼是 Bash 做不到的。 也許您可能需要它的唯一原因是您需要從事與 .NET Framework 相關的工作,或者您是一位經驗豐富的 Windows 用戶,並且只喜歡 bash 以外的任何東西。

另請閱讀:輕鬆管理 WSL 實例的 Windows 終端

目錄

在 Linux 上安裝 PowerShell

在本教程中,我們將看到在不同的 Linux 發行版上安裝此 Windows 工具的過程,並嘗試執行一些基本任務以檢查它是否在 Linux 環境中正常工作。

在 Fedora 工作站中

我們只需註冊 Microsoft Red Hat 存儲庫,然後安裝此應用程序,只需打開終端並鍵入以下命令:

                      
# Register the Microsoft RedHat repository
curl https://packages.microsoft.com/config/rhel/8/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo

# Installation
sudo dnf install --assumeyes powershell

                    
在 Fedora 上安裝 PowerShell

在基於 Debian 的 Linux 上

只需打開一個終端並輸入以下命令:

                      
# Install system components
sudo apt update  && sudo apt install -y curl gnupg apt-transport-https

# Import the public repository GPG keys
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

# Register the Microsoft Product feed
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-
                      
                        debian
                      
                      -bullseye-prod bullseye main" > /etc/apt/sources.list.d/microsoft.list'

# Install
sudo apt update && sudo apt install -y powershell

                    

在 Ubuntu Linux 上

Ubuntu可以通過在終端中鍵入以下命令來安裝 Linux、PowerShell:

                      
# 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

# Download the Microsoft repository GPG keys
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"

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

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

# Installation
sudo apt-get install -y powershell

                    

在基於 Arch Linux 的發行版上

這個包可以很容易地從 Arch User Repository 安裝到基於 Arch 的發行版上,所以只需打開一個終端並使用你最喜歡的 AUR 助手安裝它:

                      
# If you use yay :
yay -S powershell-bin

# If you use paru helper :
paru -S powershell-bin

                    

運行基本命令

現在,要直接從終端啟動此工具,只需鍵入:

                      
pwsh

                    
在 Fedora 上運行 PowerShell

可以看到,命令行選項與bash中的完全不同,連提示符都變了。 讓我們嘗試僅使用 Windows 命令來瀏覽我們的系統。

                      
# To change directory
chdir /

# to list the contents of the current directory
ls

# To go to the home directory
cd ./home/

                    
Ejecución de comandos básicos en P Shell在 P-Shell 中運行基本命令

概括

PowerShell 現已開源,是微軟為在雲計算領域獲得更多市場份額而採取的主要步驟之一。 整個“Microsoft loves Linux”計劃真的很可疑,即使現在有了 WSL(Linux 的 Windows 子系統),Microsoft 也阻止了更多的開發人員在本地使用任何 Linux 發行版。

但是值得稱讚的是,開源是正確的做法,如果發現任何漏洞,可以很快發現並修補。

參考

微軟官方文檔

相關文章