Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

How to Install and Use Microsoft PowerShell on Linux

November 30, 2017 01:50PM

As Microsoft has made Powershell open source and can be installed and used on different operating system family (Linux and MacOS), Basically Microsoft is working towards new Powershell Core make it available everywhere now and it is cross platform. As I don't have much knowledge on bash scripting, I can use my PowerShell scripting knowledge on Linux to write some scripts for automation. Powershell core can be downloaded from https://github.com/PowerShell/PowerShell. There are different powershell application packages available on github for different OS flavors.

Github microsoft powershell for linux centos, rpm, deb download, Powershell DSC on linux

Once downloaded PowerShell rpm file I have used WinSCP tool to copy/transfer rpm file on Linux server. When I list directory where RPM file is stored with ls -l, file name shows in Red. Red color means it can not be executed and installed. To make it executable and modify permissions run command chmod +777 powershell-xxx.rpm, once again I run ls -l, file color shows Green and good to execute run. (I don't have yum repository configured so I am using rpm file)

Powershell on centos linux DSC, chmode 777, execute rpm, rpm qa, microsoft powershell core rhel.png

To install package run command - sudo yum install -y powershell-xxx.rpm. This ask for sudo root password, while installation few lines need to read from onscreen installation logs, there are no dependencies for Powershell, and it is installed successfully.  

Powershell rpm yum install on linux, sudo, dependancies resolved, microsoft powershell core, installed successfully

Next I am using putty to login Linux server (Powershell yellow colored commands are not visible correctly on white background terminal). I can view and verify Powershell rpm is installed successfully using rpm -qa powershell command. Type powershell in the console (case sensitive) to execute powershell, but in my case it shows error as bash: powershell: command not found... Instead use pwsh command. and try normal cmdlets (ie: Get-Process, Get-Command) to check it is working as expected.

redhat centos powershell rhel, putty, rpm, linux on powershell dsc, pwsh, get-process linux, get-command linux, desired state configuration.png

Command exit will close powershell and come back to bash shell. I am not master on Linux, but I do normal troubleshooting very well, here I can add alias powershell to pwsh in /etc/profile using command echo 'alias powershell='pwsh'' >> /etc/profile. next time I can execute powershell by running powershell only.

redhat centos powershell rhel, putty, rpm, linux on powershell dsc, pwsh, get-process linux, get-command linux, desired state configuration, get-executionpolicy, echo, alias powershell, source load profile, get-variable

When Powershell is installed on Linux it is located under default path /opt/microsoft /powershell/6.xxxxx/. Next verify powershell version using command $PSVersionTable.

Microsoft powershell core linux psversion psedition, root opt folder, psversiontable install powershell on linux centos

Useful Blogs
Different ways to bypass Powershell execution policy :.ps1 cannot be loaded because running scripts is disabled
Installing, importing and using any module in powershell
POWERSHELL PS REMOTING BETWEEN STANDALONE WORKGROUP COMPUTERS 

Go Back

Comment