Preface

In late 2002, Slashdot posted a story about a “next generation shell” rumored to be in development at Microsoft. As a longtime fan of the power unlocked by shells and their scripting languages, the post immediately captured my interest. Could this shell possibly provide the command-line power and productivity that I’d long loved on Unix systems?

Since I had just joined Microsoft six months earlier, I jumped at the chance to finally get to the bottom of a Slashdot-sourced Microsoft Mystery. The post talked about strong integration with the .NET Framework, so I posted a query to an internal C# mailing list. I got a response that the project was called “Monad,” which I then used to track down an internal prototype build.

Prototype was a generous term. In its early stages, the build was primarily a proof of concept. Want to clear the screen? No problem! Just lean on the Enter key until your previous commands and output scroll out of view! But even at these early stages, it was immediately clear that Monad marked a revolution in command-line shells. As with many things of this magnitude, its beauty was self-evident. Monad passed full-fidelity .NET objects between its commands. For even the most complex commands, Monad abolished the (until now, standard) need for fragile text-based parsing. Simple and powerful data manipulation tools supported this new model, creating a shell both powerful, and easy to use.

I joined the Monad development team shortly after that to help do my part to bring this masterpiece of technology to the rest of the world. Since then, Monad has grown to become a real, tangible, product—now called Windows PowerShell.

So why write a book about it? And why this book?

Many users have picked up (and will continue to pick up) PowerShell for the sake of learning PowerShell. Any tangible benefits come by way of side effect. For others, though, you might prefer to opportunistically learn a new technology as it solves your needs. How do you use PowerShell to navigate the filesystem? How can you manage files and folders? Retrieve a web page?

This book focuses squarely on helping you learn PowerShell through task-based solutions to your most pressing problems. Read a recipe, read a chapter, or read the entire book—either way, you’re bound to learn something.

Who This Book Is For

This book helps you use PowerShell to get things done. It contains hundreds of solutions to specific, real-world problems. For systems management, you’ll find plenty examples that show how to manage the filesystem, Windows Registry, event logs, processes, and more. For enterprise administration, you’ll find two entire chapters devoted to WMI, Active Directory, and other enterprise-focused tasks.

For administrators of Exchange 2007 or Operations Manager 2007 (MOM), you’ll find a chapter devoted to each that covers the getting started information and top tasks for those groundbreaking new products.

Along the way, you’ll also learn an enormous amount about PowerShell: its features, its commands, and its scripting language—but you’ll most importantly solve problems.

How This Book Is Organized

This book consists of five main sections: a guided tour of PowerShell, PowerShell fundamentals, common tasks, administrator tasks, and a detailed reference.

Part I: Tour

A Guided Tour of Windows PowerShell breezes through PowerShell at a high level. It introduces PowerShell’s core features:

  • An interactive shell

  • A new command model

  • An object-based pipeline

  • A razor-sharp focus on administrators

  • A consistent model for learning and discovery

  • Ubiquitous scripting

  • Integration with critical management technologies

  • A consistent model for interacting with data stores

The guided tour lets you orient yourself and become familiar with PowerShell as a whole. This familiarity helps create a mental framework for you to understand the details and solutions from the rest of the book.

Part II: Fundamentals

Chapter 1, Chapter 2, Chapter 3, Chapter 4,Chapter 5 through Chapter 6 cover the PowerShell fundamentals that underpin many of the solutions used throughout the book. The solutions in this section introduce you to the PowerShell interactive shell, fundamental pipeline and object concepts, and many features of the PowerShell scripting language.

Part III: Common Tasks

Chapter 7, Chapter 8, Chapter 9, Chapter 10, Chapter 11, Chapter 12, Chapter 13, Chapter 14, Chapter 15 through Chapter 16 cover the tasks you will run into most commonly when starting to tackle more complex problems in PowerShell. This includes working with simple and structured files, Internet-connected scripts, code reuse, user interaction, and more.

Part IV: Administrator Tasks

Chapter 17, Chapter 18, Chapter 19, Chapter 20, Chapter 21, Chapter 22, Chapter 23, Chapter 24, Chapter 25 through Chapter 26 focus on the most common tasks in systems and enterprise management. Chapter 17, Chapter 18, Chapter 19, Chapter 20, Chapter 21 through Chapter 22 focus on individual systems: the filesystem, registry, event logs, processes, services, and more. Chapter 23 and Chapter 24 focus on Active Directory, as well as the typical tasks most common in managing networked or domain-joined systems.

Chapter 25 and Chapter 26 are devoted to managing Exchange 2007 and Operations Manager 2007 (MOM), respectively.

Part V: References

Many books belch useless information into their appendix simply to increase page count. In this book, however, the detailed reference underpins an integral and essential resource for learning and using PowerShell. It covers:

  • The PowerShell language and environment

  • Regular expression syntax and PowerShell-focused examples

  • PowerShell’s automatic and default variables

  • PowerShell’s standard verbs

  • Administrator-friendly .NET classes and their uses

  • Administrator-friendly WMI classes and their uses

  • Administrator-friendly COM objects and their uses

  • .NET string formatting syntax and PowerShell-focused examples

  • .NET DateTime formatting syntax and PowerShell-focused examples

What You Need to Use This Book

The majority of this book requires only a working installation of Windows Power-Shell. If you do not yet have PowerShell installed, you may obtain it by following the download link at http://www.microsoft.com/PowerShell. This link provides download instructions for PowerShell on Windows XP, Windows Server 2003, and Windows Vista. For Windows Server 2008, PowerShell comes installed as an optional component that you can enable through the Control Panel like other optional components.

The Active Directory scripts given in “Active Directory” are most useful when applied to an enterprise environment, but Recipe 23.1, “Test Active Directory Scripts on a Local Installation” shows how to install additional software (Active Directory Application Mode) that lets you run these scripts against a local installation.

Chapter 26 and Chapter 27 require that you have access to an Exchange or Operations Manager 2007 environment. If you do not have access to these environments, Recipe 25.1, “Experiment with Exchange Management Shell” and Recipe 26.1, “Experiment with the Command Shell” show you how to use Microsoft Virtual Labs for Exchange and Operations Manager as a viable alternative.

Conventions Used in This Book

The following typographical conventions are used in this book:

Plain text

Indicates menu titles, menu options, menu buttons, and keyboard accelerators (such as Alt and Ctrl)

Italic

Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities

Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands

Constant width bold

Shows commands or other text that should be typed literally by the user

Constant width italic

Shows text that should be replaced with user-supplied values

Tip

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Code Examples

Obtaining Code Examples

To obtain electronic versions of the programs and examples given in this book, visit the Examples link at:

http://www.oreilly.com/catalog/9780596528492

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Windows PowerShell Cookbook by Lee Holmes. Copyright 2007 Lee Holmes, 978-0-596-52849-2.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Comments and Questions

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/9780596528492

To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our web site at:

http://www.oreilly.com

Acknowledgments

"I do not like writing, but I do like having written.”

—William Zinsser

On Writing Well

Writing is the task of crafting icebergs. The heft of the book you hold in your hands is just a hint of the effort it took to get it there—by a cast much larger than me.

The groundwork started decades ago. My parents nurtured my interest in computers and software, supported an evening-only bulletin board service, put up with “viruses” that told them to buy a new computer for Christmas, and even listened to me blather about batch files or how PowerShell compares to Excel. Without their support, who knows where I’d be.

My family and friends helped keep me sane for the past year. Ariel: you are the light of my life. Robin: thinking of you reminds me each day that serendipity is still alive and well in this busy world. Thank you to all of my friends and family for being there for me. You can have me back now. :)

I would not have written this book without the tremendous influence of Guy Allen, visionary of University of Toronto’s Professional Writing program. Guy: your mentoring forever changed me, just as it molds thousands of others from English hackers into writers.

Of course, members of the PowerShell team (both new and old) are the ones that made this a book about PowerShell. Building this product with you has been a unique challenge and experience—but most of all, a distinct pleasure. In addition to the PowerShell team, the entire PowerShell community defined this book’s focus. From MVPs, to early adopters, to newsgroup lurkers: your support, questions, and feedback have been the inspiration behind each page.

Converting thoughts into print always involves a cast of unsung heroes, even though each author tries their best to convince the world how important these heroes are.

Thank you to my technical reviewers: Christina Lemaire, Dean Tsaltas, Debbie Timmins, James Manning, Jeffrey Tadlock, June Blender, Markus Lindemann, Michael Dragone, and Michael Howard. I truly appreciate you donating your nights and weekends to help craft something of which we can all be proud.

To the awesome staff at O’Reilly—John Osborn, Laurel Ruma, Kyley Caldwell, and the production team—your patience and persistence helped craft a book that holds true to its original vision. It also ensured that the book didn’t just knock around in my head, but actually got out the door.

This book would not be possible without the support from each and every one of you.

Get Windows PowerShell Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.