1 Before you begin
PowerShell just turned 15 years old (on November 14, 2021). Itâs hard to believe itâs been around this long, but there is still a large number of IT folks who havenât used it yet. We get itâthere is only so much time in the day, and you are already familiar with doing things the way you always have. Or maybe your cybersecurity officer will not let you turn on PowerShell because it can only be used by the bad guys. Either way, we are glad you could join us on our adventure. We have been using PowerShell for a long time. In fact, two of us, James and Tyler, actually learned PowerShell from earlier editions of this very book.
There was a huge shift in the industry around 2009 when a new concept was realized about PowerShell. It isnât a scripting language, nor is it a programming language, so the way we teach PowerShell needed to change as well. PowerShell is actually 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.
The previous editions of this book were the result of that culture shift, and we keep that same mindset here today. Itâs the best that weâve yet devised to teach PowerShell to someone who might not have a scripting background (although it certainly doesnât hurt if you do). But before we jump into the instruction, letâs set the stage for you.
1.1 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âwhen you do, 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.
1.1.1 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 whole 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. Do you remember the first time you opened Active Directory Users and Computers? Maybe you 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 5 minutes to create a new user in Active Directory (and assuming youâre filling in a lot of the fields, thatâs a reasonable estimate), youâll never get any faster than that. One hundred users will take 500 minutesâthereâs no way, short of learning to type and click faster, to make the process go any quicker.
Microsoft has 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? Okay, 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 so far. Dozens of product groups within Microsoft have adopted PowerShell, an extensive ecosystem of third parties depends on it, and a global community of experts and enthusiasts are pushing the PowerShell envelope every day.
1.1.2 Life with PowerShell
Microsoftâs goal for Windows PowerShell is to build 100% of a productâs administrative functionality in PowerShell. Microsoft continues to build GUI consoles, but those consoles are executing 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 have already 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 Amazon Web Services (AWS) and VMware, have taken a keen interest in PowerShell as well.
Windows Server 2012, which was where PowerShell v3 was introduced, and higher are almost completely managed from PowerShellâor by a GUI sitting atop PowerShell. Thatâs why you canât afford to ignore PowerShell: Over the last few years, PowerShell has become 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 I were in charge of a team of IT administrators (and perhaps you are), who would I want in my 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 will start to split into two groups: administrators who can use PowerShell and those who canât. Our favorite quote from Don Gannon-Jones at Microsoftâs TechEd 2010 conference is, âYour choice is Learn PowerShell, or Would you like fries with that?â We are glad you decided to make the plunge and learn PowerShell with us!
1.2 Windows, Linux, and macOS, oh my
In mid-2016, Microsoft made the unprecedented decision to open source PowerShell Version 6 (then known as 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. So for this edition of the book, we have done our best to demonstrate the multiplatform use of PowerShell and included examples for macOS and Linux environments as well. We still feel that PowerShellâs biggest audience will be Windows users, but we also want to make sure you understand how it works on other operating systems.
We have done our best to make everything in this book cross-platform compatible. However, as of the writing of this book, there are just over 200 commands available for Linux and macOS, so not everything we wanted to show you will work. With that in mind, we want to call out chapters 19 and 20 in particular, as they are 100% Windows focused.
1.3 Is this book for you?
This book doesnât try to be all things to all people. Microsoftâs PowerShell team loosely defines three audiences who use PowerShell:
-
Administrators (regardless of OS) who primarily run commands and consume tools written by others
-
Administrators (regardless of OS) who combine commands and tools into more-complex processes, and perhaps package those as tools that less-experienced administrators can use
-
Administrators (regardless of OS) and developers who create reusable tools and applications
This book is designed primarily for the first audience. We think itâs valuable for anyone, even a developer, to understand how PowerShell is used to run commands. After all, if youâre going to 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.
If youâre interested in creating scripts to automate complex processes, such as new user provisioning, then youâll see how to do that by the end of this book. Youâll even see how to get started on creating your own commands that other administrators can use. But this book wonât probe the depths of everything that PowerShell can possibly do. Our goal is to get you using PowerShell and being effective with it in a production environment.
Weâll also show you a couple of ways to use PowerShell to connect to external management technologies; remoting and interacting with Common Information Model (CIM) classes and regular expressions are two examples that come quickly to mind. For the most part, weâre going to introduce only those technologies and focus on how PowerShell connects to them. Those topics deserve their own books (and have them), so we concentrate solely on the PowerShell side of things. Weâll provide suggestions for further exploration if youâd like to pursue those technologies on your own. In short, this book isnât meant to be the last thing you use to learn about PowerShell, but instead is designed to be a great first step.
1.4 How to use this book
The idea behind this book is that youâll read one chapter each day. You donât have to read it during lunch, but each chapter should take you only about 40 minutes to read, giving you an extra 20 minutes to gobble down the rest of your sandwich and practice what the chapter showed you.
1.4.1 The chapters
Of the chapters in this book, chapters 2 through 26 contain the main content, giving you 25 daysâ worth of lunches to look forward to. You can expect to complete the main content of the book in about a month. Try to stick with that schedule as much as possible, and donât feel the need to read extra chapters in a given day. Itâs more important that you spend some time practicing what each chapter shows you, because using PowerShell will help cemen...