How to Install PowerShell on Ubuntu 16.04 LTS

To Install PowerShell on Ubuntu 16.04 LTS

PowerShell is a cross-platform automation and configuration tool/framework that works well with your existing tools and is optimized for dealing with structured data such as JSON, CSV, XML, etc and also on REST APIs, and object models. It includes a command-line shell, an associated scripting language and a framework for processing camlet. It is really so simple to install PowerShell on Ubuntu and this tutorial covers the ground on the same process. 

 

Installing PowerShell

First, install PowerShell using official Ubuntu Microsoft Repository in the following manner.

root@linuxhelp:~# curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    79  100    79    0     0     39      0  0:00:02  0:00:01  0:00:01    39
deb [arch=amd64] https://packages.microsoft.com/ubuntu/16.04/prod xenial main
And then, make sure you update repositories with the help of the following command. 
root@linuxhelp:~# apt-get update 
Hit:1 http://in.archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu xenial-updates InRelease      
.
.
Reading package lists... Done


Now, you can complete the PowerShell installation by making use of the following command. 

root@linuxhelp:~# apt-get install powershell
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
.
.
Setting up powershell (6.0.1-1.ubuntu.16.04) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...

 

 

After the installation, you can run the PowerShell, by just issuing this command “ pwsh” in the prompt:

root@linuxhelp:~# pwsh
PowerShell v6.0.1
Copyright (c) Microsoft Corporation. All rights reserved.

 

You have installed PowerShell, Let' s see some of its allied commands. If you want to create a directory, you need to run the following.

PS /home/user1>  md linuxhelp    
PS /home/user1>  ls                                                                                                                   
Desktop  Documents  Downloads  examples.desktop  linuxhelp  Music  Pictures  Public  Templates    Videos

 

 

If you want to get information on the current working directory, you can run the following command. 

 PS /home/user1>  pwd 
  Path       
----       
/home/user1

The following command can be used to get the process that is running on the local computer.

PS /home/user1>  ps                                                                                                                   
   PID TTY          TIME CMD
  4848 pts/2    00:00:00 sudo
  4849 pts/2    00:00:00 bash
  5909 pts/2    00:00:01 pwsh
  5972 pts/2    00:00:00 ps

 

You can also easily remove the directory in the following manner.

PS /home/user1>  rd ./linuxhelp/   
PS /home/user1>  ls                                                                                                                   
Desktop  Documents  Downloads  examples.desktop  Music    Pictures  Public  Templates  Videos

 

 

If you want to check the list of commands run during the process, run the following.

PS /home/user1>  h                                                                                                                    
  Id CommandLine                                                                                                                    
  -- -----------                                                                                                                    
   1 md linuxhelp                                                                                                                   
   2 ls                                                                                                                             
   3 pwd                                                                                                                            
   4 ps                                                                                                                             
   5 rd ./linuxhelp/                                                                                                                
   6 ls                

 

You can also do it by making use of the history command.

PS /home/user1>  history                                                                                                              

  Id CommandLine                                                                                                                    
  -- -----------                                                                                                                    
   1 md linuxhelp                                                                                                                   
   2 ls                                                                                                                             
   3 pwd                                                                                                                            
   4 ps                                                                                                                             
   5 rd ./linuxhelp/                                                                                                                
   6 ls                                                                                                                             
   7. h     

 

You can create a new item by making use of the ni command.

PS /home/user1>  ni linuxhelp                                                                                                         

    Directory: /home/user1

Mode                LastWriteTime         Length Name                                                                               
----                -------------         ------ ----                                                                               
------         07/02/18   6:20 PM              0 linuxhelp    

 

 

 

A new item has been created, you can use the move command to move from one location to another.                                                                                                                                            

PS /home/user1>  move ./linuxhelp /home/user1/Desktop/                                                                                

PS /home/user1>  ls                                                                                                                  

Desktop  Documents  Downloads  examples.desktop  Music       Pictures  Public  Templates  Videos

 

 

 

If you want to check the properties of the file, make sure you run the following command.

PS /home/user1/Desktop>  gp linuxhelp                                                                                                



    Directory: /home/user1/Desktop



Mode                LastWriteTime         Length Name                                                                              

----                -------------         ------ ----                                                                               

------         07/02/18   6:20 PM              0 linuxhelp               

                                                                                                                   

 

You can exit from this, by making use of the following command.

PS /home/user1/Desktop>  exit

 

With this, the installation and usage of PowerShell come to an end.

FAQ
Q
Use an example to show how you can map a network drive in PowerShell?
A
In PowerShell for mapping a network drive, you have to use the command like:

# PowerShell Map Network Drive
$Net = $(New – Object – ComObject Wscript.Network)
$Net.MapNetworkDrive(“F:”, \\Escobar\Narco
Q
Which are the different types of comparison operators in PowerShell?
A
There are four types of comparison operators in PowerShell:

1. Equality
2. Match
3. Containment
4. Replace
Q
Describe in detail about the importance of brackets in PowerShell?
A
he three types of bracket and their significance is described below:

Braces Brackets {}: Curly brackets are used in blocked statements

Parenthesis Brackets (): Curved parenthesis brackets are utilized for compulsory arguments.
Q
What is MS PowerShell?
A
Developed by Microsoft, Windows PowerShell is a shell that performs task management and configuration management. It is based on the .NET framework and comprises of scripting language and a command line shell.
Q
What is the use of Windows PowerShell?
A
he primary function of PowerShell is to apply changes to any system without any use of Graphical User Interface. One can run automation directly from the script while using Windows PowerShell. There is no need to remember your sequence of point & click for creating the desired state. PowerShell also allows you to perform repetitive tasks in a better way by writing scripts and combining multiple commands together.