Foreword
In April 2003, Microsofts Jeffrey Snover gave me an early peek at PowerShell or, as it was known in its beta days, Monad. I must admit that, while I fell in love with PoSH at first sight, I was just too darned busy with other work to really get my hands dirty with it for another five years, and I soon realized that boy, had I missed a few memos. Objects in a pipeline? Is that anything like snakes on a plane? Hash tables? Can I get mine with a fried egg?
Yup, there was a lot to learn, and I nearly wore out Google looking up PoSH-y things. Just about every one of those searches, however, seemed to lead me to the same place: the Hey, Scripting Guy! Blog. I quickly noticed that the blog delivered new articles daily, and so I was very surprised to see that the vast majority of those articles were penned by one guy: Ed Wilson. Since then, Ive gotten to know Ed personally, and trust me, hes even funnier and more entertaining in person than he is in print, which brings me to this volume.
If youre a Windows admin, learning Windows PowerShell is an essential (as in you need to do this if you want to remain a Windows admin) task. Its not always an easy one, though, and you will often find yourself wishing for the answers in the back of the book so to speak. Well, Eds written that book, and youre holding the latest edition. Work your way through Windows PowerShell Best Practices , actually take the time to try out the examples, and soon you, too, will be automating, scripting, and workflow-ing like mad. Happy PowerShelling!
Mark Minasi, author of the Mastering Windows Server books
P.S. In case you dont already know, objects in a pipeline are way cooler than snakes on a plane. Really.
Introduction
Welcome to Windows PowerShell Best Practices , a book that was developed together with the Microsoft Windows PowerShell product group to provide in-depth information about Windows PowerShell and best practices based on real-life experiences with the product in use in different environments. Numerous sidebars are also included that detail experiences from skilled industry professionals such as Enterprise Admins and Windows PowerShell Most Valuable Professionals (MVPs).
The book is largely based on Windows PowerShell 4.0 as it exists on Windows 8.1 and on Windows Server 2012 R2. Because Windows PowerShell introduced Desired State Configuration in Windows PowerShell 4.0, , must be run on a computer with Windows PowerShell 4.0 installed on it. Nearly all of the material in the other chapters will work without modification on Windows PowerShell 3.0 (on Windows 8 or on Windows Server 2012). A large part of the book also applies to Windows PowerShell 2.0 running on any version of Windows that it installs upon.
Who is this book for?
Microsoft Windows PowerShell Best Practices is for anyone tasked with designing, implementing or managing enterprise products. This includes Active Directory Domain Services, System Center, Exchange, and SharePoint products. In addition, it is designed for anyone who either teaches or trains others on Windows PowerShell or even for the MCSE track of courseware. Lastly, power users who want to automate their desktops will also benefit from the explanations, scenarios, and sample scripts.
How is this book organized?
This book is organized into four parts:
The first part of this book consists of two chapters that focus on the basics of Windows PowerShell capabilities. This portion of the book is a level setting and would be ideal for anyone just learning Windows PowerShell.
The second part of the book discusses identifying scripting opportunities, the scripting environment, and avoiding scripting pitfalls. This part is also ideal for people learning Windows PowerShell, but it is also a great section for admins experienced with the fundamentals of Windows PowerShell but who need to write new scripts.
The third section of the book talks about how you actually design a scripthow you plan for inputs and outputs to the script and how you document your scripts. This is a more advanced section, and it is appropriate for advanced students and for people who write scripts that others are expected to utilize.
The last section of the book talks about deploying scriptshow you run them; how you handle versioning; and how you use remote, workflow, and DSC capabilities in your script. This is appropriate for enterprise admins who are firmly entrenched in DevOps.
System requirements
This book is designed to be used with the following Exchange 2010 software:
Windows Server 2008 or Windows Server 2008 R2
1 GB of RAM
x64 architecture-based computer with Intel or AMD processor that supports 64 bit
1.2 GB of available disk space
Display monitor capable of 800 600 resolution
The following list details the minimum system requirements needed to run the content in the books companion website:
Windows XP with the latest service pack installed and the latest updates from Microsoft Update Service
Display monitor capable of 1024 768 resolution
CD-ROM drive
Microsoft mouse or compatible pointing device
The companion website
This book features a companion website that makes available to you additional information such as job aids, quick reference guides, and additional Windows PowerShell resources. These elements are included to help you plan and manage your Windows PowerShell organization and apply the books recommended best practices. The companion website includes the following:
Job Aids . Additional documents on most of the chapters that help you to collect and structure your work through the book.
Quick Reference Guides . These guides provide an overview of all best practice recommendations in the book as well as a collection of all Internet links referenced in the book.
You can download these files from the companion website, which is located at http://gallery.technet.microsoft.com/scriptcenter/PowerShell-40-Best-d9e16039 .
Acknowledgements
A book of this scope does not happen without assistance. First I must thank my wife, Teresa Wilson, aka the Scripting Wife. She not only coordinated the acquisition of sidebars, but she also read the entire book at least three times. My technical reviewer, Microsoft PFE Brian Wilhite, was great at catching things that would have made me look silly. He also made numerous suggestions for improving not only the clarity of the writing, but in some cases the accuracy of the code. Brian absolutely rocks. Luckily, the Windows PowerShell community is very enthusiastic and as a result was receptive for my call for sidebars. The high quality of the sidebars, and the diversity of content was fun to read, and in the end makes for a much better book. If you run across one of the authors of the sidebars, make sure you tell them hi. Lastly, I want to thank Jeffrey Snover, Ken Hansen and the rest of the Windows PowerShell team. They made an awesome product that just keeps getting better and better each year. Windows PowerShell for the win!
Support & feedback
The following sections provide information on errata, book support, feedback, and contact information.