Cyberithub

How to Install PowerShell on Ubuntu or Debian

Advertisements

In this article, we will see how to install powershell on Ubuntu or Debian based linux systems. There are many reasons why one could think of installing powershell on a linux based systems. The first and foremost reason is that any windows users who is accustomed of using powershell migrating to ubuntu or debian based linux systems might find lot of new terminal commands to learn to perform various tasks. Rather than learning new commands, it will be much easy for them to install and work with powershell on a linux system.

With the introduction of powershell core, now powershell scripts developed on Windows can be easily run on Linux and Unix based systems and vice versa. It has also numerous other robust features that makes it suitable to be used on a Ubuntu or Debian based systems. Here we will see the steps to install PowerShell on a Ubuntu or Debian Linux.

 

What is PowerShell

PowerShell is a task-based command-line shell and scripting language designed especially for system administration. It is built over .NET framework to helps Windows professionals and power users automate the administration of the Windows operating system and applications that run on Windows Server environments. Later on, it is also available to be used on Linux and Unix systems.

 

Key Features

  • Cmdlets: These are built-in commands in PowerShell, designed to perform specific functions like managing services, processes, and event logs, or manipulating objects in technologies like Active Directory or Windows Management Instrumentation (WMI).
  • Scripting Language: PowerShell includes a rich scripting language that allows complex automation scripts to be created, which are more powerful than batch scripts. It leverages the .NET framework, providing access to a vast range of functionalities.
  • Pipeline: PowerShell uses a pipeline mechanism that allows the output of one cmdlet to be piped as input to another cmdlet, enabling complex operations to be performed in a single command.
  • Objects: Unlike traditional command-line interfaces that output text, PowerShell cmdlets and scripts work with and return .NET objects. This allows for a more detailed and structured way to manipulate data and system components.
  • Remote Management: PowerShell supports robust remote management capabilities, enabling scripts and cmdlets to be run on remote computers or sessions.
  • Extensibility: Users can create custom cmdlets, functions, modules, and scripts to extend PowerShell's capabilities, making it highly adaptable to specific needs.
  • Integrated Scripting Environment (ISE): PowerShell ISE is a graphical host application that provides a multi-tabbed environment for running interactive commands and developing, testing, and debugging scripts.
  • Cross-Platform: With the introduction of PowerShell Core (denoted by the command pwsh), PowerShell is now available on multiple platforms, including Windows, Linux, and macOS, facilitating cross-platform management and automation.

 

How to Install PowerShell on Ubuntu or Debian

How to Install PowerShell on Ubuntu or Debian

Also Read: How to Install Minecraft on Linux [Easy Steps]

Step 1: Prerequisites

a) You should have a running Ubuntu or Debian Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get and dpkg utility available in your System.

d) You should also have wget utility available in your Server.

 

Step 2: Update Your Server

As a standard practice, before installing any new packages it is mandatory to check and install all the latest upgrade using sudo apt update && sudo apt upgrade command as shown below. This will keep the system updated with latest available security patches and bugfixes.

cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade
[sudo] password for cyberithub:
Hit:1 http://in.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease
Get:4 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Hit:5 https://aquasecurity.github.io/trivy-repo/deb jammy InRelease
Hit:6 http://in.archive.ubuntu.com/ubuntu jammy-backports InRelease
Fetched 119 kB in 4s (27.0 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
94 packages can be upgraded. Run 'apt list --upgradable' to see them.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed:
linux-headers-6.5.0-25-generic linux-hwe-6.5-headers-6.5.0-25 linux-image-6.5.0-25-generic linux-modules-6.5.0-25-generic
linux-modules-extra-6.5.0-25-generic ubuntu-pro-client
The following packages have been kept back:
libldap-2.5-0 libldap-common
The following packages will be upgraded:
accountsservice alsa-ucm-conf base-files bind9-dnsutils bind9-host bind9-libs binutils binutils-common binutils-x86-64-linux-gnu coreutils cups cups-bsd
cups-client cups-common cups-core-drivers cups-daemon cups-ipp-utils cups-ppdc cups-server-common dns-root-data dnsmasq-base dpkg dpkg-dev
firmware-sof-signed gdm3 gir1.2-accountsservice-1.0 gir1.2-gdm-1.0 gjs iptables language-pack-en language-pack-en-base language-pack-gnome-en
.................................................................

 

 

Step 3: Install PowerShell

PowerShell can be installed using two different methods. Here we will look into both the methods. You can choose to install using any of the below methods depending on your needs and requirements.

a) Using repo

First get the version of Ubuntu or Debian by running source /etc/os-release command as shown below.

cyberithub@ubuntu:~$ source /etc/os-release

Then you have to download the Microsoft repository GPG Keys using wget utility as shown below. Since I am using Ubuntu based system so below command would work fine for me. But if you are using Debian based systems then you have to run wget -q https://packages.microsoft.com/config/debian/$VERSION_ID/packages-microsoft-prod.deb command.

cyberithub@ubuntu:~$ wget -q https://packages.microsoft.com/config/ubuntu/$VERSION_ID/packages-microsoft-prod.deb

After downloading the debian package, you have to install it by using sudo dpkg -i packages-microsoft-prod.deb command.

cyberithub@ubuntu:~$ sudo dpkg -i packages-microsoft-prod.deb
Selecting previously unselected package packages-microsoft-prod.
(Reading database ... 243993 files and directories currently installed.)
Preparing to unpack packages-microsoft-prod.deb ...
Unpacking packages-microsoft-prod (1.0-ubuntu22.04.1) ...
Setting up packages-microsoft-prod (1.0-ubuntu22.04.1) ...

Now that packages.microsoft.com is added, update the list of packages by running sudo apt-get update command as shown below.

cyberithub@ubuntu:~$ sudo apt-get update
Get:1 https://packages.microsoft.com/ubuntu/22.04/prod jammy InRelease [3,632 B]
Hit:2 https://aquasecurity.github.io/trivy-repo/deb jammy InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu jammy InRelease
Get:4 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:6 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main all Packages [1,035 B]
Get:7 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 Packages [136 kB]
Get:8 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main arm64 Packages [34.4 kB]
Get:9 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main armhf Packages [12.2 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu jammy-backports InRelease [109 kB]
Get:11 http://security.ubuntu.com/ubuntu jammy-security/main i386 Packages [421 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1,462 kB]
...................................................

Finally, install the package from repo by using sudo apt-get install -y powershell command as shown below.

cyberithub@ubuntu:~$ sudo apt-get install -y powershell
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
powershell
0 upgraded, 1 newly installed, 0 to remove and 94 not upgraded.
Need to get 71.0 MB of archives.
After this operation, 181 MB of additional disk space will be used.
Get:1 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 powershell amd64 7.4.1-1.deb [71.0 MB]
Fetched 71.0 MB in 23s (3,094 kB/s)
Selecting previously unselected package powershell.
(Reading database ... 244001 files and directories currently installed.)
Preparing to unpack .../powershell_7.4.1-1.deb_amd64.deb ...
Unpacking powershell (7.4.1-1.deb) ...
Setting up powershell (7.4.1-1.deb) ...
Processing triggers for man-db (2.10.2-1) ...

b) Using deb package

You have to first download the latest available PowerShell debian package from GitHub using wget utility as shown below.

cyberithub@ubuntu:~$ wget https://github.com/PowerShell/PowerShell/releases/download/v7.4.1/powershell_7.4.1-1.deb_amd64.deb
--2024-03-16 21:06:58-- https://github.com/PowerShell/PowerShell/releases/download/v7.4.1/powershell_7.4.1-1.deb_amd64.deb
Resolving github.com (github.com)... 20.207.73.82
Connecting to github.com (github.com)|20.207.73.82|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/49609581/57ea5605-d2ac-40cd-9ed8-0a1f9e7d1ecc?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20240316%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240316T153658Z&X-Amz-Expires=300&X-Amz-Signature=46b469dcedb9aea8bd70e4edbe4b78ce54b63ab1c78bc5a321a872f46791869c&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=49609581&response-content-disposition=attachment%3B%20filename%3Dpowershell_7.4.1-1.deb_amd64.deb&response-content-type=application%2Foctet-stream [following]
--2024-03-16 21:06:58-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/49609581/57ea5605-d2ac-40cd-9ed8-0a1f9e7d1ecc?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20240316%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240316T153658Z&X-Amz-Expires=300&X-Amz-Signature=46b469dcedb9aea8bd70e4edbe4b78ce54b63ab1c78bc5a321a872f46791869c&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=49609581&response-content-disposition=attachment%3B%20filename%3Dpowershell_7.4.1-1.deb_amd64.deb&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.111.133, 185.199.110.133, 185.199.108.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 71016830 (68M) [application/octet-stream]
Saving to: ‘powershell_7.4.1-1.deb_amd64.deb’

powershell_7.4.1-1.deb_amd64.deb 100%[============================================================================>] 67.73M 3.95MB/s in 15s

2024-03-16 21:07:15 (4.41 MB/s) - ‘powershell_7.4.1-1.deb_amd64.deb’ saved [71016830/71016830]

Then install the package by using sudo dpkg -i powershell_7.4.1-1.deb_amd64.deb command as shown below.

cyberithub@ubuntu:~$ sudo dpkg -i powershell_7.4.1-1.deb_amd64.deb
[sudo] password for cyberithub:
Selecting previously unselected package powershell.
(Reading database ... 243987 files and directories currently installed.)
Preparing to unpack powershell_7.4.1-1.deb_amd64.deb ...
Unpacking powershell (7.4.1-1.deb) ...
Setting up powershell (7.4.1-1.deb) ...
Processing triggers for man-db (2.10.2-1) ...

If required, you can also use sudo apt-get install -f command to resolve all missing dependencies and complete the installation.

cyberithub@ubuntu:~$ sudo apt-get install -f
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded.

 

 

Step 4: Check Version

To check the current installed version, run pwsh --version command as shown below.

cyberithub@ubuntu:~$ pwsh --version
PowerShell 7.4.1

 

 

Step 5: Start PowerShell

To launch a session of PowerShell from terminal, run pwsh command as shown below. It will open a new session of PowerShell Core, allowing you to start executing PowerShell commands and scripts.

cyberithub@ubuntu:~$ pwsh
PowerShell 7.4.1
PS /home/cyberithub>

 

 

Step 6: List all running processes

To list all running processes in your system, run Get-Process cmdlet as shown below. This cmdlet allows us to see detailed information about process attributes such as process name, ID, CPU usage, memory usage, and more.

PS /home/cyberithub> Get-Process

NPM(K)    PM(M)    WS(M)   CPU(s)      Id   SI    ProcessName
------    -----    -----   ------      --   --    -----------
     0     0.00     5.88     0.00    2334   …32   (sd-pam)
     0     0.00     7.70     0.21     575   575   accounts-daemon
     0     0.00     1.88     0.01     576   576   acpid
     0     0.00     0.00     0.00      45     0   acpi_thermal_pm
     0     0.00    78.11     0.83    4063   …39   apport-gtk
     0     0.00     7.75     0.02    3354   …39   at-spi-bus-launcher
     0     0.00     7.62     0.02    3485   …39   at-spi2-registryd
..........................................

 

 

Step 7: Check all the available options

To check all the options available with pwsh utility, run pwsh --help command as shown below.

cyberithub@ubuntu:~$ pwsh --help

Usage: pwsh[.exe] [-Login] [[-File] <filePath> [args]]
                  [-Command { - | <script-block> [-args <arg-array>]
                                | <string> [<CommandParameters>] } ]
                  [-CommandWithArgs <string> [<CommandParameters>]
                  [-ConfigurationName <string>] [-ConfigurationFile <filePath>]
                  [-CustomPipeName <string>] [-EncodedCommand <Base64EncodedCommand>]
                  [-ExecutionPolicy <ExecutionPolicy>] [-InputFormat {Text | XML}]
                  [-Interactive] [-MTA] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile]
                  [-NoProfileLoadTime] [-OutputFormat {Text | XML}]
                  [-SettingsFile <filePath>] [-SSHServerMode] [-STA]
                  [-Version] [-WindowStyle <style>]
                  [-WorkingDirectory <directoryPath>]

        pwsh[.exe] -h | -Help | -? | /?
................................................

 

 

Step 8: Uninstall PowerShell

In case you don't need PowerShell anymore in your system then you can remove it by using sudo apt remove powershell command as shown below.

cyberithub@ubuntu:~$ sudo apt remove powershell
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
powershell
0 upgraded, 0 newly installed, 1 to remove and 11 not upgraded.
After this operation, 181 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 244667 files and directories currently installed.)
Removing powershell (7.4.1-1.deb) ...
Processing triggers for man-db (2.10.2-1) ...

Leave a Comment