+3 votes
224 views
How to install PowerShell on Ubuntu

in Linux / Unix by (551k points)
reopened | 224 views

1 Answer

+4 votes
Best answer

1. Install Snap on Ubuntu Linux
2. Install PowerShell with Snap on Ubuntu Linux
3. Run PowerShell on Linux

PowerShell is the administration console developed by Microsoft with which a centralized administration of all aspects of the system is sought both at hardware and software level. Now, in January of this year, Microsoft has released PowerShell Core 6.0 defined for the Linux, Windows and macOS operating systems and in addition to a Docker container which expands the possibilities of managing many more commands and control options in the system..

Linux distributions
This version of PowerShell for Linux is available for the following Linux distributions:
  • Ubuntu 14.04
  • Ubuntu 16.04
  • Ubuntu 17.10
  • Ubuntu 18.04
  • Debian 8
  • Debian 9
  • CentOS 7
  • Red Hat Enterprise Linux (RHEL) 7
  • OpenSUSE 42.3
  • Fedora 27
  • Fedora 28
  • Linux arch
  • Kali
What's new PowerShell 6.0
Among the novelties of PowerShell 6.0 we find:
  • It is possible to use the os_log APIs on Mac and Syslog on Linux.
  • There have been changes in the versions of macOS and Linux in order to support characters in file names that were not supported in Windows
  • Support for Docker has been added.
  • The powershell executable name (.exe) has been edited to pwsh (.exe).
  • Case sensitivity has been standardized in both macOS and Linux.
  • Support for side-by-side installations has been included, making it easier to validate new versions of PowerShell and migrate existing scripts.
  • Backward compatibility with Windows PowerShell.
  • The PSRP (PowerShell Remoting Protocol) protocol is compatible and works with SSH.
  • Character encoding in UTF-8 by default without using Byte ***** Mark.

1. Install Snap on Ubuntu Linux


Before executing the PowerShell installation process, it will be necessary to install Snap, to understand a little about this, Snap is a squashFS file system in which the application code and a snap.yaml file are housed. It contains specific metadata, has a read-only file system and, once installed, a writing area.

Snap groups most of the necessary libraries and runtimes and can be updated and reversed without affecting the distribution used, which is ideal for use without impairing the optimal operation of the system. Snap can exchange content and features with other snapshots based on detailed policies that are controlled by the user and the operating system defaults.

To install Snap, in this case on Ubuntu 18.04, we will run the following:

 sudo apt install snap 

image


2. Install PowerShell with Snap on Ubuntu Linux

Step 1

Once Snap is installed, we can go to the Ubuntu Software center and from there look for PowerShell, we will get the following results: image
Step 2

There we have the option of installing a stable version of PowerShell or a previous version which, as the name implies, is unstable. If we want to use this method to install PowerShell, just click on the desired option and in the pop-up window select “Install”: image
Step 3

Now, if we want to perform the installation process from the terminal, we can execute the following command as necessary:
For Ubuntu and Debian
 Stable version: sudo apt-get install -y powershell Previous version: sudo apt-get install -y powershell-preview 
For CentOS and RedHat
 Stable version: sudo yum install -y powershell Previous version: sudo yum install -y powershell-preview 
For OpenSUSE
 Stable version: sudo zypper install powershell Previous version: sudo zypper install powershell-preview 
For Fedora
 Stable version: sudo dnf install -y powershell Previous version: sudo dnf install -y powershell-preview 
Step 4

In this case we can use the Ubuntu software center and clicking on Install will need to enter the administrator password: image
Step 5

Click on "Authenticate" and the PowerShell installation process will begin: image
Step 5

In the case of Ubuntu 18 we can install PowerShell using the following commands:
Import GPG Repository Keys
Ubuntu Microsoft Repository Registration
 sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/18.04/prod.list 
Product list update
 sudo apt-get update 
PowerShell installation
 sudo apt-get install -y powershell-preview 
PowerShell Execution
 pwsh-preview 
Step 6

Once the installation process is finished we will see the following: image

3. Run PowerShell on Linux

Step 1

Now, we can launch PowerShell from the "Start" button or execute the following command in the terminal:
 pwsh 

image

Step 2

To obtain a general list of the commands available for use, we will execute the following cmdlet:
 Get-command 
image
Step 3

Now it will be possible to make use of these cmdlets to obtain general information in the system: image
Step 4

PowerShell on Linux is intended for use with Azure, but we can see that it is another alternative for general system administration. The PowerShell administration paths in Linux are:
  • $ PSHOME is /opt/microsoft/powershell/6.0.2/
  • User profiles will be read from ~ / .config / powershell / profile.ps1
  • The default profiles will be read from $ PSHOME / profile.ps1
  • User modules will be read from ~ / .local / share / powershell / Modules
  • Shared modules will be read in / usr / local / share / powershell / Modules
  • The default modules will be read from $ PSHOME / Modules
  • PSReadline history will be recorded in
 ~ / .local / share / powershell / PSReadLine / ConsoleHost_history.txt 
We have seen how PowerShell, being exclusive to Microsoft, can be used in Linux for centralized management purposes and in this way extend the administration capabilities that we can carry out in each compatible distribution.

Let's take advantage of what each PowerShell cmdlet gives us for maximum benefit..


by (3.5m points)
edited

Related questions

+4 votes
1 answer
+5 votes
1 answer
asked May 8, 2021 in Linux / Unix by backtothefuture (551k points) | 319 views
+3 votes
1 answer
asked May 8, 2021 in Linux / Unix by backtothefuture (551k points) | 337 views
+5 votes
1 answer
asked May 6, 2021 in Linux / Unix by backtothefuture (551k points) | 320 views
+4 votes
1 answer
asked May 6, 2021 in Linux / Unix by backtothefuture (551k points) | 392 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,644 questions
10,776 answers
510 comments
3 users