Getting Started with PowerShell in Linux [Beginner Guide]

After Microsoft fell in love with Linux (what has popularly come to be known as “Microsoft Loves Linux”), PowerShell which was originally a Windows-only component, was open-sourced and made cross-platform on 18 August 2016, available on Linux and Mac OS.

PowerShell is a task automation and configuration management system developed by Microsoft. It is made up of a command language interpreter (shell) and scripting language built on the .NET Framework.

It offers complete access to COM (Component Object Model) and WMI (Windows Management Instrumentation), thereby allowing system administrators to carry out administrative tasks on both local and remote Windows systems as well as WS-Management and CIM (Common Information Model) enabling administration of remote Linux systems plus network devices.

Under this framework, administrative tasks are fundamentally carried out by particular .NET classes called cmdlets (pronounced command-lets).

Similar to shell scripts in Linux, users can build scripts or executables by storing groups of cmdlets in files by following certain rules. These scripts can be used as independent command-line utilities or tools.

Install PowerShell in Linux Systems

To install PowerShell in Linux, we will use the official Microsoft repository that will allow us to install through the most popular Linux package management tools such as apt-get or apt and yum or dnf.

Install PowerShell On Ubuntu

First import the public repository GPG keys, then register the Microsoft Ubuntu repository in the APT package sources list to install Powershell:

$ sudo apt-get update
$ sudo apt-get install -y wget apt-transport-https software-properties-common
$ wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
$ sudo dpkg -i packages-microsoft-prod.deb
$ sudo apt-get update
$ sudo apt-get install -y powershell

Install PowerShell On Debian 11

PowerShell for Debian distribution releases is published to package repositories for easy installation and updates.

$ sudo apt update
$ sudo apt install -y curl gnupg apt-transport-https
$ curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
$ 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'
$ sudo apt update
$ sudo apt install -y powershell

Install PowerShell On Debian 10

$ wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
$ sudo apt-get update
$ sudo apt-get install -y powershell

Install PowerShell On RHEL Systems

PowerShell for RHEL-based distributions such as CentOS Stream, Rocky, and AlmaLinux are published to official Microsoft repositories for easy installation and updates.

---------- On RHEL, CentOS, Rocky & AlmaLinux 9 ---------- 
$ curl https://packages.microsoft.com/config/rhel/9.0/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
$ sudo dnf install --assumeyes powershell

---------- On RHEL, CentOS, Rocky & AlmaLinux 8 ----------
$ curl https://packages.microsoft.com/config/rhel/8/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
$ sudo dnf install --assumeyes powershell

---------- On RHEL/CentOS 7 ----------
$ curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
$ sudo dnf install --assumeyes powershell

How to Use Powershell in Linux

In this section, we will have a brief introduction to Powershell; where we will see how to start powershell, run some basic commands, and look at how to work with files, directories, and processes. Then later dive into how to list all available commands, and show command help and aliases.

To start Powershell, type:

$ pwsh

PowerShell 7.3.3
PS /root> 

You can check the Powershell version with the command below:

PS /root> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.3
PSEdition                      Core
GitCommitId                    7.3.3
OS                             Linux 5.10.0-9-amd64 #1 SMP Debian 5.10.70-1 (2021-09-30)
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Running some basic Powershell commands on Linux.

get-date          [# Display current date]
get-uptime        [# Display server uptime]
get-location      [# Display present working directory]
Running PowerShell Commands
Running PowerShell Commands

Working with Files and Directories in Powershell

1. Create a new empty file using the two methods below:

new-item  tecmint.tex
OR
“”>tecmint.tex

Then add content to it and view the file content.

set-content tecmint.tex -value "TecMint Linux How Tos Guides"
get-content tecmint.tex
Create New File in Powershell
Create New File in Powershell

2. Delete a file in powershell.

remove-item tecmint.tex
get-content tecmint.tex
Delete File in Powershell
Delete File in Powershell

3. Create a new directory.

mkdir  tecmint-files
cd  tecmint-files
“”>domains.list
ls
Create Directory in Powershell
Create a Directory in Powershell

4. To perform a long listing, which displays details of a file/directory including mode (file type), and last modification time.

dir
Directory Long Listing in Powershell
Directory Long Listing in Powershell

5. View all running processes on your system:

get-process
View Running Processes in Powershell
View Running Processes in Powershell

6. To view details of a single/group of running processes with a given name, provide the process name as an argument to the previous command as follows:

get-process apache2
View Specific Process in Powershell
View Specific Process in Powershell

Meaning of the units in the output above:

  • NPM(K) – the amount of non-paged memory that the process is using, in kilobytes.
  • PM(K) – the amount of pageable memory that the process is using, in kilobytes.
  • WS(K) – the size of the working set of the process, in kilobytes. The working set consists of the pages of memory that were recently referenced by the process.
  • CPU(s) – the amount of processor time that the process has used on all processors, in seconds.
  • ID – process ID (PID).
  • ProcessName – the name of the process.

7. To know more, get a list of all Powershell commands for different tasks:

get-command
List Powershell Commands
List Powershell Commands

8. To learn how to use a command, view its help page (similar to the man page in Unix/Linux); in this example, you can get help for the Describe command:

get-help Describe
Powershell Help Manual
Powershell Help Manual

9. view all available command aliases, type:

get-alias
List Powershell Command Aliases
List Powershell Command Aliases

10. Last but not least, display command history (list of commands you had run previously) like so:

history
List Powershell Commands History
List Powershell Commands History

That’s all! for now, in this article, we showed you how to install Microsoft’s Powershell in Linux. To me, Powershell still has a very long way to go in comparison to the traditional Unix/Linux shells which offer, by far better, more exciting and productive features to operate a machine from the command line and importantly, for programming (scripting) purposes as well.

Visit Powershell Github repository: https://github.com/PowerShell/PowerShell

However, you can give it a try and share your views with us in the comments.

Similar Posts