Learn PowerShell in a Month of Lunches, Fourth Edition
eBook - ePub

Learn PowerShell in a Month of Lunches, Fourth Edition

Covers Windows, Linux, and macOS

Travis Plunk, James Petty, Tyler Leonhardt

Share book
  1. 360 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learn PowerShell in a Month of Lunches, Fourth Edition

Covers Windows, Linux, and macOS

Travis Plunk, James Petty, Tyler Leonhardt

Book details
Book preview
Table of contents
Citations

About This Book

Designed for busy IT professionals, this innovative guide will take you from the basics to PowerShell proficiency through 25 tutorials you can do in your lunch break. In Learn PowerShell in a Month of Lunches, Fourth Edition you will learn: Discoverability with the Help system
Background jobs and automation techniques
Simple scripting to automate repetitive tasks
Managing cloud services from major cloud providers
Extending PowerShell with commands
Common syntax and commands cheat sheet Learn PowerShell in a Month of Lunches, Fourth Edition is a task-focused guide for administering your systems using PowerShell. It covers core language features and admin tasks, with each chapter a mini-tutorial you can easily complete in under an hour. Discover how PowerShell works on different operating systems, and start automating tasks so they take just a few seconds to complete. No previous scripting experience required. The book is based on the bestselling Learn Windows PowerShell in a Month of Lunches by community legends Don Jones and Jeffery Hicks. PowerShell team members Travis Plunk and Tyler Leonhardt and Microsoft MVP James Petty have updated this edition to the latest version of PowerShell, including its multi-platform expansion into Linux and macOS. About the technology
PowerShell gives you complete command line control over admin tasks like adding users, exporting data, and file management. Whether you're writing one-liners or building complex scripts to manage cloud resources and CI/CD pipelines, PowerShell can handle the task. And now that PowerShell is truly cross-platform, you don't have to switch scripting languages when you move between Windows, Linux, and macOS. About the book
Learn PowerShell in a Month of Lunches, Fourth Edition is a new edition of the bestseller that introduced PowerShell to over 100, 000 readers. With bite-sized lessons and hands-on exercises, this amazing book guides you from your first command to writing and debugging reusable scripts for Windows, Linux, and macOS. Set aside just an hour a day and you'll soon be tackling increasingly complex automation tasks with PowerShell. What's inside Discoverability with the Help system
PowerShell on macOS and Linux
Background jobs and automation techniques
Managing cloud services from major cloud providers
Common syntax and commands cheat sheet About the reader
No previous experience with PowerShell or Bash required. About the author
James Petty is CEO of PowerShell.org and The DevOps Collective, and a Microsoft MVP. Travis Plunk is an engineer on the PowerShell team. Tyler Leonhardt is an engineer on Visual Studio Code. Don Jones and Jeffery Hicks are the original authors of Learn Windows PowerShell in a Month of Lunches.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Learn PowerShell in a Month of Lunches, Fourth Edition an online PDF/ePUB?
Yes, you can access Learn PowerShell in a Month of Lunches, Fourth Edition by Travis Plunk, James Petty, Tyler Leonhardt in PDF and/or ePUB format, as well as other popular books in Computer Science & System Administration. We have over one million books available in our catalogue for you to explore.

Information

Publisher
Manning
Year
2022
ISBN
9781638350613

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...

Table of contents