How to Install and Use PowerShell on Ubuntu 20.04

PowerShell is a task-based configuration tool of the Windows operating system. It is a very powerful tool used to automate system administrator tasks. Microsoft has released PowerShell Core for Linux operating systems. With PowerShell, you can perform different administrative tasks on a remote and a local computer.

In this tutorial, we will show you how to install PowerShell on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured the server.

Getting Started

Before starting, you will need to update your system packages to the latest version. You can update them using the following command:

apt-get update -y

Once your server is updated, you can proceed to the next step.

Install PowerShell using Snap

There are two ways to install the PowerShell on Ubuntu 20.04. In this section, we will show you how to install PowerShell with snapd.

First, install the Snap package manager with the following command:

apt-get install snap snapd -y

Once installed, you can install the PowerShell with the following command:

snap install powershell --classic

Once the PowerShell is installed, connect to the PowerShell with the following command:

pwsh

You should get the following output:

PowerShell 7.1.2
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /root> 

Next, exit from the PowerShell with the following command:

PS /root> exit

You can uninstall the PowerShell from your system with the following command:

snap remove powershell

Install PowerShell from Ubuntu Repository

You can also install the PowerShell from the Ubuntu repository. First, download the PowerShell repository package with the following command:

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

Once the download is completed, install the downloaded package with the following command:

dpkg -i packages-microsoft-prod.deb

Next, update the repository and install the PowerShell with the following command:

apt-get update -y
apt-get install powershell -y

Once the installation has been completed, connect to the PowerShell with the following command:

pwsh

You should get the following output:

PowerShell 7.1.2
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /root> 

How to Use PowerShell

In this section, we will show you how to use the PowerShell command-line on Linux.

To list the directory, run the following command:

PS /root> dir

You should get the following output:

    Directory: /root

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           2/23/2021  1:48 PM                snap
-----            1/7/2021  2:45 AM      162406548 nexus-3.29.2-02-unix.tar.gz
-----           4/23/2020  7:02 PM           3124 packages-microsoft-prod.deb

To get a detail information of PowerShell, run the following command:

PS /root> Get-Host

You should get the following output:

Name             : ConsoleHost
Version          : 7.1.2
InstanceId       : 65ee7aa5-320c-478d-9337-d8642073a26a
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

To print all the PowerShell command history, run the following command:

PS /root> Get-History

You should get the following output:

  Id     Duration CommandLine
  --     -------- -----------
   1        0.026 ls -l
   2        0.069 dir
   3        0.027 dir /
   4        0.004 dir
   5        0.139 Get-Aliasias cd
   6        0.006 cd
   7        0.004 Set-Location
   8        0.042 Get-Aliasias cd
   9        0.110 Get-Process
  10        0.050 Get-Host

To get the all process information, run the following command:

PS /root> Get-Process

You should get the following output:

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
      0     0.00       3.89       0.00    7605 …03 (sd-pam)
      0     0.00       0.00       0.00     137   0 acpi_thermal_pm
      0     0.00       1.96       0.00     517 517 agetty
      0     0.00       1.79       0.00     520 520 agetty
      0     0.00       0.00       0.00     127   0 ata_sff
      0     0.00       3.76       0.04    7628 …28 bash
      0     0.00       3.88       0.00   10316 …28 bash
      0     0.00       3.85       0.03   10327 …28 bash
      0     0.00       3.69       0.00   10934 …28 bash
      0     0.00       0.00       0.00     125   0 blkcg_punt_bio
      0     0.00       0.00       0.00     175   0 charger_manager
      0     0.00       0.00       0.00      14   0 cpuhp/0
      0     0.00       0.00       0.00      15   0 cpuhp/1
      0     0.00       2.80       0.01     327 327 cron

To print the help information, run the following command:

PS /root> help

You can also use the Get-Help command to get help on your desired command.

For example, to get help on the Set-Location command use the following command:

PS /root> Get-Help Set-Location

If you want to remove the PowerShell, run the following command:

apt-get remove powershell -y

Conclusion

In the above guide, you learned how to install and use the PowerShell on Ubuntu 20.04. You can now write the PowerShell script to automate your day-to-day administration tasks. Feel free to ask me if you have any questions.

Share this page:

Suggested articles

0 Comment(s)

Add comment