PowerShell is a distributed, scalable, heterogeneous configuration, and automation framework, consisting of an interactive command-line shell and scripting language, for Windows operating system. It is built on .NET framework, and It allows the users to automate and simplify the system tasks. For more details about PowerShell, refer the following link.
In this brief tutorial, let us see how to install PowerShell in Ubuntu 14.04/16.04/18.04 LTS and CentOS 7 64-bit server editions.
Table of Contents
Install Windows PowerShell Core 6.0 in Linux
PowerShell can be installed on many popular Linux distributions including Arch Linux, Debian, Ubuntu, Fedora, CentOS, SUSE. Here I have included installation instructions for Debian, Ubuntu and CentOS.
On Ubuntu 14.04 LTS:
Download and register PowerShell Repository GPG key:
$ wget -q https://packages.microsoft.com/config/ubuntu/14.04/packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
Update the software sources list:
$ sudo apt-get update
Then, install PowerShell using command:
$ sudo apt-get install -y powershell
On Ubuntu 16.04 LTS:
Add PowerShell Repository GPG key:
$ wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
Update the software sources list:
$ sudo apt-get update
Then, install PowerShell using command:
$ sudo apt-get install -y powershell
On Ubuntu 18.04 LTS:
Register PowerShell repository GPG key:
$ wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
Update repository lists and install PowerShell:
$ sudo apt-get update
$ sudo apt-get install -y powershell
On Debian 8:
$ sudo apt-get install curl apt-transport-https
$ curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
$ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-jessie-prod jessie main" > /etc/apt/sources.list.d/microsoft.list'
$ sudo apt-get update
$ sudo apt-get install -y powershell
On Debian 9:
$ sudo apt-get install curl gnupg apt-transport-https
$ curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
$ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list'
$ sudo apt-get update
$ sudo apt-get install -y powershell
On CentOS 7:
Add PowerShell repository as root user:
# curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
Install PowerShell:
# yum install -y powershell
For other distributions, please refer the official installation instructions.
We have now installed PowerShell. Next, we will see how to use it in real time.
Getting started with PowerShell
Please note that PowerShell for Linux is still in development stage, so you encounter with some bugs. If there are any bugs, join the PowerShell community blog (The link is given at the end of this article) and get help.
Once you installed PowerShell, run the following command to enter to the PowerShell console/session.
pwsh
This is how PowerShell console looks like in my Ubuntu 18.04 LTS server.
PowerShell 6.1.2 Copyright (c) Microsoft Corporation. All rights reserved. https://aka.ms/pscore6-docs Type 'help' to get help. PS /home/sk>
In PowerShell session, we mention the powershell commands as cmdlets, and we mention PowerShell prompt sign as PS />.
Working in PowerShell is almost similar to BASH. I ran some Linux commands in PowerShell. It seems almost all Linux commands works in the PowerShell. Also, PowerShell has its own set of commands (cmdlets). The TAB function (autocomplete) feature works as like in BASH.
Clear? Well, Let us few examples.
View PowerShell version
To view the version of the PowerShell, enter:
$PSVersionTable
Sample output:
Name Value ---- ----- PSVersion 6.1.2 PSEdition Core GitCommitId 6.1.2 OS Linux 4.15.0-45-generic #48-Ubuntu SMP Tue Jan ... Platform Unix PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0
As you see in the above output, the version of the PowerShell is 6.1.2.
Creating files
To create a new file, use 'New-Item' command as shown below.
New-Item ostechnix.txt
Sample output:
Directory: /home/sk Mode LastWriteTime Length Name ---- ------------- ------ ---- ------ 2/11/19 10:28 AM 0 ostechnix.txt
or simply use ">" as shown below below:
"" > ostechnix.txt
Here, "" - describes that the file is empty. ostechnix.txt is the filename.
To append some contents in the file, run the following command:
Set-Content ostechnix.txt -Value "Welcome to OSTechNix blog!"
Or
"Welcome to OSTechNix blog!" > ostechnix.txt
Viewing the content of a file
We have created some files from the PowerShell. How do we view the contents of that files? That's easy.
Simply use 'Get-Content' command to display the contents of any file.
Get-Content <Path> <filename>
Example:
Get-Content ostechnix.txt
Sample output:
Welcome to OSTechNix blog!
Deleting files
To delete a file or item, use 'Remove-Item' command as shown below.
Remove-Item ostechnix.txt
Let us verify whether the item has really been deleted using command:
Get-Content ostechnix.txt
You should see an output like below.
Get-Content : Cannot find path '/home/sk/ostechnix.txt' because it does not exist. At line:1 char:1 + Get-Content ostechnix.txt + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (/home/sk/ostechnix.txt:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Or you can simply use the "ls" command to view if the file is exist or not.
Viewing the running processes
To view the list of running processes, just run:
Get-Process
Sample output:
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 0 0.00 0.00 0.02 599 599 agetty 0 0.00 0.00 0.00 2385 385 anacron 0 0.00 0.00 0.00 257 0 ata_sff 0 0.00 0.00 0.07 556 556 auditd 0 0.00 0.00 0.03 578 578 avahi-daemon 0 0.00 0.00 0.00 590 578 avahi-daemon 0 0.00 0.00 0.05 2327 327 bash 0 0.00 0.00 0.00 19 0 bioset 0 0.00 0.00 0.00 352 0 bioset 0 0.00 0.00 0.00 360 0 bioset 0 0.00 0.00 0.35 597 597 crond 0 0.00 0.00 0.00 31 0 crypto 0 0.00 0.00 0.11 586 586 dbus-daemon 0 0.00 0.00 0.03 63 0 deferwq 0 0.00 0.01 0.93 585 585 firewalld 0 0.00 0.00 0.00 30 0 fsnotify_mark 0 0.00 0.00 0.00 43 0 ipv6_addrconf 0 0.00 0.00 0.02 94 0 kauditd 0 0.00 0.00 0.00 20 0 kblockd 0 0.00 0.00 0.00 14 0 kdevtmpfs 0 0.00 0.00 0.00 351 0 kdmflush 0 0.00 0.00 0.00 359 0 kdmflush 0 0.00 0.00 0.00 13 0 khelper 0 0.00 0.00 0.03 29 0 khugepaged 0 0.00 0.00 0.00 26 0 khungtaskd 0 0.00 0.00 0.00 18 0 kintegrityd 0 0.00 0.00 0.00 41 0 kmpath_rdacd 0 0.00 0.00 0.00 42 0 kpsmoused 0 0.00 0.00 0.00 28 0 ksmd 0 0.00 0.00 0.17 3 0 ksoftirqd/0 0 0.00 0.00 0.02 27 0 kswapd0 0 0.00 0.00 0.00 2 0 kthreadd 0 0.00 0.00 0.00 39 0 kthrotld 0 0.00 0.00 0.01 2313 0 kworker/0:0 0 0.00 0.00 0.04 2369 0 kworker/0:0H 0 0.00 0.00 0.00 2440 0 kworker/0:1 0 0.00 0.00 0.05 2312 0 kworker/0:2H 0 0.00 0.00 0.28 2376 0 kworker/0:3 0 0.00 0.00 0.25 6 0 kworker/u2:0 0 0.00 0.00 0.00 272 0 kworker/u2:2 0 0.00 0.00 0.01 473 473 lvmetad 0 0.00 0.00 0.02 2036 036 master 0 0.00 0.00 0.00 21 0 md 0 0.00 0.00 0.00 7 0 migration/0 0 0.00 0.00 0.00 15 0 netns 0 0.00 0.00 0.22 653 653 NetworkManager 0 0.00 0.00 0.00 16 0 perf 0 0.00 0.00 0.01 2071 036 pickup 0 0.00 0.00 0.05 799 799 polkitd 0 0.00 0.02 5.02 2401 327 powershell 0 0.00 0.00 0.00 2072 036 qmgr 0 0.00 0.00 0.00 8 0 rcu_bh 0 0.00 0.00 0.73 10 0 rcu_sched 0 0.00 0.00 0.00 9 0 rcuob/0 0 0.00 0.00 0.51 11 0 rcuos/0 0 0.00 0.00 0.06 582 582 rsyslogd 0 0.00 0.00 0.00 267 0 scsi_eh_0 0 0.00 0.00 0.00 271 0 scsi_eh_1 0 0.00 0.00 0.00 275 0 scsi_eh_2 0 0.00 0.00 0.00 269 0 scsi_tmf_0 0 0.00 0.00 0.00 273 0 scsi_tmf_1 0 0.00 0.00 0.00 277 0 scsi_tmf_2 0 0.00 0.00 0.03 1174 174 sshd 0 0.00 0.00 0.79 2322 322 sshd 0 0.00 0.00 1.68 1 1 systemd 0 0.00 0.00 0.24 453 453 systemd-journal 0 0.00 0.00 0.04 579 579 systemd-logind 0 0.00 0.00 0.19 481 481 systemd-udevd 0 0.00 0.00 0.54 1175 175 tuned 0 0.00 0.00 0.02 12 0 watchdog/0 0 0.00 0.00 0.01 798 798 wpa_supplicant 0 0.00 0.00 0.00 17 0 writeback 0 0.00 0.00 0.00 378 0 xfs_mru_cache 0 0.00 0.00 0.00 379 0 xfs-buf/dm-1 0 0.00 0.00 0.00 539 0 xfs-buf/sda1 0 0.00 0.00 0.00 382 0 xfs-cil/dm-1 0 0.00 0.00 0.00 542 0 xfs-cil/sda1 0 0.00 0.00 0.00 381 0 xfs-conv/dm-1 0 0.00 0.00 0.00 541 0 xfs-conv/sda1 0 0.00 0.00 0.00 380 0 xfs-data/dm-1 0 0.00 0.00 0.00 540 0 xfs-data/sda1 0 0.00 0.00 0.51 383 0 xfsaild/dm-1 0 0.00 0.00 0.00 543 0 xfsaild/sda1 0 0.00 0.00 0.00 377 0 xfsalloc
The above command will display the whole list of running processes in your Linux system.
To view any particular running process, use '-Name' option with the above command.
For example, to view the powershell process, run:
Get-Process -Name pwsh
Sample output:
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 0 0.00 99.32 3.28 2575 398 pwsh
Viewing command aliases
Are you too lazy to type a whole command? Just type few words and hit the tab key, the command will autocomplete or the list of suggested commands will display, just like in Linux BASH shell.
Alternatively, there are aliases for some commands.
For example, to clear the screen, you would type: Clear-Host.
Or you can simply type the alias of the above command 'cls' or 'clear' to clear the screen.
To view the list of available aliases, run:
Get-Alias
Here is the complete list of available aliases:
CommandType Name Version Source ----------- ---- ------- ------ Alias ? -> Where-Object Alias % -> ForEach-Object Alias cd -> Set-Location Alias chdir -> Set-Location Alias clc -> Clear-Content Alias clear -> Clear-Host Alias clhy -> Clear-History Alias cli -> Clear-Item Alias clp -> Clear-ItemProperty Alias cls -> Clear-Host Alias clv -> Clear-Variable Alias cnsn -> Connect-PSSession Alias copy -> Copy-Item Alias cpi -> Copy-Item Alias cvpa -> Convert-Path Alias dbp -> Disable-PSBreakpoint Alias del -> Remove-Item Alias dir -> Get-ChildItem Alias dnsn -> Disconnect-PSSession Alias ebp -> Enable-PSBreakpoint Alias echo -> Write-Output Alias epal -> Export-Alias Alias epcsv -> Export-Csv Alias erase -> Remove-Item Alias etsn -> Enter-PSSession Alias exsn -> Exit-PSSession Alias fc -> Format-Custom Alias fhx -> Format-Hex 3.1.0.0 Microsoft.PowerShell.Utility Alias fl -> Format-List Alias foreach -> ForEach-Object Alias ft -> Format-Table Alias fw -> Format-Wide Alias gal -> Get-Alias Alias gbp -> Get-PSBreakpoint Alias gc -> Get-Content Alias gci -> Get-ChildItem Alias gcm -> Get-Command Alias gcs -> Get-PSCallStack Alias gdr -> Get-PSDrive Alias ghy -> Get-History Alias gi -> Get-Item Alias gin -> Get-ComputerInfo 3.1.0.0 Microsoft.PowerShell.Management Alias gjb -> Get-Job Alias gl -> Get-Location Alias gm -> Get-Member Alias gmo -> Get-Module Alias gp -> Get-ItemProperty Alias gps -> Get-Process Alias gpv -> Get-ItemPropertyValue Alias group -> Group-Object Alias gsn -> Get-PSSession Alias gsv -> Get-Service Alias gu -> Get-Unique Alias gv -> Get-Variable Alias h -> Get-History Alias history -> Get-History Alias icm -> Invoke-Command Alias iex -> Invoke-Expression Alias ihy -> Invoke-History Alias ii -> Invoke-Item Alias ipal -> Import-Alias Alias ipcsv -> Import-Csv Alias ipmo -> Import-Module Alias kill -> Stop-Process Alias md -> mkdir Alias measure -> Measure-Object Alias mi -> Move-Item Alias move -> Move-Item Alias mp -> Move-ItemProperty Alias nal -> New-Alias Alias ndr -> New-PSDrive Alias ni -> New-Item Alias nmo -> New-Module Alias nsn -> New-PSSession Alias nv -> New-Variable Alias oh -> Out-Host Alias popd -> Pop-Location Alias pushd -> Push-Location Alias pwd -> Get-Location Alias r -> Invoke-History Alias rbp -> Remove-PSBreakpoint Alias rcjb -> Receive-Job Alias rcsn -> Receive-PSSession Alias rd -> Remove-Item Alias rdr -> Remove-PSDrive Alias ren -> Rename-Item Alias ri -> Remove-Item Alias rjb -> Remove-Job Alias rmo -> Remove-Module Alias rni -> Rename-Item Alias rnp -> Rename-ItemProperty Alias rp -> Remove-ItemProperty Alias rsn -> Remove-PSSession Alias rv -> Remove-Variable Alias rvpa -> Resolve-Path Alias sajb -> Start-Job Alias sal -> Set-Alias Alias saps -> Start-Process Alias sasv -> Start-Service Alias sbp -> Set-PSBreakpoint Alias sc -> Set-Content Alias select -> Select-Object Alias set -> Set-Variable Alias si -> Set-Item Alias sl -> Set-Location Alias sls -> Select-String Alias sp -> Set-ItemProperty Alias spjb -> Stop-Job Alias spps -> Stop-Process Alias spsv -> Stop-Service Alias sv -> Set-Variable Alias type -> Get-Content Alias where -> Where-Object Alias wjb -> Wait-Job
To view the alias for any particular command, type:
Get-Alias cls
Sample output:
CommandType Name Version Source ----------- ---- ------- ------ Alias cls -> Clear-Host
Viewing complete list of available commands
To view the list of all available PowerShell commands, run:
Get-Command
Viewing help
Don't know what will particular do? No problem. Just type 'help' to get help. You don't have to search on Internet.
You can also use 'Get-Help' command along with the any powershell commands. It is something similar to 'man' command in the Linux.
For example, to display the help section of a command called "Clear-Host", run:
Get-Help Clear-Host
Sample output:
NAME Clear-Host SYNOPSIS SYNTAX Clear-Host [<CommonParameters>] DESCRIPTION RELATED LINKS https://go.microsoft.com/fwlink/?LinkID=225747 REMARKS To see the examples, type: "get-help Clear-Host -examples". For more information, type: "get-help Clear-Host -detailed". For technical information, type: "get-help Clear-Host -full". For online help, type: "get-help Clear-Host -online
As you see above, 'Get-Help' displays the help section of a specific PowerShell command, like the name of the command, syntax format, aliases, and remarks etc.
To exit from the PowerShell console, just type:
exit
I hope you got a basic idea about how to install PowerShell Core alpha version in Linux (Ubuntu and CentOS), and the basic usage.
Further reading:
- PowerShell on GitHub
- Getting started with PowerShell from Channel9 - Video tutorial
- PowerShell community page
- PowerShell YouTube channel
Related read:
8 comments
Ugh, why bother. With Powershell, MS is fixing something that ain’t broke. Learn bash. Learn zsh. Learn something useful.
Agreed brother.
Yeah, I saw this and was like, ‘what the heck? Why would I PowerShell?’
Agree too o/
nevermind.
I’m sorry. but this is pointless. There are already a myriad of command-line shells, why in the world would I want something from Microsoft on my system? Which happens to work WITHOUT bugs or glitches? As far as I’m concerned the entire “Microsoft Loves Linux” issue is one, that makes no sense. For DECADES, they’ve done everything in their power to ENSUE that things “don’t play nice” in the world of Linux, which literally FORCED the Linux developers to come up with their own solutions to problems. This…in turn “forced” the Linux communities to learn whole new ways and methods of doing things. And now?….just because there’s a new CEO who happens to have “warm and fuzzy” feelings towards Linux the users are supposed to abandon their trappings for this new “Frankenstein” mixture of MS and Open Source Linux software? Nah. No thanks. They’ve taken their ball and gone home, and now they come back wanting to play again, but the GAME has changed and the BALL is different! For those who might want to “experiment” with it….enjoy. As for me and my Linux machines?…we’re going to REMAIN Microsoft-free for as long as there are Linux distros available for download.
PowerShell, Magicarp of shells.
Although the PS commands are crappy, still it will be useful for those working in multi platform environment. To be honest Linux hasn’t widespread among end users who still craving for user friendly OS across the world, specifically in middle east and other parts of the country even in India. Employers still looking experts in MS Windows server. Personally I love Linux but at the same time we should walk hand in hand with windows environment to land a better job.