Administrators can perform different administrative tasks on the local computer, or on a remote computer using PowerShell. It’s a very powerful command line tool.
PowerShell is also a very powerful scripting language for system administration. You can write very complex scripts with PowerShell to help you automate your everyday system administration tasks.
In this article, I am going to show you how to install PowerShell on Ubuntu 18.04 LTS and also how to get started with PowerShell. So, let’s dig in.
Installing PowerShell on Ubuntu 18.04 LTS:
PowerShell is not available in the official package repository of Ubuntu 18.04 LTS. But, you can easily add the official Microsoft package repository on Ubuntu 18.04 LTS and install PowerShell from there using the APT package manager.
First, navigate to the /tmp directory with the following command:
Now, download the Microsoft package repository installer DEB package file with the following command:
Now, install the Microsoft package repository installer DEB package with the following command:
Microsoft package repositories should be enabled.
Now, update the APT package repository cache with the following command:
The APT package repository cache should be updated. As you can see, Microsoft package repository is listed here as well. So, Microsoft package repository is being used.
Now, you can install PowerShell on your Ubuntu 18.04 LTS machine.
To install PowerShell on your Ubuntu 18.4 LTS machine, run the following command:
To confirm the installation, press y and then press <Enter>.
PowerShell should be installed.
Now, to check whether PowerShell is working or not, run the following command:
The PowerShell version information should be printed on the console. As you can see, I am running PowerShell 6.2.0. It may be different by the time you read this article.
Now that PowerShell is installed, you can start it with the following command:
PowerShell should start. Now, you can start running PowerShell commands, write PowerShell scripts, run PowerShell scripts and many more.
Getting Started with PowerShell on Ubuntu 18.04 LTS:
In this section of the article, I am going to show you the basics of PowerShell. So, let’s get started.
PowerShell has support for all the common cmd.exe and Unix commands. For example, cat, dir, rm, echo, cls, cd etc.
On Linux, you can use any of the existing Linux commands from PowerShell. As you can see, I ran the apt and whoami Linux commands and it ran successfully.
PowerShell uses a more descriptive aliases for the Windows cmd.exe commands such as dir, cd, cls etc.
You can use the Get-Alias command of PowerShell to find the alias of your desired commands.
For example, to find the alias of the cd command, use Get-Alias as follows:
As you can see, the alias is Set-Location. PoweShell commands are really easy to remember.
As you can see, I can use the Set-Location command to navigate to another directory.
PowerShell also has powerful auto completion features. You use <Tab> the same way as you do on Linux to get auto completion.
If you prefer short commands, PowerShell also got your back. PowerShell provides some standard aliases for that.
Earlier, I used the Get-Alias command. But, I could’ve used a shorter alias gal. It would give me the same result as you can see in the screenshot below.
Here, g is for Get and al is for Alias.
The same way, you can use sal instead of the Set-Alias command. Here, s is for Set and al is for Alias.
For more information on command alias, take a look at the official documentation of PowerShell at https://docs.microsoft.com/en-us/powershell/scripting/learn/using-familiar-command-names?view=powershell-6
On Linux we have man pages. Man pages help us learn what the command is used for, what command options are available, what each of the options do and many more. The same way, you can get help on PowerShell commands.
On PowerShell, the Get-Help command is used to get help on your desired command.
For example, to get help on the Set-Location command we have seen earlier, run the following command:
As you can see, the Get-Help command shows the NAME of the command, the SYNTAX of the command, the ALIASES of the command. But it should show more!
In the earlier example, you’ve seen that the Get-Help command could not find the Help files of the command in my computer. So, it displayed partial help. I actually did not have the Help files downloaded for that command.
To download the Help files on your computer, run the following PowerShell command:
As you can see, PowerShell is downloading and installing the help files.
Once the help files are downloaded, let’s try to get help on the Set-Location command again.
Nothing changed! Well, by default, partial help is displayed. But, you can view full help if you want now.
To display full help on the Set-Location command, run Get-Help as follows:
As you can see, the full help is displayed. The full help contains examples as well as explanation of every options.
If you don’t want to download Help files, you can also find help online with the Get-Help command as follows:
A web browser will open up with the help page for Set-Location as you can see.
The online help page also has examples.
Now that you know how to run commands and how to get help on PowerShell, you should be able to start learning more about it. Thanks for reading this article.