Installing Microsoft Powershell on Ubuntu 18.04

Reading Time: 5 minutes

If you are a Windows administrator who has recently been tasked with administering a Linux-based Ubuntu server, you may find that utilizing Microsoft Powershell may help ease the transition into Linux, and allow you to be more productive. If you are a Linux administrator who is interested in exploring the options that Powershell provides, then this tutorial is for you as well.

What is Powershell?

Powershell is a .net-based configuration management and automation tool initially developed by Microsoft for the Windows operating system. In August 2018, Microsoft open-sourced this software, and with its cross-platform ability, paved the way for its availability on Linux.

Powershell on Linux allows you to combine Powershell language as well as all of your usual Linux and GNU shell tools as well as a solid framework for processing cmdlets

Prerequisites

Before you can install Powershell there are a few things you need to do.

  • Make sure you have access to the server as the root user
  • You will also need to utilize the ‘wget’ tool. If you need to install this tool.

Installation

Step 1: Retrieve the Microsoft Repository GPG Keys

GPG is a cryptographic system that allows users to exchange encrypted emails or other information securely. Another feature of GPG is its ability to “cryptographically sign” a file. When a file is signed in this manner using GPG, the author can distribute that file and an associated signature key. You can then use the GPG key to verify that no one has tampered with the data within the file before downloading them. In our case, this process is used to ensure no one has modified our Powershell with malware or other malicious software. To retrieve the keys, we will run the following command.

root@host [~]# cd /usr/src/
root@host:/usr/src# wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

Step 2: Install the Microsoft GPG Keys

Next, we will use the dpkg command to manually install the Debian package that was downloaded in Step 1.

root@host:/usr/src# sudo dpkg -i packages-microsoft-prod.deb
Selecting previously unselected package packages-microsoft-prod.
(Reading database ... 257148 files and directories currently installed.)
Preparing to unpack packages-microsoft-prod.deb ...
Unpacking packages-microsoft-prod (1.0-ubuntu18.04.2) ...
Setting up packages-microsoft-prod (1.0-ubuntu18.04.2) ...
root@host:/usr/src#

Step 3: Update the Package List for Apt

Now, we will update our repository list to ensure we have the latest list of packages available.

root@host:/usr/src# apt-get update
Hit:1 http://us.archive.ubuntu.com/ubuntu eoan InRelease                                                                        
Hit:2 http://archive.canonical.com/ubuntu eoan InRelease                                                                        
Get:3 http://security.ubuntu.com/ubuntu eoan-security InRelease [97.5 kB]                                                       
Get:4 http://us.archive.ubuntu.com/ubuntu eoan-updates InRelease [97.5 kB]                                                                                                          
Get:5 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease [4,002 B]                                                                                              
Hit:6 http://archive.ubuntu.com/ubuntu eoan InRelease                                                                      
Get:7 http://us.archive.ubuntu.com/ubuntu eoan-backports InRelease [88.8 kB] 
...
...
...
Reading package lists... Done  
root@host:/usr/src# 

Step 4: Enable the “universe” repositories

Ubuntu software can be installed from multiple software repositories. To install Powershell, we must enable the universe repository.

add-apt-repository universe

Step 5: Install Powershell

You are now ready to run the command to install Powershell. The ‘-y’ flag simply answers ‘yes’ to all the questions that apt-get typically asks during the installation.

root@host:/usr/src# apt-get install -y powershell
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  liblttng-ust-ctl4 liblttng-ust0 liburcu6
The following NEW packages will be installed:
  liblttng-ust-ctl4 liblttng-ust0 liburcu6 powershell
0 upgraded, 4 newly installed, 0 to remove and 42 not upgraded.
Need to get 57.7 MB of archives.
After this operation, 158 MB of additional disk space will be used.
root@host:/usr/src#

Step 6: Run Powershell

Powershell is now installed. You can run it from the command line with the ‘pwsh’ command.

root@host:/usr/src# pwsh
PowerShell 6.2.4
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /usr/src>

You will notice that the command prompt has changed.

root@host:/usr/src#

to

PS /usr/src>

Typing in the ‘dir‘ command provides us with the following output.

PS /usr/src> dir


    Directory: /usr/src

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----            1/8/20  6:51 AM                linux-headers-5.3.0-26
d-----            1/8/20  6:51 AM                linux-headers-5.3.0-26-generic
d-----           1/29/20  6:57 AM                linux-headers-5.3.0-29
d-----           1/29/20  6:57 AM                linux-headers-5.3.0-29-generic
d-----          10/29/19  8:17 AM                nvidia-430.50
d-----           1/11/19  3:19 PM                packages
d-----            1/8/20 11:26 AM                virtualbox-6.0.14
------            1/2/19  6:49 PM           3132 packages-microsoft-prod.deb

PS /usr/src>

Uninstalling Powershell

If ever the time comes that you no longer require the Powershell software, it can be uninstalled with the following command.

PS /usr/src> apt-get remove powershell
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  docutils-common liblttng-ust-ctl4 liblttng-ust0 liburcu6 mypy pylint python3-astroid python3-asttokens python3-bs4
  python3-docutils python3-html5lib python3-isort python3-jedi python3-lazy-object-proxy python3-logilab-common python3-lxml
  python3-mccabe python3-mypy python3-mypy-extensions python3-parso python3-psutil python3-pygments python3-pyperclip
  python3-roman python3-serial python3-soupsieve python3-typed-ast python3-typing-extensions python3-webencodings python3-wrapt
  sgml-base xml-core xsel
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
  powershell
0 upgraded, 0 newly installed, 1 to remove and 42 not upgraded.
After this operation, 157 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 257789 files and directories currently installed.)
Removing powershell (6.2.4-1.ubuntu.18.04) ...
Processing triggers for man-db (2.8.7-3) ...
PS /usr/src> exit
root@host:/usr/src#

Install Powershell Using Snap

The PowerShell Core for Linux is also published in the Snap store for simple installation and continued updates

Install using Snap

root@host:/usr/src# snap install powershell --classic
powershell 6.2.4 from Microsoft PowerShell✓ installed
root@host:/usr/src#
root@host:/usr/src# powershell
PowerShell 6.2.4
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS /usr/src> 
PS /usr/src> help

TOPIC
    PowerShell Help System

SHORT DESCRIPTION
    Displays help about PowerShell cmdlets and concepts. 

LONG DESCRIPTION
    PowerShell Help describes PowerShell cmdlets,
    functions, scripts, and modules, and explains concepts, including
    the elements of the PowerShell language.

    PowerShell does not include help files, but you can read the
    help topics online, or use the Update-Help cmdlet to download help files
    to your computer and then use the Get-Help cmdlet to display the help
    topics at the command line.

    You can also use the Update-Help cmdlet to download updated help files
    as they are released so that your local help content is never obsolete. 

    Without help files, Get-Help displays auto-generated help for cmdlets, 
    functions, and scripts.


  ONLINE HELP    
    You can find help for PowerShell online in the TechNet Library
    beginning at http://go.microsoft.com/fwlink/?LinkID=108518. 

<Type 'q' to exit.>

PS /usr/src> exit
root@host:/usr/src#

After the installation, Snap will be automatically upgraded. You may also prompt a manual upgrade using the following command.

PS /usr/src>snap refresh powershell 
snap "powershell" has no updates available
PS /usr/src>

After the installation, Snap will automatically upgrade. You may also prompt a manual upgrade using the following command.

Uninstall Using Snap

To remove Powershell using snap, run this command.

root@host:/usr/src# snap remove powershell

Conclusion

All in all, Powershell is a valuable adjunct to your existing tool set. It allows us to maintain and operate computers via the command line, access data stores, and easily modify the file system. Its mature scripting language allows for further extensions to its overall functionality. Because it is open-sourced software, it can be easily forked on GitHub.

Click Below To Save $50.00!!!

Would you like to save a quick $50.00 on your next purchase, upgrade or monthly payment? Liquid Web’s Refer-A-Friend program is an excellent way to earn extra cash to put towards the items, services or extra’s you have been looking at. Simply click on the coupon link below to add your name and email address and that’s it! You’re in!

To find out more, give us a call at today 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions or Experienced Hosting advisors to learn how you can take advantage of this program right now!

Refer a friend and get hosting credit!