Friday, 20 January 2017

How to Install and Use PowerShell on Linux

Windows PowerShell is an excellent configuration management and automation framework. It’s comprised of a command line shell and .NET-based scripting language. Administrators use PowerShell as a means of managing Windows, Linux, and networked clients.
In 2016, Microsoft open-sourced PowerShell and debuted a cross-platform iteration. This .NET Core version offers Linux and macOS installers. Because system administrators often use Linux, PowerShell on Linux brought lots of new functionality. Although it’s essentially the same program, there are key differences such as the ability to run standard Bash commands in Linux PowerShell.
In this article I’m going to show you how to install, run, and use PowerShell on Linux!

What Is Windows PowerShell?

Windows PowerShell is a command-line framework for configuration management and task automation. Offering complete WMI and COM access, PowerShell enables administrators to manage remote and local Windows systems. CIM and WS-Management make PowerShell capable of managing network devices as well as Linux systems. Although PowerShell and Command Prompt may appear similar, they definitely differ.

In 2016, Windows debuted its “Windows loves Linux” campaign. Among the Linux-Windows partner projects was Bash on Windows. Users running the Windows 10 Anniversary Update can install a full Linux subsystem on Windows machines. This yields access to the command line. You can install and run various GUI programs. However, using the command line is the true intent behind Bash for Windows.

Installing PowerShell on Linux (With Command Line)

Start by downloading the appropriate PowerShell on Linux installer from the GitHub page. You can find installers for Ubuntu 16.04 and 14.04, Red Hat Enterprise Linux 7 and CentOS 7, and macOS. Start by downloading the correct installer for your operating system. I’m running Ubuntu 16.04 so I downloaded the Ubuntu 16.04 DEB. However the installer should work for any Ubuntu derivative like Lubuntu and Xubuntu.
Ubuntu users will have a DEB package whereas CentOS and RHEL users will use an RPM. The macOS installer is a PKG.
PowerShell Downloads
Because PowerShell is a command line utility, let’s first look at installing PowerShell via the Linux command line. Note where your PowerShell DEB is downloaded. Typically this will be the default “Downloads” directory. Once you’ve located the package, run the following command:

sudo apt-get install libunwind8 libicu55
sudo dpkg -i /path/to/your/powershell.deb
Replace the “path to” line with the correct folder path and exact DEB. I used the Ubuntu 16.04 package and the DEB was located in my “Downloads” directory. Therefore the command I ran looked like:
sudo apt-get install libunwind8 libicu55
sudo dpkg -i /home/moe/Downloads/powershell_6.0.0-alpha.14-1ubuntu1.16.04.1_amd64.deb
This installs Windows PowerShell and its dependencies. Because we’re installing with super user permissions, you’ll be prompted for your password during the install.
Wait as the install wraps up. You’ll see some lines about preparing to unpack PowerShell and setting up PowerShell. Once this concludes, you’re ready to begin using PowerShell.

Installing PowerShell on Linux (No Command Line)

Alternatively, you can install Windows PowerShell on Linux without using the command line. If you’re installing PowerShell, chances are you have no fear of using the command line. Turn back now if you do. But for simplicity’s sake you can merely run the package installer via the Ubuntu Software Center. Double click the DEB to open it with the Ubuntu Software Center. From here, click Install.
PowerShell Software Center
As with the command line installation, you’ll be prompted for your password.
Once the PowerShell install finishes you’ll see a notification that the process has completed.

Using PowerShell on Linux

Running Windows PowerShell on Linux requires the command line. Unlike many programs, you can’t simply find and launch the program by searching. Open a new terminal and type:
This brings up the lines:

Copyright (C) 2016 Microsoft Corporation. All rights reserved.
Beneath that there’s a line of:
PS /home/moe>
From here, you can use Windows PowerShell exactly as you would on Windows. Simply run PowerShell cmdlets like normal. For instance, we can run:
Get-ChildItem -Path /

This outputs a list of the folders and files in a system drive. Any command that functions in PowerShell on Windows should work properly on Linux. To test, I also ran:
This command enables breakpoints in the console. For a list of common PowerShell commands, try these basic PowerShell commands. For a deep dive, check out this helpful blog post with loads of commands. There are some key differences though. In PowerShell on Linux, you can use native Linux commands or the PowerShell variants. So PowerShell on Linux acts as a hybrid of the Linux command line and PowerShell. Various commands such as

No comments:

Post a Comment