Computer >> Computer tutorials >  >> System >> Linux

Installing PowerShell Core on Linux Distros

Today, PowerShell is an open cross-platform shell and scripting language that can be installed and used on Windows, Linux, macOS, and other platforms. In this article, we’ll show how to install PowerShell Core on popular Linux distros (CentOS, RHEL, Debian, Kali, Mint, Ubuntu). The article is likely to be helpful for Windows administrators familiar with PowerShell to bring their experience in Linux.

  • A complete list of compiled PowerShell packages for different platforms is available in the official Microsoft repository: https://github.com/PowerShell/PowerShell/releases
  • You can find more information about PowerShell support in different Linux distros and versions here: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-linux?view=powershell-7.2

Currently, the PowerShell package is missing from almost all default Linux repositories. So, to install PowerShell in Linux, you have to download the Microsoft repository package first, import the keys, and use the Linux package manager to install PowerShell.

Note that only PowerShell Core (based on .NET Core) may be installed on Linux. You cannot install classic Windows PowerShell 5.1 in Linux. The latest PowerShell Core version available at the time of this writing is PowerShell Core 7.2 LTS.

How to Install PowerShell Core in Ubuntu 20.04 & Linux Mint 20?

Before installing PowerShell in Ubuntu or Linux Mint, you need to update the list of packages:

sudo apt-get update -y

Download GPG keys of Microsoft repository:

wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

Install Microsoft GPG keys:

sudo dpkg -i packages-microsoft-prod.deb

Update the package list with packages from packages.microsoft.com:

sudo apt-get update

Install PowerShell:

sudo apt-get install powershell -y

Installing PowerShell Core on Linux Distros

The PowerShell package will be automatically updated by the apt package manager:

sudo apt update

If a previous PowerShell Core version was installed on your computer, it will be removed.

You can also download and install the PowerShell DEB package manually:

wget -q https://github.com/PowerShell/PowerShell/releases/download/v7.2.0/powershell-lts_7.2.0-1.deb_amd64.deb
sudo dpkg -i powershell_7.2.0-1.deb_amd64.deb

Resolve dependencies and finish the installation:

sudo apt-get install -f

Installing PowerShell Core on Debian 11

To install PowerShell Core 7.0+ in Debian 11 Bullseye, download the GPG keys for the Microsoft repository first:

wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb

Then add the GPG key you have downloaded:

sudo dpkg -i packages-microsoft-prod.deb

Update the app list in the package manager:

sudo apt update

Install PowerShell:

sudo apt install -y powershell

Installing PowerShell Core on Linux Distros

PowerShell will be updated automatically when you run package update using the command below:

sudo apt update

If you want to update the PowerShell package only:

sudo apt install powershell

Hot to Install PowerShell on Red Hat Enterprise Linux (RHEL) and CentOS?

PowerShell in Linux CentOS and Red Hat Enterprise Linux (RHEL), Oracle Linux is installed in the same way:

Add the Microsoft RedHat repository to YUM:

curl https://packages.microsoft.com/config/rhel/8/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo

Install the latest available PowerShell Core version using yum (or dnf) package manager:

sudo yum install -y powershell

Installing PowerShell Core on Linux Distros

You can update the PoSh package with the command:

sudo yum update powershell

Also, you can also install PowerShell from an RPM file:

sudo yum install https://github.com/PowerShell/PowerShell/releases/download/v7.2.0/powershell-lts-7.2.0-1.rh.x86_64.rpm

PowerShell Core 7.x Installation in Kali Linux

To install PowerShell in Kali, it is enough to run the command below:

apt update && apt -y install powershell

Installing PowerShell on Linux with Snap

You can use snap to install PowerShell in different Linux distros. This installation method is recommended for Linux distributions without official PoSh support.

The command to install PowerShell using snap is:

sudo snap install powershell --classic

In snap, PowerShell is updated automatically, but you can also do it manually:

sudo snap refresh powershell

How to Remove PowerShell in Linux?

Let’s learn the commands to remove PowerShell in different Linux distributions:

  • To remove PowerShell Core in Ubuntu/Linux Mint: sudo apt remove powershell
  • To remove PowerShell in Debian: sudo apt remove powershell
  • To remove PowerShell in Kali Linux: sudo apt -y remove powershell
  • To remove PowerShell in CentOS or RHEL: sudo yum remove powershell
  • To remove PowerShell using Snap: sudo snap remove powershell

How to Run and Use PowerShell Core on Linux?

To run PowerShell command shell on Linux, run the command below:

pwsh

You can check the version of PowerShell installed on the Ubuntu host with the command:

$PSVersionTable

When typing PowerShell commands, you can use the code auto-completion feature with the TAB key.

In our example, PowerShell Core 7.2.0 is installed:

PSVersion 7.2.0
PSEdition Core
OS Linux 5.4.0-81-generic #91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021
Platform Unix

Installing PowerShell Core on Linux Distros

You can see that commands that the commands in the PowerShell shell take several times longer to complete than in the bash. You can compare the command execution time:

time pwsh -Command Get-History
time bash -c history

Installing PowerShell Core on Linux Distros

By default, some built-in PowerShell modules are available on Linux:

Get-Module –ListAvailable

  • Microsoft.PowerShell.Archive
  • Microsoft.PowerShell.Host
  • Microsoft.PowerShell.Management
  • Microsoft.PowerShell.Security
  • Microsoft.PowerShell.Utility
  • PackageManagement
  • PowerShellGet
  • PSReadLine
  • ThreadJob

Installing PowerShell Core on Linux Distros

You can install other modules, including VMWare PowerCLI.

To get the current date:

Get-date

Check host uptime:

get-Uptime

To list the contents of the current directory:

Dir

Or:

Get-ChildItem

Installing PowerShell Core on Linux Distros

Note that PowerShell commands on Linux are not case-insensitive.

To display the PowerShell command history:

History

To get help on the command:

Get-help Get-History

To run a PowerShell script from bash:

pwsh /home/sysops/CheckSpace.ps1

We will tell more about features and scenarios of using PowerShell for Linux administration and automation in the next article.