Comprehensive Guide to Installing PowerShell on Linux

PowerShell is a powerful and versatile scripting language and automation tool that runs on a variety of platforms, including Linux. In this comprehensive guide, we’ll explore the different methods you can use to install PowerShell on your Linux distribution.

Snap Package

One of the easiest ways to install PowerShell on Linux is through the Snap package manager. Snaps are self-contained application packages that are easy to install, secure, and cross-platform. To get started, you’ll first need to ensure that snapd is installed on your system. You can follow these instructions to do so.

Once snapd is installed, you can install the latest stable version of PowerShell using the following command:

sudo snap install powershell --classic

If you want to install the latest Long-Term Support (LTS) version, use the following command:

sudo snap install powershell --channel=lts/stable --classic

Alternatively, you can install the preview version of PowerShell using:

sudo snap install powershell-preview --classic

After installation, Snap will automatically keep your PowerShell installation up-to-date. You can manually trigger an upgrade using the following commands:

sudo snap refresh powershell
sudo snap refresh powershell-preview

To uninstall PowerShell installed via Snap, use the following commands:

sudo snap remove powershell
sudo snap remove powershell-preview

Binary Archives

PowerShell also provides binary tar.gz archives for various Linux distributions. This method allows you to install any version of PowerShell, including the latest stable, LTS, or preview releases. You can download the appropriate archive from the PowerShell GitHub releases page.

Before installing the binary archive, you’ll need to ensure that your system has the necessary dependencies installed. The dependencies can vary depending on your Linux distribution. The following list provides the distributions officially supported by Microsoft, along with their dependency information:

To install PowerShell from a binary archive, follow these steps:

  1. Download the appropriate tar.gz archive for your platform:
    curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.4.3/powershell-7.4.3-linux-x64.tar.gz
    
  2. Create the target folder where PowerShell will be installed:
    sudo mkdir -p /opt/microsoft/powershell/7
    
  3. Extract the archive to the target folder:
    sudo tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7
    
  4. Set the execute permissions for the pwsh binary:
    sudo chmod +x /opt/microsoft/powershell/7/pwsh
    
  5. Create a symbolic link to the pwsh binary:
    sudo ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh
    

To uninstall PowerShell installed from a binary archive, simply run the following command:

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

.NET Global Tool

If you already have the .NET Core SDK installed on your system, you can install PowerShell as a .NET Global Tool. This is a convenient option if you’re already working with .NET on your Linux machine.

To install PowerShell as a .NET Global Tool, use the following command:

dotnet tool install --global PowerShell

The dotnet tool installer will add the ~/.dotnet/tools directory to your PATH environment variable. However, the currently running shell may not have the updated PATH. You can start PowerShell from a new shell by typing pwsh.

In summary, this comprehensive guide has covered the three main methods for installing PowerShell on Linux: using the Snap package, binary archives, and as a .NET Global Tool. Each method has its own advantages, and the choice will depend on your specific requirements and the Linux distribution you’re using. Regardless of the method, you now have the knowledge to get PowerShell up and running on your Linux system.

For more information and support, please refer to the PowerShell community support options.