How to Install PowerShell on Linux

Install PowerShell on Linux and explore the cross-platform compatible command-line shell coming from Microsoft. It is a scripting language designed with .NET that makes automating tasks for flexible OS management a seamless operation.

PowerShell, for years, has been a go-to command-line utility for Windows users. But in recent times, the compatibility has extended to other platforms making the utility more vivid. In this article, I’ll tell you how to install PowerShell on Linux and start invoking commands in minutes.

Pre-Requisites

Before you install PowerShell on Linux, verify if you’ve access to the following:

  • Supported Linux distribution
  • An account with root access.
  • Sudo privileges

How to Install PowerShell on Linux

If you’ve all the resources ready, let’s jump right into the tutorial and guide you on how to install PowerShell on Linux in a matter of minutes.

Step 1: Update the Package Repository

As usual, before you install PowerShell on Linux, updating the package repository is crucial. The fact that PowerShell uses the apt package manager to install apt packages makes updating even more vital. Moreover, it will ensure that the installation process concludes with no issues whatsoever.

Launch the Terminal using the “Ctrl+Alt+T” key combination.

Run the following command:

$ sudo apt update
update system repository

Step 2: Install PowerShell on Linux

Having the system package repositories updated, you can now install PowerShell on Linux. Although there are several ways, the Ubuntu-based distributions usually employ two different methods. While one uses snap packages, the other is via the .NET SDK.

Install PowerShell on Linux: Snap Packages Method

The snap packages method is considered one of the easiest ways to install PowerShell on Linux. In case you wonder, what Snaps are? These are packages that operate across several Linux versions and distributions.

Now to conclude the installation process, all you need to do is launch the Terminal and run the corresponding snap command alongside the --classic flag.

$ sudo snap install powershell --classic
Install PowerShell on Linux

Install PowerShell on Linux: .NET SDK Method

Although the Snap package method works well, sometimes it fails to provide the latest available version of PowerShell. Now, if you’re willing to get the updated version of PowerShell installed, opting for the .NET SDK route is always a good idea.

You’ll use the dotnet application by employing the dotnet tool install command. It specifies a NuGet package where the desired PowerShell is located. Using the --global flag, you can secure that the dotnet installs the PowerShell package to the ~\.dotnet\tools directory. It eventually helps ensure that the concerned directory is in the system PATH, making it flexibly executable.

Invoke the following command:

$ dotnet tool install --global PowerShell 
Install PowerShell on Linux using dotnet tools

With that, you know how to effortlessly install PowerShell on Linux. But the job isn’t done as it is equally important to understand how it works.

Running Basic Command with PowerShell on Linux

To get started with PowerShell on Linux, the first basic action should be running the pwsh command. It will land you into the interactive console of PowerShell.

Despite PowerShell being a cross-platform scripting language, it also supports all the usual commands on Linux. The list includes the likes of CMD, sudo apt update, and more. One easy example is checking the version underuse.

Verifying the Installed PowerShell Version

To check which version of the PowerShell is installed on your system, you can use the $PSVersionTable command. Remember, you must see that the value against the PSEdition shows as Core while the same for Platform is Unix.

Input:

$PSVersionTable
verifying powershell version

PowerShell Modules on Linux

Modules are a significant concept of the PowerShell utility. These are the groups of commands which are built packages coming from Microsoft and the PowerShell community.

Finding the Installed Module

Talking about the default scenario, PowerShell shelters several modules. You can find all these by simply invoking the Get-Module command. The ListAvailable is also an essential parameter as it tells which Get-Module to return and the status of all modules on the filesystem that are not just loaded modules.

$ Get-Module -ListAvailable
find installed modules

For instance, you’ll find that the Pester module is installed in the /root/.local/share/powershell/Modules. This is usually the user location for all the PowerShell modules.

Getting the Exact Location of Installed Modules

To view the location of all the installed modules, run the PSModulePath environment variable, as below. 

$ env:PSModulePath -split ':'
location of installed modules

Installing PowerShell Modules

PowerShell comes with numerous pre-installed modules. However, you can leverage the PowerShell Gallery and install more module. This will help improve the overall PowerShell functionality. The PowerShell Gallery represents a repository that features a heap of modules compatible with both Windows and Linux.

Use the Find-Module parameter to search for the module you’re after.

Once done, you can use the Install-Module command and install the desired module on Linux.

PowerShell will prompt you, making you aware that the installation is being carried out via an untrusted repository. This is because the repository is usually marked as untrusted. Trust me; you don’t need to worry about this warning, instead type A and hit the Enter key to confirm.

Finally, as the process concludes, invoke the Get-InstalledModule command and if the module has been installed. Alternatively, you can run the Get-Command command.

Running PowerShell Command from Bash

Do you’ve got a PowerShell script, and you need to execute the same without even getting yourself into the PowerShell console? Well, then there is a piece of good news PowerShell commands can get executed directly from Bash.

To start with, launch any of your favorite Linux text editors

After that, paste the following command into it:

Get-PSDrive

Save the file and name it as DrivesInformation.ps1.

Now, in your Bash terminal, avoid running the pwsh command; instead, hit the Enter key followed by invoking the script inside of the PowerShell console.

With that, you now know how to install PowerShell on Linux. Apart from discussing the installation steps, the article ensures you learn about the most promising way to set everything up and run your first PowerShell command.


If this guide helped you, please share it.

Leave a Reply
Related Posts