When we say the term PowerShell, all we can think of is Windows operating system. But do you know you can install PowerShell on macOS also? I know it’s weird to say that, but in this article, I will show you the steps to install PowerShell on a Mac operating system. Back in 2016, Microsoft announced that PowerShell is open-sourced and is available for Linux as well as macOS. So, if you have a macOS, now you do not need to start windows virtual machine on your macOS to use PowerShell. Instead, just install PowerShell on the macOS, as shown in this article, and start using it.
What is PowerShell?
PowerShell is a task-based command-line shell and scripting language built on the .NET Framework to help IT professionals control and automate the administration of the Windows, Linux, Mac operating systems and the applications that run on them.
Prerequisites
To install PowerShell 7.0 or higher on macOS, you need to have macOS 10.13 or a higher version.
PowerShell Installation on macOS
There are 3 different ways in which you can easily install PowerShell on macOS:
- Installing PowerShell on macOS using Homebrew. Homebrew is the most widely used package manager on macOS.
- Installing PowerShell on macOS using binary archives
- Installing PowerShell on macOS by Direct Download
You can choose any of the above-mentioned ways to install PowerShell on macOS. Once the installation finishes, you will have to setup OpenSSL to perform remoting ad CIM operations on macOS PowerShell. Let me now show you all the 3 ways of installing PowerShell on macOS.
Installing PowerShell on macOS using Homebrew
Most of the Mac users would already have Homebrew installed on their macOS. In case you do not have it, firstly install brew on your system. To install homebrew, open the terminal in your macOS and run the command below:
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
Now the homebrew is available on the macOS, you the brew command to install PowerShell on macOS:
brew install --cask powershell
After the installation finishes, you should verify if the installation happened correctly and PowerShell is working properly. Use the command below to check that:
pwsh
In future, when you have new releases of PowerShell, and you would like to upgrade to that, you just need to update brew first on the macOS and then upgrade to the latest PowerShell version using the command below:
brew update
brew upgrade powershell –cask
Installing PowerShell on macOS using binary archives
In the binary archive method, you need to download tar.gz file of the latest stable release of PowerShell from here. Go to the release page and download the osx PowerShell file as shown below:
You can also run the below command to directly download PowerShell binary archive without going to the releases page.
curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.1.4/powershell-7.1.4-osx-x64.tar.gz
Now create a new folder to place the PowerShell binary archive files.
sudo mkdir -p /usr/local/microsoft/powershell/latest
Now extract all the binary archive files to the new folder created.
sudo tar zxf /tmp/powershell.tar.gz -C /usr/local/microsoft/powershell/latest
Set the execute permission for the PowerShell application on macOS.
sudo chmod +x /usr/local/microsoft/powershell/latest/pwsh
Create a symbolic link for the pwsh application file.
sudo ln -s /usr/local/microsoft/powershell/latest/pwsh /usr/local/bin/pwsh
When you choose the binary method for installation, you also need to install all the dependencies manually.
Installing PowerShell on macOS by Direct Download
You can find the stable release of PowerShell here. Go to the release page, scroll down to the bottom and download the .pkg file of the latest stable PowerShell release on your macOS.
You can either double click the .pkg file to begin the PowerShell installation or run the command below in the mac terminal:
sudo installer -pkg powershell-7.1.4-osx-x64.pkg -target /
That’s it, and this single command is going to install PowerShell for you on your macOS.
Uninstall PowerShell on macOS
If you had installed PowerShell using Homebrew on macOS, use the uninstall command:
brew uninstall --cask powershell
If you had installed PowerShell through direct download, then you need to manually remove it from macOS using the command below:
sudo rm -rf /usr/local/bin/pwsh /usr/local/microsoft/powershell
After this, you need to manually remove all the paths that were set when installation happened. Below are those paths:
Home -> /usr/local/microsoft/powershell/latest
User Profiles -> ~/.config/powershell/profile.ps1
Default Profiles -> $PSHOME/profile.ps1
User modules -> ~/.local/share/powershell/Modules
Shared modules -> /usr/local/share/powershell/Modules
Default modules -> $PSHOME/Modules
PSReadline history -> ~/.local/share/powershell/PSReadLine/ConsoleHost_history.txt
Final Thoughts
PowerShell is a very powerful tool created by the Microsoft team, which is built on the .NET framework. It a very useful tool for administering the applications of your operating system. Earlier it was only used by Windows users, but now you can use it on macOS also. So go ahead, choose any of the installation methods mentioned in this article, install the PowerShell, and give it a try.