O'Reilly logo

Windows PowerShell Cookbook, 2nd Edition by Lee Holmes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required


Ed Wilson

Microsoft Scripting Guy and author of Windows PowerShell 2.0 Best Practices

As someone who has written, or contributed to, more than a dozen books, I am well aware of the incredible amount of work and monumental commitment of time and resources involved with writing a book. That someone would choose to do this at essentially the same time one is burning the midnight oil while developing one of the most exciting products in Microsoft’s history bespeaks a most committed person. However, more than simple commitment is involved. From my conversations with Lee, I can tell that he is passionate about Windows PowerShell. He sees the revolutionary changes introduced with the 2.0 release of the product. If Windows PowerShell 1.0 was the concept, Windows PowerShell 2.0 is the answer. If Windows PowerShell 1.0 was the vision, Windows PowerShell 2.0 is the reality. If Windows PowerShell 1.0 was for early adopters, Windows PowerShell 2.0 is moving into the mainstream.

With the inclusion of Windows PowerShell 2.0 in Windows 7 and Windows Server 2008 R2, we are beginning to see the commitment Microsoft is making to the product. That the SharePoint, SQL, Exchange, Active Directory Domain Services (AD DS), Internet Information Server (IIS) teams, and others have all made cmdlets should tell you that Windows PowerShell is not a passing fad. Windows PowerShell questions are already cropping up on Microsoft Certification Examinations, and as a network administrator or a consultant, you will need to learn Windows PowerShell.

Learning Windows PowerShell need not be tedious, boring, or exhausting. In fact, you will be joining a community that is at least as passionate about Windows PowerShell as Lee (or the rest of the Windows PowerShell team) or me (I write the Hey, Scripting Guy! blog seven days a week—the only Microsoft blog updated daily, by the way). What other product from Microsoft has inspired a half dozen songs to be written about it? Not by the marketing department, but by people who fell in love with Windows PowerShell, or, better yet, to use the community term: became addicted.

I attended a recent SQL Saturday in Charlotte, North Carolina, because I wanted to meet and interact with members of the Windows PowerShell community. That is right: there is a huge group of hardcore SQL administrators who are adopting Windows PowerShell because of its cool server management capabilities. In addition, a project known as the SQL Server PowerShell Extensions (SQLPSX) module (available from CodePlex) has wrapped much of the SQL Management Objects (SMO) into more than 130 useful functions. This provides ease of use for people who are not experts with SQL SMO and Windows PowerShell. By leveraging modules, the community is taking advantage of one of the great new features of Windows PowerShell 2.0. In fact, there are more than 200 Windows PowerShell projects on CodePlex. One person presenting at SQL Saturday declared that the active Windows PowerShell community was one of the great strengths of Windows PowerShell. You are not alone when it comes to learning and implementing Windows PowerShell.

I do not own every Windows PowerShell book ever written. I have probably looked at most Windows PowerShell books, but I found some of them redundant and some others confusing. However, a few of the Windows PowerShell books are essential. Lee’s 1.0 version of this book fell into that category. I keep it within arm’s length of my desk and grab it often. I have highlighted certain sections, dog-eared others, and placed sticky tabs on the more essential pages. Over the last couple years, Lee’s Windows PowerShell Cookbook has grown to look more like a skinny porcupine on a bad hair day than a typical scripting book—and that is a good thing, because his book is not a typical scripting book; it is a cookbook. Just like a “real cookbook” that contains recipes for meals, this fascinating volume is what I find myself thumbing through when I am hungry to try something new with Windows PowerShell.

In reviewing Lee’s upgraded Windows PowerShell Cookbook, I see that I will not be placed on a diet of “foo” and “bar”; instead, there are tasty morsels such as Get-PageUrls, a way-cool script that illustrates using regular expressions to extract URLs from a downloaded web page. It even fixes relative URLs so that they include the server from which they originated. All this happens faster than you can say “super useful” three times.

I found Chapter 14 on debugging to be well worth a careful read. Lee has a number of really good points, the premier one being: do not make the mistake in the first place. This echoes my own best practice. Of course, mistakes are made, errors are introduced, and that is when the debugger commands are called upon. Windows PowerShell 2.0 ships with some great debugging cmdlets, and Lee has some extremely cool scripts to simplify the process, or at least to reduce some of the tedium. I really like the Watch-Expression script because it automatically displays the values of expressions you wish to track.

If Chapter 14 is worth a careful read, Chapter 18 is worth a sticky tab because you will find yourself coming back to it often. Security and script signing is a subject of much debate in the Windows PowerShell community. You will want to hear about security from the horse’s mouth. A common question I hear when giving presentations on Windows PowerShell is “How can I invoke a command as another user without switching contexts?” The genesis of this question is, of course, the Unix sudo command. Lee has a useful script named Invoke-ElevatedCommand that allows you to accomplish this task. Most excellent.

One other thing you need to read about is the Windows PowerShell Integrated Scripting Environment (ISE), in Chapter 19. A common request for years was for Microsoft to write a script editor. For years, I have been telling people we did write a script editor—Notepad. The Windows PowerShell ISE is much better than Notepad. Not only is the Windows PowerShell ISE a great script editor in its own right, but the Windows PowerShell team also exposed an object model that allows you to modify its behavior and to configure it to work in the way you wish to work. Lee has a whole section in Chapter 19 that talks about the ISE and how to modify it.

Working with files, directories, the registry, services, processes, WMI, remoting, transactions, and event handling—it is all in this book. I am not going to go over all that, because I do not want to spoil the plot. Suffice it to say that once this book sees print, it will rapidly join its dog-eared younger brother in that small collection of Windows PowerShell books that I consider essential.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required