PowerShell is a versatile task-based configuration management tool initially developed for the Windows operating system. Microsoft has expanded its reach with PowerShell Core, making it available for Linux environments. With PowerShell, you can automate and manage administrative tasks on both local and remote computers.
In this guide, we’ll walk you through the process of installing PowerShell on Ubuntu 20.04.
Prerequisites
- A server running Ubuntu 20.04.
- The root password configured on the server.
Getting Started
Before we begin, it’s important to update your system packages to their latest versions. Execute the following command to update them:
apt-get update -y
After the system is updated, proceed to the installation steps below.
Install PowerShell Using Snap
There are two methods to install PowerShell on Ubuntu 20.04. We will begin by demonstrating how to install it using Snap.
First, install the Snap package manager with this command:
apt-get install snap snapd -y
With Snap installed, you can now install PowerShell by running:
snap install powershell --classic
To start using PowerShell, enter the following command:
pwsh
Upon successful installation, you will see the following prompt:
PowerShell 7.1.2 Copyright (c) Microsoft Corporation. https://aka.ms/powershell Type 'help' to get help. PS /root>
To exit PowerShell, use this command:
PS /root> exit
To remove PowerShell from your system, execute:
snap remove powershell
Install PowerShell from Ubuntu Repository
Alternatively, you can install PowerShell directly from the Ubuntu repository. Begin by downloading the repository package using:
wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
After downloading, install the package with the following command:
dpkg -i packages-microsoft-prod.deb
Next, update your repository and install PowerShell by executing:
apt-get update -y apt-get install powershell -y
To initiate PowerShell, type:
pwsh
Upon starting, you should see:
PowerShell 7.1.2 Copyright (c) Microsoft Corporation. https://aka.ms/powershell Type 'help' to get help. PS /root>
How to Use PowerShell
Explore using PowerShell’s command-line interface on Linux by trying the following commands:
List directory contents with:
PS /root> dir
The expected output is:
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
Get detailed PowerShell host information with:
PS /root> Get-Host
The output should look like this:
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 view command history, use:
PS /root> Get-History
Expected 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 list running processes, enter:
PS /root> Get-Process
This will display:
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
For help information, type:
PS /root> help
To get detailed help on specific commands, use Get-Help:
PS /root> Get-Help Set-Location
Conclusion
This guide covered the installation and basic usage of PowerShell on Ubuntu 20.04. You can now develop PowerShell scripts to streamline and automate daily administrative tasks. If you have any questions, feel free to reach out for more information.
FAQ
Q: Can PowerShell be used to manage both Windows and Linux systems?
A: Yes, PowerShell can be used for administrative tasks on both Windows and Linux systems, providing a versatile environment for cross-platform management.
Q: What are the prerequisites for installing PowerShell on Ubuntu 20.04?
A: A server running Ubuntu 20.04 and administrative access to that server.
Q: How do I uninstall PowerShell from my system?
A: If installed via Snap, run snap remove powershell
. If installed via the repository, use apt-get remove powershell -y
to uninstall.
Q: Where can I find more information about using PowerShell commands?
A: You can use help
within PowerShell or Get-Help <command>
for specific command help to explore more about PowerShell commands.