PowerShell Cookbook
by Lee Holmes
Copyright 2021 Lee Holmes. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Suzanne McQuade
- Development Editor: Angela Rufino
- Production Editor: Kate Galloway
- Copyeditor: Stephanie English
- Proofreader: James Fraleigh
- Indexer: Judith McConville
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- October 2007: First Edition
- August 2010: Second Edition
- January 2013: Third Edition
- June 2021: Fourth Edition
Revision History for the Fourth Edition
- 2021-06-16: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781098101602 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. PowerShell Cookbook, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-098-10160-2
[LSI]
Foreword
Welcome to the fourth edition of the Windows PowerShell Cookbook!
Ooops. I got that wrong. Let me try again.
Welcome to the fourth edition of the Windows PowerShell Cookbook!
The name change says it all. Just as the Windows PowerShell Cookbook deserved a place on the desk of every Windows admin, the PowerShell Cookbook deserves a place on desk of every admin.
The PowerShell team always focused on giving admins the tools needed to become the heroes of their company. But the team worked for Microsoft, and as former Microsoft CEO Steve Ballmer used to say, Windows is the air we breathe. Thats why the first three editions of this book were titled the Windows PowerShell Cookbook.
From the very beginning, the team wanted to support Linux. We knew that fragmented technologies produced fragmented organizations. Instead of having an admin team, companies organized into siloed Windows admin teams and Linux admin teams. We wanted to deliver a single tool for all admins, regardless of platform (Windows or Linux), regardless of skill level (simple interactive user, first-time scripter, advanced systems developers), and regardless of what they managed (Azure, AWS, Google, VMware, etc.). But our ambition was gated by our .NET dependency. Everything changed when .NET started porting to Linux. The first version of .NET Core was cross-platform, and we ported to it as soon as possible. The result was PowerShell Core v6, which ran on both Windows and Linux. The industry took notice. Our launch partners included VMware, Google, and AWS: not your typical set of Microsoft partners.
PowerShell Core was great for Linux, but the small number of .NET Core libraries meant that it was less capable than Windows PowerShell v5 in several important areas. Windows users were faced with a choice: Windows PowerShell or PowerShell Core. All that changed in 2020 with the v7 release of PowerShell. That was built upon .NET Core 3.1, which dramatically increased compatibility on Windows. That combined with substantial work on our part produced a no-compromise version. We changed the name, dropping both Windows and Core. With version 7, theres just PowerShellthe single tool for all teams to manage anything thats in their environment .
And just as Windows PowerShell evolved to better meet the needs of admins who want to manage anything, so too, this Cookbook has evolved to better meet the needs of those same admins. This is a major edition of the book with more than 30,000 deletions and more than 35,000 additions. Lee starts the book with , a drill-in on eight key PowerShell concepts. With this foundation, youre ready to solve some problems. The next 10 sections are focused on common tasks like code reuse, debugging, tracing, and error management. I like to joke that there is solving a problem and there is solving a problem in a way you dont regret a month later. These sections teach you the latter. These are the hard-earned lessons of how to write no-regrets PowerShell for production environments. The next 12 sections cover specific administrator tasks like dealing with files and directories, the Windows registry, active directory, and remoting.
While many of these topics were covered in previous editions, this edition brings them up-to-date with the latest and greatest tools in the PowerShell ecosystem including the Windows Terminal, Visual Studio Code, and SSH, and the lessons and perspective that can only be earned through a couple decades of in-the-trench experiences. As the saying goes, A wise man doesnt learn from his mistakes. A wise man learns from the mistakes of others. So you can spend the next two decades learning from your own mistakes, or you can read this book and learn from Lee, who has been a superstar on the team since the day he joined the original v1 team.
The thing I love the most about the PowerShell Cookbook is that it teaches the reader how to think about problems. Yes, there are hundreds of pages of solutions to specific problems and that alone would make this book a must-have for every admin. But Lee has a teach a person to fish mindset, and each of his solutions sets you up to solve SETs of problems.
So how does one approach a book that has more than a thousand pages?
Certainly, many will get a lot from reading , and then hopping to a particular section when a problem arises.
I think a better approach for beginners is grounded in Lev Vygotskys activity theory. Vygotsky identified the distinction between competence and performance. He pointed out that our performance can exceed our competence when we are being directed by an expert. Imagine the case where I want to find all the files modified before a certain date but dont know any PowerShellI am not (yet) competent to perform this task. But because I was smart enough to buy this book, all I do is open to . There I see the solution. I type the commands and I am performing a very sophisticated task. Lee (expert) helps me solve a complex problem (perform) even though I dont know PowerShell (competence).
As awesome as that is, the magic occurs with the next step. Now that Im able to successfully perform a complex task, I can now experiment, and in experimenting, I establish and grow my competence.
I run the commands. Then I intentionally make a mistake and see what the error message is. Next time I see that error message, I now know what mistake I made. From here, I look up the commands with