Windows PowerShell is the next-generation shell and scripting environment created by Microsoft. It’s intended to fix the weaknesses in existing Windows command-line and scripting solutions. The driving force behind its creation was the need to address the problems in automating Windows system management. Windows lacked the ad hoc management capabilities found in many competing systems. With PowerShell’s comprehensive support for .NET, it now has broad application potential beyond the system administration space. PowerShell can be used for text processing, general scripting, build management, creating test frameworks, and so on.
This book is written by one of principal creators of PowerShell. It uses many examples, both small and large, to illustrate the features of the language and its environment and shows how to compose those features into solutions, quickly and effectively.
Because of the broad focus of the PowerShell product, the book has a commensurately broad focus. It was not designed as a cookbook of preconstructed management examples, such as how to deal with Active Directory or how to script Exchange. Instead, it provides information about the core of the PowerShell runtime and how to compose solutions in the “PowerShell Way.” After reading this book, the reader should be able to take any example written in other languages, such as C# or Visual Basic, and leverage those examples to build solutions in PowerShell. (To facilitate this, appendix A in the book includes a number of discussions about the differences between PowerShell and other languages.)
The other thing this book doesn’t cover is the PowerShell SDK. PowerShell is both a hostable (that is, it can be embedded in other applications) as well as an extensible environment. This book doesn’t address these topics. Instead, it focuses on the shell user and scripter. (Though we do explain how to load and use .NET assemblies from a PowerShell script in chapter 11.)
This book is designed for anyone who wants to learn PowerShell and use it well. Rather than being a book of recipes, this book tries to give the reader deep knowledge about how PowerShell works and how to apply it. All users of PowerShell will find this book beneficial and useful.
So, if you’re a Windows sysadmin, this book is for you. If you’re a developer and you need to get things done in a hurry, if you're interested in .NET, or just if you like to experiment with computers, PowerShell is for you—and this book is for you!
The book is divided into two and a half parts. The two major parts of the book are “Learning PowerShell” and “Using PowerShell,” described below. The “half” part is primarily appendix B which contains examples showing how to use PowerShell to solve system administration problems. In appendix A we provide comparisons between PowerShell and other languages. Finally, in appendix C we present the grammar for the PowerShell language.
Part 1 “Learning PowerShell” is a comprehensive tour of the PowerShell language and runtime. The goal is to introduce new PowerShell users to the language as well as to provide experienced users with deep insight into how and why things are the way they are.
In part 1, we look at all aspects of the PowerShell language, including the syntax, the type system, and so on. Along the way, we’ll present examples showing how each feature works. Since the goal of the first part of the book is to focus on the individual features of the environment, most of the examples are small and are intended to be entered in an interactive session. The second part of this book focuses on larger examples that bring the individual features together to build larger applications.
Chapter 1 begins with some history and the rationale for why PowerShell was created in the first place, followed by a quick tour of the features of the environment. The remaining chapters in part 1 cover each element of the language, starting with basic PowerShell concepts in chapter 2.
Chapter 3 introduces the PowerShell type system and its relationship to .NET. This chapter also presents the syntax for each of the PowerShell literal data types.
The discussion of operators and expressions (PowerShell has lots of these) begins in chapter 4, which covers the basic arithmetic, comparison, and assignment operators. It also covers the wildcard and regular expression pattern matching operators.
Chapter 5 continues the discussion of operators with the advanced operations for working with arrays (indexing and slicing) and objects (properties and methods). It also covers output redirection and the formatting operator, and introduces PowerShell variables.
Chapter 6 covers the PowerShell language constructs such as if statement and loops.
Chapter 7 introduces programming in PowerShell and covers functions and scripts, variable scoping, and other programming-related topics.
Chapter 8 builds on the material in chapter 7 and introduces advanced programming techniques, such as object construction and extensions. It also covers first-class functions (scriptblocks) and shows how to extend the PowerShell language using these features.
Chapter 9 completes part 1, covering the features available in PowerShell for handling errors and debugging scripts.
In part 2 of the book, we shift our focus from individual features toward combining those features into larger examples. This part of the book looks at applying PowerShell in specific technology areas and problem domains.
We begin in chapter 10, looking at how PowerShell can be used to attack the kind of text processing tasks that have been the traditional domain of languages such as Perl. This chapter begins with basic string processing, then introduces file processing (including handling binary files), and finishes up with a section on working with XML documents.
Then, in chapter 11, we look at how we can discover and apply the vast capabilities of the .NET framework from .NET. We cover locating, exploring, and instantiating types in the .NET framework, including generic types. Then we look at a number of applications using these types, including network programming and graphical programming with WinForms.
In chapter 12, we look at how to use and apply other Microsoft object technologies, specifically COM and WMI. This includes using the application automation models to script applications such as Microsoft Word using PowerShell. We look at how to use WMI from the command line and in scripts to inspect, update, and manage a Windows system. We also spend some time looking at how to interact with VBScript, Microsoft’s previous-generation scripting tool.
Finally, in chapter 13, we introduce the security features in PowerShell, along with a general discussion of security. This is an important chapter to read. Like all powerful scripting tools (Perl, Python, and so forth), PowerShell can be used to create malware such as virus and worm programs. The PowerShell runtime contains features which allow you to deploy it in a manner that minimizes these risks.
That covers two out of the two and a half parts. Since the examples in part 2 of the book, while larger, still focus on particular technology areas, we have appendix B, which presents examples where we solve some common system administration tasks using PowerShell. While it’s not a complete management cookbook, it does show what can be done with PowerShell and how to do it.
In appendix A we present comparisons of PowerShell with other programming or scripting languages, including cmd.exe, UNIX shells and VBScript. This appendix includes tips for experienced users and highlights some potential problems users of other languages may run into with PowerShell. Finally, appendix C contains the annotated grammar and tokenization rules for the PowerShell Language along with syntax examples.
Since PowerShell is an interactive environment, we’ll show a lot of example commands as the user would type them, followed by the responses the system generates. Before the command text, there will be a prompt string that looks like this: PS (2) > . Following the prompt, the actual command will be displayed in bold font. PowerShell’s responses will follow on the next few lines. Since PowerShell doesn’t display anything in front of the output lines, you can distinguish output from commands by looking for the prompt string. These conventions are illustrated as follows:
PS (1) > get-date
Sunday, October 08, 2006 11:24:42 PM
Sometimes commands will span multiple lines. In this case, subsequent lines of user input will be preceded by >> as shown:
PS (2) > 1..3 |
>> foreach {"+" * $_}
>>
+
++
+++
PS (4) >
Note that the actual prompt sequence you see in your PowerShell session will be somewhat different than what is shown in the book. The prompt display is user-controllable by redefining the prompt function (see section A.1.8 for more information). For this book, a prompt sequence was chosen that includes command numbers to make it easier to follow the examples.
Source code for all of the examples used in this book can be downloaded from the publisher’s website at www.manning.com/payette.
Purchase of Windows PowerShell in Action includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/payette. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking the author some challenging questions, lest his interest stray!
The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.
Bruce Payette is one of the founding members of the Windows PowerShell team. He is co-designer of the PowerShell language along with Jim Truher, and is the principal author of the language implementation. He joined Microsoft in 2001, working on Interix—the POSIX subsystem for Windows—and then moved to help found the PowerShell project shortly thereafter. Prior to joining Microsoft, he worked at various companies including Softway (the creators of Interix) and MKS (producers of the MKS Toolkit) building UNIX tools for Windows. He lives in Bellevue, Washington, with his wife, many computers, and three extremely over-bonded and very spoiled cats.
By combining introductions, overviews, and how-to examples, the In Action books are designed to help learning and remembering. According to research in cognitive science, the things people remember are things they discover during self-motivated exploration.
Although noone at Manning is a cognitive scientist, we are convinced that for learning to become permanent, it must pass through stages of exploration, play, and, interestingly, retelling of what is being learned. People understand and remember new things, which is to say they master them, only after actively exploring them. Humans learn in action. An essential part of an In Action guide is that it is example-driven. It encourages the reader to try things out, to play with new code, and explore new ideas.
There is another, more mundane, reason for the title of this book: our readers are busy. They use books to do a job or solve a problem. They need books that allow them to jump in and jump out easily and learn just what they want just when they want it. They need books that aid them in action. The books in this series are designed for such readers.
The figure on the cover of Windows PowerShell in Action is a “Mufti, the chief of religion,” or the chief scholar who interpreted the religious law and whose pronouncements on matters both large and small were binding to the faithful. The illustration is taken from a collection of costumes of the Ottoman Empire published on January 1, 1802, by William Miller of Old Bond Street, London. The title page is missing from the collection and we have been unable to track it down to date. The book's table of contents identifies the figures in both English and French, and each illustration bears the names of two artists who worked on it, both of whom would no doubt be surprised to find their art gracing the front cover of a computer programming book...two hundred years later.
The collection was purchased by a Manning editor at an antiquarian flea market in the “Garage” on West 26th Street in Manhattan. The seller was an American based in Ankara, Turkey, and the transaction took place just as he was packing up his stand for the day. The Manning editor did not have on his person the substantial amount of cash that was required for the purchase and a credit card and check were both politely turned down. With the seller flying back to Ankara that evening the situation was getting hopeless. What was the solution? It turned out to be nothing more than an old-fashioned verbal agreement sealed with a handshake. The seller simply proposed that the money be transferred to him by wire and the editor walked out with the bank information on a piece of paper and the portfolio of images under his arm. Needless to say, we transferred the funds the next day, and we remain grateful and impressed by this unknown person’s trust in one of us. It recalls something that might have happened a long time ago.
The pictures from the Ottoman collection, like the other illustrations that appear on our covers, bring to life the richness and variety of dress customs of two centuries ago. They recall the sense of isolation and distance of that period—and of every other historic period except our own hyperkinetic present.
Dress codes have changed since then and the diversity by region, so rich at the time, has faded away. It is now often hard to tell the inhabitant of one continent from another. Perhaps, trying to view it optimistically, we have traded a cultural and visual diversity for a more varied personal life. Or a more varied and interesting intellectual and technical life.
We at Manning celebrate the inventiveness, the initiative, and, yes, the fun of the computer business with book covers based on the rich diversity of regional life of two centuries ago‚ brought back to life by the pictures from this collection.