EXCERPT
PowerShell: not just for Windows anymore
From Learn PowerShell in a Month of Lunches, 4th Edition by Travis Plunk, James Petty, Tyler Leonhardt, Don Jones, Jeffery Hicks
Take 37% off Learn PowerShell in a Month of Lunches: 4th Edition by entering fccplunk into the discount code box at checkout at manning.com.
We’ve used and taught PowerShell from the early days (some of us from Version 1). Back then, most of the folks using PowerShell were experienced VBScript users, and they were eager to apply their VBScript skills to learning PowerShell. As a result, we and the other folks who taught PowerShell wrote books and articles, and all adopted a teaching style that takes advantage of prior programming or scripting skills.
From late 2009 a shift occurred. More and more administrators without prior VBScript experience started trying to learn PowerShell. All of a sudden, our old teaching patterns didn’t work as well, because we had focused on scripting and programming. At this point, we realized that PowerShell isn’t a scripting language; It’s a command-line shell where you run command-line utilities. Like all good shells, it has scripting capabilities, but you don’t have to use them, and you certainly don’t have to start with them. We started changing our teaching patterns, beginning with the many conferences we speak at each year.
Why you can no longer afford to ignore PowerShell
Batch. KiXtart. VBScript. Let’s face it, PowerShell isn’t exactly Microsoft’s (or anyone else’s) first effort at providing automation capabilities to Windows administrators. We think it’s valuable to understand why you should care about PowerShell, because when you do, you’ll feel comfortable that the time you commit to learning PowerShell will pay off. Let’s start by considering what life was like before PowerShell came along, and look at some of the advantages of using this shell.
Life without PowerShell
Windows administrators have always been happy to click around in the graphical user interface (GUI) to accomplish their chores. After all, the GUI is largely the point of Windows — the operating system isn’t called Text, after all. GUIs are great because they enable you to discover what you can do. Don remembers the first time he opened Active Directory Users and Computers. He hovered over icons and read tooltips, pulled down menus, and right-clicked things, all to see what was available. GUIs make learning a tool easier. Unfortunately, GUIs have zero return on that investment. If it takes you five minutes to create a new user in Active Directory (assuming you’re filling in a lot of the fields, this is a reasonable estimate), you’ll never get any faster than that. One hundred users take five hundred minutes — there’s no way, short of learning to type and click faster, to make the process go any quicker.
- Microsoft tried to deal with that problem a bit haphazardly, and VBScript was probably its most successful attempt. It might have taken you an hour to write a VBScript that could import new users from a CSV file, but after you’d invested that hour, creating users in the future would take only a few seconds. The problem with VBScript is that Microsoft didn’t make a wholehearted effort in supporting it. Microsoft had to remember to make things VBScript accessible, and when developers forgot (or didn’t have time), you were stuck. Want to change the IP address of a network adapter by using VBScript? OK, you can. Want to check its link speed? You can’t, because nobody remembered to hook that up in a way that VBScript could get to. Sorry. Jeffrey Snover, the architect of Windows PowerShell, calls this the last mile. You can do a lot with VBScript (and other, similar technologies), but it tends to let you down at some point, never getting you through that last mile to the finish line.
Windows PowerShell is an express attempt on Microsoft’s part to do a better job and to get you through the last mile. And it’s been a successful attempt this far. Dozens of product groups within Microsoft have adopted PowerShell, an extensive ecosystem of third parties depend on it, and a global community of experts and enthusiasts are pushing the PowerShell envelope every day.
Life with PowerShell
Microsoft’s goal for Windows PowerShell is to build one hundred percent of a product’s administrative functionality in PowerShell. Microsoft continues to build GUI consoles, but those consoles execute PowerShell commands behind the scenes. This approach forces the company to make sure that every possible thing you can do with the product is accessible through PowerShell. If you need to automate a repetitive task or create a process that the GUI doesn’t enable well, you can drop into PowerShell and take full control for yourself.
Several Microsoft products adopted this approach over the years, including Exchange, SharePoint, System Center Products, Microsoft 365, Azure, and let’s not forget Windows Admin Center. Non-Microsoft products, including AWS and VMware, have taken a keen interest in PowerShell, as well.
Windows Server 2012, which was where PowerShell v3 was introduced, and higher is almost completely managed from PowerShell — or by a GUI sitting atop PowerShell. This is why you can’t afford to ignore PowerShell: Over the last few years, PowerShell became the basis for more and more administration. It’s already become the foundation for numerous higher-level technologies, including Desired State Configuration (DSC), and much more. PowerShell is everywhere!
Ask yourself this question: If you were in charge of a team of IT administrators (and perhaps you are), who would you want in your senior, higher-paying positions? Administrators who need several minutes to click their way through a GUI each time they need to perform a task, or ones who can perform tasks in a few seconds after automating them? We already know the answer from almost every other part of the IT world. Ask a Cisco administrator, or an AS/400 operator, or a UNIX administrator. The answer is, “I’d rather have the person who can run things more efficiently from the command line.” Going forward, the Windows world starts to split into two groups: administrators who can use PowerShell, and those who can’t. As Don famously said at Microsoft’s TechEd 2010 conference, “Your choice is learn PowerShell, or would you like fries with that?”
Windows, Linux and macOS, Oh My!
In mid-2016, Microsoft made the unprecedented decision to open-source PowerShell version 6 (then known at PowerShell Core). At the same time, it released versions of PowerShell — without the Windows attached — for macOS and numerous Linux builds. Amazing! Now, the same object-centric shell is available on many operating systems, and can be evolved and improved by a worldwide community. For this article, we did our best to demonstrate the multi-platform use of PowerShell and included examples for MacOS and Linux environments as well. We still feel that PowerShell’s biggest audience is Windows users, but we also want to ensure you understand how it works on other operating systems.
Meet PowerShell
This section is about getting you situated and helping you to decide which PowerShell interface you’ll use (yes, you have a choice).
This article applies exclusively to PowerShell Windows, macOS and PowerShell on Ubuntu 18.04. Windows and other Linux distributions have a similar setup, but they aren’t covered in this article. For those other installation instructions, you can get them right from PowerShell’s GitHub page at: https://github.com/PowerShell/PowerShell#get-powershell
USEFUL TERMS We should define a few terms that we use in this article:
Shell — A shell is generally referred to as an application that can accept text-based commands and it’s commonly used to interact with your computer or other machines via a script or interactive experience like a terminal. Examples of shells include Bash, Fish, or PowerShell.
Terminal — A terminal is an application that can run a shell application within it to allow a user to interact with the shell in a visual way. Terminals are shell agnostic and you can run any shell in any terminal you’d like.
Windows PowerShell — Refers to PowerShell 5.1 that comes pre-installed on your Windows 10 device
PowerShell — Refers to 7.x version that you have installed.
PowerShell on Windows
PowerShell comes pre-installed on windows PCs from Windows 7 and later versions (and Server 2008). It’s important to note that the process name for PowerShell 7 has changed on Windows. It’s no longer powershell.exe
but instead pwsh.exe.
PowerShell 7 is a side-by-side installation, meaning that Windows PowerShell (5.1) is still installed by default (hence why the process name had to change).
Let’s install PowerShell 7 first. You can install it in multiple ways (Such as from the Windows Store, WinGet, Chooclatley etc.) and you can choose any method you want, but for this article we go with the straight forward approach, which is to download the MSI from the PowerShell GitHub Repo found here PowerShell/PowerShell: PowerShell for every system! (github.com). Make sure you download the stable release as this is the latest GA release from the PowerShell Team.
Walk through the MSI wizard, accept the defaults, and then you’re done.
You can launch PowerShell several ways. After it’s installed, you can search for it in the task bar. This is also a great time to point out the icons have changed a little bit as well.
If you click the PowerShell 7 icon, we suggest you also make it a taskbar icon. This launches the PowerShell console. If you’re familiar with Windows PowerShell you’ll see a noticeable difference in the way it looks. This is because the background color black and not blue. For the purposes of this article we changed our conosle colors to make them easier to read.
The PowerShell console application is your only option when you’re running PowerShell on a server that doesn’t have a GUI shell installed
- The console application is tiny. It loads fast and doesn’t use much memory.
- It doesn’t require any more .NET Framework stuff than PowerShell needs.
- You can set the colors to be green text on a black background and pretend you’re working on a 1970s-era mainframe.
If you decide to use the console application, we have a few suggestions for configuring it. You can make all of these configurations by clicking the window’s upper-left-corner control box and selecting Properties; you’ll see the dialog box in figure 1. This looks slightly different in Windows 10, as it’s gained some new options, but the gist is the same.
On the Options tab, you can increase the size of the Command History Buffer Size. This buffer enables the console to remember which commands you’ve typed, and lets you recall them by using the up and down arrows on your keyboard.
On the Font tab, pick something a bit larger than the default 12-point font. Please. We don’t care if you have 20/10 vision; increase the font size a bit. PowerShell needs you to be able to quickly distinguish between a lot of similar-looking characters — such as ‘ (an apostrophe or a single quote) and ` (a backtick or a grave accent) — and a tiny font doesn’t help.
On the Layout tab, set both Width sizes to the same number, and make sure the resulting window fits on your screen. Failing to do this can result in a horizontal scrollbar at the bottom of the window, which can lead to some PowerShell output appearing wrapped off the right side of the window, where you’ll never see it. We’ve had students spend half an hour running commands, thinking they were producing no output at all, when in fact the output was scrolled off to the right.
Finally, on the Colors tab, don’t go nuts. Keep things high contrast and easy to read, and if you want to you can set the colors to match your Windows PowerShell terminal.
One point to keep in mind: This console application isn’t PowerShell; it’s merely the means by which you interact with PowerShell.
PowerShell on macOS
If you’re using a Mac, this section is for you. We talk about how to install and run PowerShell specifically on macOS. This article assumes that you know how to open Terminal — macOS’s default terminal application. You can use a different terminal on macOS, but we’ll stick with the default . Okay, let’s install PowerShell!
Installation on macOS
Today, PowerShell doesn’t come included with macOS. Maybe one day it’ll happen, but until then we’ll have to install it ourselves. Thankfully, it’s easy to install and there are many ways to do it. We’ll cover the easiest way to install PowerShell for macOS; via Homebrew — the preferred package manager for macOS. Homebrew provides the ability to install PowerShell via a terminal without a single click of our mouse.
NOTE Homebrew also doesn’t come with macOS; if you don’t have it, you can head over to Homebrew’s website (https://brew.sh) for instructions on how to install it. Go ahead and get it. We’ll wait for you to get back!
Once you install Homebrew and it’s ready to go, you can install PowerShell. All you need is an instance of Terminal;
open it on your Mac. Using Homebrew, install PowerShell in one command:
brew cask install powershell
Type this command into Terminal
and hit ENTER
; you’ll see Homebrew install PowerShell.
Listing 1 Homebrew installing PowerShell
==> Satisfying dependencies
All Formula dependencies satisfied.
==>Downloading https://github.com/PowerShell/PowerShell/releases/download/v7.1.3/powershell-7.1.3-osx-x64.pkg
==> Verifying SHA-256 checksum for Cask 'powershell'.
==> 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.
installer: Package name is PowerShell - 7.1.3
installer: Installing at base path /
installer: The install was successful.powershell was successfully installed!
You’re all set! Let’s run it. What do we run? Great question. To run PowerShell, you must run the command pwsh
which starts PowerShell in your terminal. You should see the following output:
~ pwsh
PowerShell 7.1.3
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS /Users/steve>
We now have PowerShell running in Terminal.app on macOS! Well done. This is one of the main ways to interact with PowerShell on macOS. We’ll go over the other main way in a bit, but first we need to talk to those of you that are using Linux as your operating system.
PowerShell on Linux (Ubuntu 18.04)
This is the part of the article where we tell you how awesome PowerShell is that it’s able to run on an incredibly long list of Linux distributions. But, as we don’t have hundreds of pages, we’re just going to run through how to install PowerShell on Ubuntu 18.04 because it’s the latest LTS version of the one of the most popular Linux distributions. If you use a machine that runs something else, fear not! All the documentation on how to install PowerShell on all the different supported Linux distributions can be found in the PowerShell docs article on specifically this topic:
https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux
Alright now on to the installation. We should also mention that this article assumes you know how to open the Terminal application on Ubuntu 18.04. You can use any terminal for these steps, but we’ll stick to the default one.
Installation on Ubuntu 18.04
Ubuntu 18.04 ships with Canonical’s own package manager called snap
. This gives us a single command installation of PowerShell. First, go ahead and open up an instance of Terminal and type the following command:
snap install powershell –-classic
Once you do this, hit ENTER
to run it. You may be asked to put in your password; go ahead and put that in if this is the case. This is because snap
needs to be run as root
to install PowerShell. The output you should see should look like this:
PowerShell 7.1.3 from Microsoft PowerShell✓ installed
NOTE The reason we add the --classic
part to the command is because PowerShell’s considered a “classic snap package”. Classic snaps remove the restrictions on snap packages allowing PowerShell to fully interact with the operating system.
You’re all set! Let’s run it. What do we run? Great question. To run PowerShell, run the command pwsh
which starts PowerShell in your terminal. You should see the following output:
~ pwsh
PowerShell 7.1.3
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS /Users/tyleonha>
We now have PowerShell running in Terminal on Ubuntu 18.04! Well done. This is one of the main ways to interact with PowerShell on Ubuntu 18.04.
Is PowerShell for you?
PowerShell doesn’t try to be all things to all people. Microsoft’s PowerShell team loosely defines three audiences who use PowerShell:
- Administrators (regardless of your os) who primarily run commands and consume tools written by others.
- Administrators (regardless of your os) who combine commands and tools into more-complex processes, and perhaps package those as tools which less-experienced administrators can use.
- Administrators (regardless of your os) and developers who create reusable tools and applications.
Our book is primarily for the first audience. But we think it’s valuable for anyone, even a developer, to understand how PowerShell is used to run commands. After all, if you create your own tools and commands, you should know the patterns that PowerShell uses, as they allow you to make tools and commands that work as well as they can within PowerShell.
That’s all for this article. Thanks for reading.