Yes, you read that right. This is the first in what will be an ongoing series on the topic of PowerShell on macOS. Stick around and follow along if you are already a Mac admin, looking to expand your skillset. Or perhaps you’re a Windows admin hoping to leverage your existing PowerShell skills to embrace macOS clients.
With its UNIX underpinnings, there’s no shortage of scripting languages and shell environments available for macOS, so why bother learning PowerShell? Isn’t that a Windows thing? Yes. It is. Or was, originally. I was skeptical for a while, too – honestly, I still am – that Microsoft would open the source, make PowerShell run on Linux and macOS, and then continue to support and develop new versions. This is Microsoft, after all. Surely it will eventually be abandoned in favor of something proprietary and incompatible. Or so I thought at the time. However, PowerShell Core, as the open source product has been dubbed, has continued to evolve since the general release, from version 6 in 2018 to the current version 7.3.
Windows admins have relied on PowerShell for scripting and command line interaction for more than a decade, and as such, there is a considerable user community surrounding it. Microsoft’s documentation and supporting tools are similarly extensive. Between first-party- and community-generated examples, tutorials, and support forums, you would be hard pressed to find something that hasn’t already been done in PowerShell. At least, on Windows. Mac and Linux examples are thin on the ground, but this series aims to remedy that, at least in some small way.
For the most part, the PowerShell syntax and command structure will be the same across all the supported platforms. Obvious differences aside – drive letters, the orientation of slashes in paths, and security postures – translating sample PowerShell code from one platform to another is about as difficult as modifying a bash script intended for Linux into one that works on macOS. With a little bit of work, it’s possible to write scripts that will work on any platform. And if not, at least fail gracefully, which is sometimes all one can ask.
Before you dive into learning an entirely new scripting language, though, there are a few things you should be aware of.
Much has been written about Apple deprecating sh and bash in favor of zsh, and the resulting scramble to update legacy automation code. With PowerShell in active development, it’s even more of a moving target than zsh. It seems that the 7.x versions are more stable in terms of changes to the syntax, but you are likely to run into some frustration if you learn from code examples that were written for 6.x or older. I know I have.
The ultimate goal of this series is to teach you, gentle reader, to use PowerShell to do useful Mac admin work. One major roadblock in our way is that PowerShell isn’t installed by default on your Mac, or any of your client machines for that matter. And if it is, many of the tasks for which you would leverage it require dependencies and libraries in the form of “cmdlets” and other modules. While you’re less likely to become ensnared in “dependency hell” than in other environments (I’m looking at you, Python), it’s worth mentioning.
To simplify matters, I’ll make a few assumptions about the Mac you’re working on:
I know what they say about assuming, but this should make my job less complicated. Or at least complicated in a predictable way.
So, to begin: Install PowerShell.
This can be accomplished by downloading a package directly from the Microsoft PowerShell Team’s Github repo (https://github.com/PowerShell/PowerShell), but my preferred and recommended method is to use Homebrew.
Quick aside: If you aren’t familiar with Homebrew, or you don’t have it installed already, stop right here and install it. Go to https://brew.sh and follow the instructions there. I’ll wait.
Everybody on the same page now?
The process to install PowerShell and any requisite libraries with Homebrew is this Terminal command:
% brew install powershell
The command should begin spitting out progress messages, then pause to ask for your password.
==> Caveats
To use Homebrew in PowerShell, set:
Add-Content -Path $PROFILE.CurrentUserAllHosts -Value '$(/usr/local/bin/brew shellenv) | Invoke-Expression'
==> Downloading https://github.com/PowerShell/PowerShell/releases/download/v7.3.1/powershell-7.3.1-osx-x64.pkg
==> Downloading from https://objects.githubusercontent.com/github-production-release-asset-2e65be/49609581/81c4e6f9-8081-4b2
######################################################################## 100.0%
All formula dependencies satisfied.
==> Installing Cask powershell
==> Running installer for powershell; your password may be necessary.
Package installers may write to any location; options such as `--appdir` are ignored.
Password:
installer: Package name is PowerShell - 7.3.1
installer: Installing at base path /
installer: The install was successful.
🍺 powershell was successfully installed!
For the most part, you can ignore the details, so long as the last line says the package was successfully installed. We’ll get back to the advice in the “Caveats” section in a future article.
Once installed, you can invoke PowerShell in much the same way you would open any shell. At the Terminal prompt, type:
% pwsh
And your prompt will change to something like this:
PowerShell 7.3.1
PS /Users/username>
For the moment, that’s enough to know the installation was, indeed, successful. This will get you back to more familiar territory:
> exit
You may have noticed that as soon as you started typing with ‘e’ the ‘exit’ command was offered as an automatic suggestion. (ProTip) That’s going to come in handy.
Running an interactive PowerShell session is certainly a good way to learn the syntax and play around, but the use case for most admins is going to involve writing and saving scripts. To do that, you’ll need a text editor.
I prefer to do nearly all my coding in the elder statesman of Mac text editors, BBEdit. If you’re similarly of the opinion that BBEdit is the only text editor that doesn’t suck*, then you should be fine to follow along with the examples and tutorials I’m planning for this series. You may need to install a PowerShell language module, for things like code highlighting and autocompletion: https://github.com/doug-baer/BBEdit-PowerShell
However, the weapon of choice for most people writing and debugging PowerShell is Visual Studio Code. An installer for VS Code can be acquired directly from https://code.visualstudio.com/ or, as before, via Homebrew:
% brew install visual-studio-code
Once installed, open the VS Code app, and in the Code menu, choose Settings > Extensions. A lengthy list of different programming and scripting languages should appear below a search box. Search for “PowerShell”, and the top hit should be the latest version, with Microsoft listed as the publisher. Click the button to install that extension, and you’ll have all the tools necessary to follow along with the tutorials and examples in the rest of this series.
When next we meet, plan on jumping right into the deep end with a few real-world tasks, and learning how to accomplish them with the tools you’ve just installed. In the meantime, though, here are a couple of sites to bookmark and explore on your own:
*No, Bare Bones Software is not paying me to say that. And no, I wouldn’t mind if they sent me some free swag, just saying.
Charles Mangin is an independent Mac consultant, vintage electronics collector, and may or may not be a self-aware collection of bash scripts. He can be found lurking in the forums at https://community.jumpcloud.com/ and at https://oldbytes.space/@option8
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
New to the site? Take a look at these additional resources:
Ready to join us? You can register here.