Thursday, January 11, 2018

How to Install Windows PowerShell Core 6.0 in Linux

https://www.ostechnix.com/how-to-install-windows-powershell-in-linux


install windows powershell
The CEO of Microsoft Mr.Satya Nadella said, “Microsoft loves Linux”. He hasn’t just said that, but also proved it. After partnership with Ubuntu, Microsoft has now open sourced PowerShell, and made it available on Linux and Mac OS. Currently, PowerShell supports CentOS, RHEL, Ubuntu Linux operating systems (more will follow), and Mac OS X. For those who don’t know, 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 the .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 LTS, Ubuntu 16.04 LTS and CentOS 7 64-bit server editions.

Install Windows PowerShell Core 6.0 in Linux

As of now, PowerShell supports RHEL and its clones like CentOS, Ubuntu operating systems. PowerShell developers now made installation much easier.
On Ubuntu 14.04 LTS:
Add PowerShell Repository public key:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
Add PowerShell repository:
curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list
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 public key:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
Add PowerShell repository:
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft.list
Update the software sources list:
sudo apt-get update
Then, install PowerShell using command:
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 > /etc/yum.repos.d/microsoft.repo
Update the repositories list:
yum install -y powershell
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.
powershell
This is how PowerShell console looks like in my CentOS 7 server.
PowerShell 
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS /root>
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.0.0-alpha 
PSEdition Core 
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} 
BuildVersion 3.0.0.0 
GitCommitId v6.0.0-alpha.15 
CLRVersion 
WSManStackVersion 3.0 
PSRemotingProtocolVersion 2.3 
SerializationVersion 1.1.0.1
As you see in the above screenshot, the version of the PowerShell is 6.0.0-alpha.15.
Creating files
To create a new file, use ‘New-Item’ command as shown below.
New-Item ostechnix.txt
Sample output:
 Directory: /root


Mode LastWriteTime Length Name 
---- ------------- ------ ---- 
------ 2/5/17 7:05 PM 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  
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 '/root/ostechnix.txt' because it does not exist.
At line:1 char:1
+ Get-Content ostechnix.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo : ObjectNotFound: (/root/ostechnix.txt:String) [Ge 
 t-Content], ItemNotFoundException
 + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo 
 ntentCommand
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 powershell
Sample output:
 NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName 
 ------ ----- ----- ------ -- -- ----------- 
 0 0.00 0.02 5.19 2401 327 powershell
Check the following link to learn how to work in Windows PowerShell.
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. You don’t have to search on Internet. Just run ‘Get-Help’ command along with the powershell command. 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 []
 
 
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.
For further reading:
You might want to download the free resources related to PowerShell and Windows.
That’s all for today. If you find this guide useful, share it on your social networks and support OSTechNix.
Cheers!
Happy weekend!!

No comments:

Post a Comment