Guest Contributer
Full Stack Developer

Installing PowerShell on Mac

Installing PowerShell on Mac

PowerShell is one of the most utility-rich command-line shells developed by Microsoft. In fact, it's much more than just another command-line shell. If you've ever used a Windows system, you probably already know about it since it comes preinstalled on a Windows device by default.

However, most people assume that PowerShell is exclusive to a Windows system.

So, can you run PowerShell on a Mac? Yes, you can!

In this post, I'll walk you through how to install and use PowerShell on Mac. We'll explore some common commands as well.

I'll show you how to install this using Homebrew but you can find different other ways to install PowerShell on Mac from Microsoft's official guide here.

Make sure you have Homebrew installed. If you don't have it already, you can install it from here.

Now, open your command line and run the following command:

 

The above command installs PowerShell on your Mac device using Homebrew. The --cask flag is necessary due to the licenses involved in using and installing PowerShell on Mac. Once you do that, you may also be prompted to enter your device password (if any).

As your installation proceeds, you should see some messages on your terminal indicating the status and progress of the installation:

It should take a few minutes before the entire process is completed and PowerShell is successfully installed on your Mac.

Verify PowerShell Installation on Mac

Now that PowerShell is successfully installed, let's verify it. In order to verify that PowerShell is installed on Mac, run the pwsh command.

Running this command should give back the version of PowerShell installed. In this tutorial, we've installed version 7.2.6. It should also give a message pertaining to Microsoft Corporation's copyright.

It looks like we've also verified PowerShell's installation on our Mac device. You can further run the PSVersion command that will tell you the version of PowerShell installed.

It should also indicate the OS and platform of your device. In order to run this command, type the following on your command line and hit enter: $PSVersionTable.

Here is how the PSVersion Table should be returned back to you:

Awesome!

Let's explore some common commands in PowerShell now. As indicated in the above message, you can also directly run the help command to get some information on using PowerShell.

List All PowerShell Commands

The best way to learn and explore PowerShell is to browse through all the available commands that you can run. The Get-Command in PowerShell retrieved a list of all the available commands that you can run on it. Let's run the Get-Command. To do that, simply type "Get-Command" and hit enter on your terminal.

It should return a number of commands, some of which are shown below:

Notice that the command list also tells you the type of each command and the PowerShell version it's compatible with. Do you want to go ahead and try out a command from the Get-Command list?

Let's move ahead!

Check Built-in Variables

You can check the built-in variables that indicate the OS of your device. The next command that does this is Get-Variable. Simply run the following on your command line: Get-Variable Is*.

It should return a table with some booleans and their corresponding values. We're on a Mac right now, so only the isMacOS should be true, and the rest should be false.

Notice that there's another variable called isCoreCLR. This is a PowerShell variable that tells us if PowerShell was built targeting the device we're currently running it from.

List All Available Modules

You can also list all of the available PowerShell modules in the system using PowerShell. Run the following Get-Module command in your terminal:

It should then list all of the available modules in a table. It also tells you the directory where these modules are present.

Using Windows Commands

To list all the directories inside the current directory, we generally run the following command on a Mac: ls.

It then displays the directories or files and folders present inside the current directory where you're running the command from:

The equivalent of this in Windows is the dir command. With PowerShell, you can run all the Windows equivalents of your Mac commands. Now if we run the following on the terminal—dir—we get the same list of directories present inside the current directory in a table fashion.

With this, you also see some more metadata around these directories, such as when they were last updated, etc.

You can try running some more of the commonly used Windows commands here.

Getting Currently Running Process in PowerShell

The Get-Process command is commonly used to list all the running processes on your device locally. Let's run it by typing Get-Process on the command line and hitting enter.

It should then return a list of all the processes along with their process ID:

Further, you can also list specific processes by mentioning their process ID. For instance, if we want to see the process with ID 296, we can run the following on the terminal:

That should then return that particular process back:

Using .NET on PowerShell

PowerShell is more powerful than the regular command line. In general, command-line shells simply take a string as a command and return another string back as a result of the command. However, with PowerShell, you can run .NET natively on it. This means that you can pass and return complex .NET data structures like .NET objects.

Or you can simply run some .NET commands directly on PowerShell. For instance, here's how we can use the Math command to find the square of 3:

In the above command, [math] is the object and pow is the function available on the Math object. Let's take a look at the output as well:

Like any other shell, you can also run simple computations. Or you can see today's date using the Get-Date command:

You can further manipulate the above Get-Date command by returning a date four days in advance:

 

Using OpenSSL With PowerShell

Ever since Google has mandated the use of HTTPS for secure browsing, SSL has become an essential thing for developers. OpenSSL allows you to create digital SSL certificates, sign them, create private keys for them, and much more. All you need to do is run a bunch of OpenSSL commands from your command-line system.

PowerShell allows you to easily set up some environment variables required for OpenSSL. You can also run all the OpenSSL commands directly from the PowerShell.

PowerShell on Mac vs. Windows

You've seen how Windows commands can be easily translated to a Mac using PowerShell. However, you might think that PowerShell on a Mac behaves exactly the same as on Windows. However, that's not true. PowerShell is built on top of .NET and, hence, any OS level or platform-specific differences for operating .NET will introduce some disparities on Mac and Windows.

Windows systems use the full-blown .NET framework. However, Lunix and Unix OS only use a part of the .NET framework—that is, .NET core. Hence, if you run some scripts on a Windows machine that utilize components of the .NET framework that aren't present in .NET core, they'll fail to run the same way on a Mac device.

Conclusion

In this tutorial, we've learned how to install and verify PowerShell installation on Mac. Using Homebrew makes it really convenient to install PowerShell directly without installing it from native binaries, which can be trickier at times.

We've explored how to list all the commands and processes, and we've seen how we can run Windows-equivalent commands on a Mac using PowerShell.

Feel free to explore other commands or learn even more about what different commands mean and how else you can use PowerShell on your Mac device.

This post was written by Siddhant Varma. Siddhant is a full stack JavaScript developer with expertise in frontend engineering. He’s worked with scaling multiple startups in India and has experience building products in the Ed-Tech and healthcare industries. Siddhant has a passion for teaching and a knack for writing. He's also taught programming to many graduates, helping them become better future developers