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 cement what you’ve learned. Not every chapter requires a full hour, so sometimes you’ll be able to spend additional time practicing (and eating lunch) before you have to get back to work. We find that a lot of people learn more quickly when they stick with just one chapter a day, because it gives your brain time to mull over the new ideas and gives you time to practice them on your own. Don’t rush it, and you may find yourself moving more quickly than you thought possible. Chapter 27 provides ideas for where to go next on your PowerShell journey. Finally, we include the appendix, “PowerShell cheat sheet,” which is a compilation of all the “gotchas” we mention throughout the body of the book; use this as a reference when you want to find something but you can’t remember where to look.

1.4.2 Hands-on labs

Most of the main content chapters include a short lab for you to complete. You’ll be given instructions, and perhaps a hint or two. The answers for these labs appear at the end of each chapter. But try your best to complete each lab without looking at the answers.

1.4.3 Supplementary materials

We have one video made with this book in mind: Tyler’s “How to navigate the help system in PowerShell”; it’s in Manning’s free content center (http://mng.bz/enYP).

We also suggest PowerShell.org, run by James, and its YouTube channel, YouTube.com/powershellorg, which contains a ton of video content. You’ll find recorded sessions from the PowerShell + DevOps Global Summit events, online community webinars, and a lot more. All free!

1.4.4 Further exploration

A few chapters in this book only skim the surface of some cool technologies, and we end those chapters with suggestions for exploring those technologies on your own. We point out additional resources, including free stuff that you can use to expand your skill set as the need arises.

1.4.5 Above and beyond

As we learned PowerShell, we often wanted to go off on a tangent and explore why something worked the way it did. We didn’t learn many extra practical skills that way, but we did gain a deeper understanding of what PowerShell is and how it works. We’ve included some of that tangential information throughout the book in sections labeled “Above and beyond.” None of those will take you more than a couple of minutes or so to read, but if you’re the type of person who likes to know why something works the way it does, they can provide some fun additional facts. If you feel those sections might distract you from the practical stuff, ignore them on your first read-through. You can always come back and explore them later, after you’ve mastered the chapter’s main material.

1.5 Setting up your lab environment

You’re going to be doing a lot of practicing in PowerShell throughout this book, and you’ll want to have a lab environment to work in. Please don’t practice in your company’s production environment.

All you’ll need to run most of the examples in this book—and to complete all of the labs—is a copy of Windows that has PowerShell 7.1 or later installed. We suggest Windows 10 or later, or Windows Server 2016 or later, both of which come with PowerShell v5.1. If you’re going to play with PowerShell, you’ll have to invest in a version of Windows that has it. For most of the labs, we included additional instructions for your Linux environment.

NOTE You have to download and install PowerShell 7 separately, as it runs side by side with Windows PowerShell 5.1, which comes preinstalled. However, most of these labs will run in Windows PowerShell. Instructions on how to install PowerShell 7 can be found at http://mng.bz/p2R2.

We will also be using Visual Studio Code (VS Code) with the latest stable release of the PowerShell extension, which can be installed from the marketplace. If you’re using a non-Windows build of PowerShell, you’ll have fewer options to worry about. Just get the right build for your version of macOS or Linux (or whatever) from http://github.com/PowerShell/PowerShell, and you should be good to go. Keep in mind, however, that a lot of the functionality we’ll be using in our examples is unique to Windows. For example, you can’t get a list of services on Linux, because Linux doesn’t have services (it has daemons, which are similar), but we will do our best to use examples that are cross-platform (such as Get-Process).

Tip You should be able to accomplish everything in this book with a single computer running PowerShell, although some stuff gets more interesting if you have two or three computers, all in the same domain, to play with.

1.6 Installing PowerShell

If you don’t have PowerShell 7 installed right now, it’s okay. We’ll go over how to do that in the next chapter. If you want to check the latest available version of PowerShell or download it, go to https://docs.microsoft.com/en-us/powershell. This official PowerShell home page has links to the latest releases and how to install them.

Tip You should check your version of PowerShell: open the PowerShell console, type $PSVersionTable, and press Enter.

Before you go any further, take a few minutes to customize PowerShell. If you’re using the text-based console host, we strongly recommend that you change the default console font to the Lucida fixed-width font. The default font makes it difficult to distinguish some of the special punctuation characters that PowerShell uses. Follow these steps to customize the font:

  1. Click the control box (that’s the PowerShell icon in the upper left of the console window) and select Properties from the menu.

  2. In the dialog box that appears, browse through the various tabs to change the font, window colors, window size and position, and so forth.

Tip Make sure that both the window size and screen buffer have the same width values.

Your changes will apply to the default console, meaning they’ll stick around when you open new windows. Of course, all of this applies only to Windows: On non-Windows operating systems, you’ll usually install PowerShell, open your operating system’s command line (e.g., a Bash shell), and run powershell. Your console window will determine your colors, screen layout, and so on, so adjust to suit your preferences.

1.7 Contacting us

We’re passionate about helping folks like you learn Windows PowerShell, and we try to provide as many resources as we can. We also appreciate your feedback, because that helps us come up with ideas for new resources that we can add to the site and ways to improve future editions of this book. On Twitter, you can reach Travis at @TravisPlunk, Tyler at @TylerLeonhardt, and James at @PsJamesP. We also hang out in the forums of https://forums.powershell.org if you have PowerShell questions. Another wonderful place for more resources is https://powershell.org, which includes free e-books, in-person conferences, free webinars, and tons more. James helps run the organization, and we can’t recommend it highly enough as a place to continue your PowerShell education after you’ve finished this book.

1.8 Being immediately effective with PowerShell

Immediately effective is a phrase we’ve made our primary goal for this entire book. As much as possible, each chapter focuses on something that you could use in a real production environment, right away. That means we sometimes gloss over some details in the beginning, but when necessary we promise to circle back and cover those details at the right time. In many cases, we had to choose between hitting you with 20 pages of theory first, or diving right in and accomplishing something without explaining all the nuances, caveats, and details. When those choices came along, we almost always chose to dive right in, with the goal of making you immediately effective. But all of those important details and nuances are still explained later in the book.

Okay, that’s enough background. It’s time to start being immediately effective. Your first lunch lesson awaits.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.146.61