Running Powershell on Linux

    Powershell on Linux? No way…

    You read correctly: Powershell on Linux is now a reality. The Powershell team just released a version of their magnificent shell on a limited number (for now) of Linux distributions and also on Mac OS X. And that’s not all: Powershell is also open sourced now so the community can join in and bring it’s contribution to the code. Now for those that listened to Mr. Snover’s talks from the last year this is not really a surprise as he said that this would happen. Even with this in mind, having a Linux box and being able to get the process list with Get-Process as opposed to just hearing about it is something else.

    Operating Systems it is available on

    The version talked about is an alpha version of Powershell 6 so for now it is not officially available on a lot of Linux distributions. You can install it on CentOS 7, Ubuntu 14.04 and Ubuntu 16.04; this is all for the Linux distributions for now. It can also be installed on Mac OS 10.11, Windows Server 2016/Windows 10 and Windows Server 2012 R2/Windows 8.1. While Powershell on Linux is the main focus of the article, it is interesting that also Mac users get to enjoy the Microsoft shell.

    The source code, download links and more information can be found on the GitHub page: https://github.com/PowerShell/PowerShell. To download a specific version just find the table of operating systems and click on the install package extension. For example for the CentOS version click on .rpm.

Powershell on Linux, Mac and Windows

Powershell on Linux, Mac and Windows

    I think it is time to install Powershell and see some examples of it running on Linux.

    Installing and running Powershell on Linux

    In my examples I am using CentOS 7 installed with the GUI. Access the link above and download the Powershell version for your OS. A window should appear that asks you what to do. I opened the RPM directly and installed it.

Install Powershell on Linux

Install Powershell on Linux

    After the installation is finished just open a terminal and type the magic word: powershell. The prompt will change to something more familiar.

 

Running Powershell on Linux

Running Powershell on Linux

    Let’s see the version of Powershell we are using:

Powershell alpha version

Powershell alpha version

    From the output we can see that for now Powershell on Linux is an alpha version and it is not the current relase but the 6th version. With this in mind it is worth noting that there is still work to be done and not everything can bo done from Powershell for now as you can see from the list of modules available. Also interesting is that script modules are supported.

Powershell on Linux: Modules

Powershell on Linux: Modules

    If you are curious of the number of CmdLets you have at your disposal here is the command. The pipeline is also used in this one.

Powershell on Linux: Number of CmdLets

Powershell on Linux: Number of CmdLets

    I know that it is still hard to beleive that this is real so here is another screenshot that should eliminate any doubt. Let’s use Get-Member on a process list to see what methods and properties processes have.

Process details

Process details

    Speaking of the Get-Process command, it is not finished as not all fields have values right now. See for example getting all proccesses starting with hyperv:

Powershell on Linux: Getting processes

Powershell on Linux: Getting processes

    Let’s try running a script to see what happens. I made a simple one that checks for a folder and creates it if it is not available and then puts a file with the process list there after that. This is how it looks:

A simple Powershell script

A simple Powershell script

    As on Windows, scripts that run in Powershell have the .ps1 extension so save the file and run it like any normal script in PS.

Powershell on Linux: Running a script

Powershell on Linux: Running a script

    The last thing I want us to try is see how exit codes are captured from native Linux programs. The answer is of course the same as on Windows: just see what the $LASTEXITCODE variable contains after running an executable.

Powershell on Linux: Program exit codes

Powershell on Linux: Program exit codes

 

    So this was a first look at running Powershell on Linux. A lot of interesting thing will happen for sure as time goes on and people develop new thing like script modules and improvements to the code.

Leave a Comment

Your email address will not be published. Required fields are marked *