Using PowerShell on Linux

I run PowerShell on my Windows 11 laptop, but I also have Linux VMs and use PowerShell on those too. In this blog post, I will show you two ways to install PowerShell on your Linux machine, and yes, I know… There are different Linux Operating Systems with different package managers. In my case, I used Ubuntu 22.04 πŸ™‚

Installation

Using Apt

You can use Apt to install PowerShell, but by default, the required Repository is not available after installing Ubuntu. Follow these steps to add it to your configuration:

# 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

Now that it’s added, you can use the Apt install command below to install PowerShell:

sudo apt-get install -y powershell

More information can be found here

Using Snap

When you try to install PowerShell using Apt install, it displays this message:

No apt package "powershell, but there is a snap with that name.
Try "snap install powershell"

And yes, it’s that simple πŸ™‚ But what is Snap? “A snap is a bundle of an app and its dependencies that works without modification across many different Linux distributions” Installation is made more accessible when using Snap, so to install PowerShell using snap use but you have to add the –classic parameter because PowerShell can do things outside the security sandbox that Snap offers:

sudo snap install powershell --classic

Starting PowerShell

In both cases, installing PowerShell using Apt or Snap, you can start PowerShell by running “pwsh”

Using PowerShell in Visual Studio Code

If you have Visual Studio Code installed (Use “sudo snap install code –classic” if not), you can create a new .ps1 file. When the file is created, VSCode will automatically ask you if you want to install the recommended extensions for PowerShell. Select Install, answer the Get Started Questions, and you’re good to go πŸ™‚ (You can skip the PowerShell 7 installation, it’s already on your system)

Limitations of PowerShell in Linux

And even though PowerShell runs fine in Linux, there are a few things that you can’t use because… Well, it’s not Windows, and some cmdlets/modules are just not working… For example:

The following Windows-specific modules are not included in PowerShell for Linux or macOS.

  • CimCmdlets
  • Microsoft.PowerShell.Diagnostics
  • Microsoft.WSMan.Management
  • PSDiagnostics

For more information, check this link

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.