An excerpt from Learn PowerShell Scripting in a Month of Lunches, Second Edition by James Petty, Don Jones and Jeffery Hicks

This excerpt describes toolmaking in PowerShell and why you should learn it.

Read on if you’re interested in learning PowerShell or improving your existing PowerShell skill set.


 

A lot of people jump into PowerShell scripting much the same way they’d jump into batch files, VBScript, Python, and so on. Nothing wrong with that. PowerShell is able to accommodate a lot of different styles and approaches. But you end up working harder than you need to unless you take a minute to understand how PowerShell really wants to work. With time and a bit of persistence, I think you’ll come to see that toolmaking is the real way to use PowerShell.

PowerShell has a strong ability to create highly reusable, context-independent tools, which it refers to as commands. Commands typically do one small thing, and they do it very well. A command might not be terribly useful by itself, but PowerShell is designed to make it easy to “snap” commands together. A single LEGO brick might not be much fun (if you’ve ever stepped on one in bare feet, you know what I mean), but a box of those bricks, when snapped together, can be amazing (hello, Death Star!). That’s the approach we take to scripting, and it’s why we use the word toolmaking to describe that approach. We believe that your effort is best spent making small, self-contained tools that can “snap on” to other tools. This approach makes your code usable across more situations, which saves you work. This approach also reduces debugging and maintenance overhead, which saves your sanity. And it’s the approach taken in this book.

What this book will teach you

This book teaches you to create effective automation scripts and tools with PowerShell. Its 27 bite-size lessons cover instantly useful techniques for writing efficient code, finding and squashing bugs, organizing your scripts into libraries, and much more. This revised second edition of the bestselling original has been fully upgraded to the latest version of PowerShell. It’s full of exciting new examples that go beyond Windows scripting and show you how you can use PowerShell to automate Linux, and macOS environments.

Is this book for you?

Before you go any further, you should make sure this is the right place for you. This book is an entry-level book on PowerShell scripting, but because we focus as much on process and approach as on the syntax, it’s fine if you’ve already been scripting for a while and are just looking to improve your technique or validate your skillset. That said, this isn’t an entry-level book on PowerShell itself. If you’re going to continue successfully with this book, you should be able to answer the following right off the top of your head:

    1. What command would you use to query all instances of Win32_LogicalDisk from a remote computer? (Hint: if you answered Get-WmiObject, you’re behind the times and need to catch up if this book is going to be useful for you.)
    2. How would you figure out how to use the Get-WinEvent command if you had never used it before?
    3. Well-written PowerShell commands don’t output text. What do they output? What commands can you use to make that output prettier on the screen?
    4. How would you figure out how to use the Get-WinEvent command if you had never used it before?
    5. What are the different shell execution policies, and what does each one mean?

You won’t find answers to these questions here—if you’re unsure of any of them, then this isn’t the right book for you. Instead, we’d recommend Learn Windows PowerShell in a Month of Lunches from Manning. Once you’ve worked your way through that book and its many hands-on exercises, this book will be a logical next step in your PowerShell education.

We also assume that you’re pretty experienced with the Windows operating system because our examples will pertain to that.

Here’s what you need to have

Let’s quickly run down some of what you’ll need to have to follow along with this book.

PowerShell version

This book is written using PowerShell 7.2 but 99% of the book also applies to earlier versions of Windows PowerShell. Download PowerShell from https://docs.microsoft.com/en-us/powershell/. Now, look: Don’t go installing new versions of PowerShell on your server computers without doing some research. Many server applications (we’re looking at you, Exchange Server) are picky about which version of PowerShell they’ll work with, and installing the wrong one can break things. Also, be aware that each version of PowerShell supports only specific versions of Windows—For this book, we are using Windows 11 and macOS.

We are using PowerSHell 7.2 (or higher as the newer version comes out), but most of the content will work on Windows PowerShell (5.1), although we haven’t tested everything against that version. The content we’re covering is so core to PowerShell, so stable, and so mature that it’s essentially evergreen, meaning it doesn’t really change from season to season. We use free e-books on PowerShell.org to help teach the of-the-moment, new-and-shiny stuff that relates to a specific version of PowerShell; this book is all about the solid core that remains stable.

Administrative privileges

You need to be able to run the PowerShell console, and your editor “as Administrator” on your computer, mainly so that the administrative examples we’re sharing with you will work. If you don’t know how to run PowerShell as an Administrator of your computer, this probably isn’t the right book for you.

Script editor

Finally, you’ll need a script editor. Windows PowerShell’s Integrated Script Editor (ISE) is included on client versions of Windows and only works with Windows PowerShell. We recommend you remove this from your machine to begin with, as the PowerShell team has not put any maintenance or support into it since Windows 7 was released. These days, Microsoft recommends Visual Studio Code (VS Code), which is free and cross-platform. You can get it at https://code.visualstudio.com/.

NOTE Visual Studio Code and PowerShell are both cross-platform. Every single concept and practice in this book applies to PowerShell running on systems other than Windows.

Expectations

As you might imagine, the book’s topic is pretty big, and there’s a lot of material that could be covered. But this book is designed for you to complete in a month of lunches, so we had to draw the line somewhere. Our goal is to provide you with fundamental information that we think everyone should have in order to start scripting and creating basic PowerShell tools. This book was never intended as an all-inclusive tutorial.

Summary

Hopefully, at this point, you’re eager to dive in and start scripting—or, better yet, to start toolmaking. You should have your prerequisite software lined up and ready to go, and you should have a good idea of how much time you want to devote to this book each week (about 1 hour per day).

 

You can learn more about the book here.