Microsoft ® Windows ® PowerShell™ Programming for the Absolute Beginner Jerry Lee Ford, Jr. © 2007 Thomson Course Technology, a division of Thomson Learning Publisher and General Manager, Inc. All rights reserved. No part of this book may be reproduced or Thomson Course Technology PTR: transmitted in any form or by any means, electronic or mechanical, Stacy L. Hiquet including photocopying, recording, or by any information storage or retrieval system without written permission from Thomson Course Associate Director of Marketing: Technology PTR, except for the inclusion of brief quotations in a Sarah O’Donnell review. The Thomson Course Technology PTR logo and related trade dress are Manager of Editorial Services: trademarks of Thomson Course Technology, a division of Thomson Heather Talbot Learning Inc., and may not be used without written permission. Microsoft, Windows, and PowerShell are either registered trademarks Marketing Manager: or trademarks of Microsoft Corporation in the United States and/or Mark Hughes other countries. Seinfeld is a copyright of Sony Pictures Entertainment. All other trademarks are the property of their respective owners. Acquisitions Editor: Important:Thomson Course Technology PTR cannot provide software Mitzi Koontz support. Please contact the appropriate software manufacturer’s technical support line or Web site for assistance. Marketing Coordinator: Thomson Course Technology PTR and the author have attempted Adena Flitt throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer. Project Editor: Jenny Davidson Information contained in this book has been obtained by Thomson Course Technology PTR from sources believed to be reliable. However, because of the possibility of human or mechanical error by our Technical Reviewer: sources, Thomson Course Technology PTR, or others, the Publisher Keith Davenport does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be PTR Editorial Services Coordinator: particularly aware of the fact that the Internet is an ever-changing Erin Johnson entity. Some facts may have changed since this book went to press. Educational facilities, companies, and organizations interested in Interior Layout: multiple copies or licensing of this book should contact the Publisher Shawn Morningstar for quantity discount information. Training manuals, CD-ROMs, and portions of this book are also available individually or can be tailored for specific needs. Cover Designer: ISBN-10: 1-59863-354-6 Mike Tanamachi ISBN-13: 978-1-59863-354-2 eISBN-10: 1-59863-792-4 Indexer: Library of Congress Catalog Card Number: 2006907921 Sharon Shock Printed in the United States of America 07 08 09 10 11 PH 10 9 8 7 6 5 4 3 2 1 Proofreader: Kate Welsh Thomson Course Technology PTR, a division of Thomson Course Technology 25 Thomson Place Boston, MA 02210 http://www.courseptr.com To my wonderful children, Alexander, William, and Molly, and my beautiful wife, Mary. Acknowledgments This book represents the culmination of hard work from a number of individu- als to whom I owe many thanks. For starters, there is Mitzi Koontz, for helping me get this book started and for her support as acquisitions editor. I also owe a special debt of gratitude to Jenny Davidson, who served as the book’s project/ copy editor and worked hard to help keep me straight and ensured that every- thing came together like it was supposed to. Thanks also go out to Keith Davenport, who as the book’s technical editor provided me with invaluable insight, guidance, and advice. Finally, I’d like to thank everyone else at Thomson Course Technology PTR for all their contributions and hard work. About the Author Jerry Lee Ford, Jr. is an author, educator, and an IT professional with over 18 years of experience in information technology, including roles as an automation analyst, technical manager, technical support analyst, automation engineer, and security analyst. Jerry has a master’s degree in Business Administration from Vir- ginia Commonwealth University in Richmond, Virginia. He is the author of 19 other books and co-author of two additional books. His published works include Microsoft Windows Shell Scripting for the Absolute Beginner, Microsoft Windows Shell Scripting and WSH Administrator’s Guide, Perl Programming for the Absolute Beginner, VBScript Professional Projects, Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner, Beginning REALbasic, Learn VBScript in a Weekend, Learn JavaScript in a Weekend, and Microsoft Windows XP Professional Administrator’s Guide. He has over five years’ experience as an adjunct instructor teaching networking courses in Information Technology. Jerry lives in Richmond, Virginia, with his wife, Mary, and their children, William, Alexander, and Molly. This page intentionally left blank Contents Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Introducing Windows PowerShell . . . . . . . . . . . . .3 Chapter 1 Project Preview: The Knock Knock Joke Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Getting to Know Windows PowerShell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 A Little History Lesson. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Integration with .NET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 PowerShell Versus cmd.exe. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Installing and Configuring Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . 10 Interacting with the PowerShell Command Prompt. . . . . . . . . . . . . . . . . . . . . . . 12 Starting a New PowerShell Session. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Executing PowerShell Cmdlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Other Types of Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 A Short PowerShell Workout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Windows PowerShell Scripting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Simplifying PowerShell Script Execution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Back to the Knock Knock Joke Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Designing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 The Final Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Interacting with the Windows PowerShell Chapter 2 Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 Project Preview: The Story of the Three Amigos . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Accessing Windows PowerShell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Customizing the Windows PowerShell Working Environment . . . . . . . . . . . . . 36 Customizing Windows PowerShell Shortcuts. . . . . . . . . . . . . . . . . . . . . . . . . . 36 Configuring the Windows Command Console. . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Windows Command Console Customization Options. . . . . . . . . . . . . . . . . . 38 Windows Command Console Editing Features . . . . . . . . . . . . . . . . . . . . . . . . 43 Windows PowerShell Edit Enhancements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Tab Completion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 The Get-History Cmdlet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Navigating Hierarchical Data Stores. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 viii Windows PowerShell Programming for the Absolute Beginner Back to The Story of the Three Amigos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Creating a New Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Declaring Script Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Displaying the Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Providing Player Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Prompting the Player for Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Collecting Additional Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Displaying the Story’s Opening. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Displaying the Rest of the Story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Object-Based Scripting with .NET . . . . . . . . . . . . .63 Chapter 3 Project Preview: The PowerShell Fortune Teller Game. . . . . . . . . . . . . . . . . . . . . 64 One Last PowerShell Customization Technique. . . . . . . . . . . . . . . . . . . . . . . . . . . 66 The Microsoft .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Key .NET Framework Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 The .NET Class Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 The Common Language Runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Accessing .NET Framework Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Executing Cmdlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Windows PowerShell Plumbing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Working with Aliases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Back to the PowerShell Fortune Teller Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Designing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Creating a New PowerShell Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Declaring and Initializing Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Displaying the Welcome Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Displaying Game Instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Controlling Gameplay. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Displaying the Closing Screen. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Working with Variables, Arrays, and Hashes . .99 Chapter 4 Project Preview: The Seinfeld Trivia Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Windows PowerShell Language Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Windows PowerShell Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Escape Characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 String Manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Contents ix Storing and Retrieving Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Back to the Seinfeld Trivia Quiz. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Designing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 The Final Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Implementing Conditional Logic . . . . . . . . . . . . . .133 Chapter 5 Project Preview: The Guess My Number Game . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Comparing Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Combining Pipelines and Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Implementing Conditional Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Comparing Data Using the if Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Making Multiple Comparisons Using the switch Statement. . . . . . . . . . . . 144 Windows PowerShell Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Comparison Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 String Comparison Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Back to the Guess My Number Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Designing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 The Final Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Using Loops to Process Data . . . . . . . . . . . . . . . . .163 Chapter 6 Project Preview: The Rock, Paper, Scissors Game . . . . . . . . . . . . . . . . . . . . . . . . 164 Working with Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Setting Up do while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Setting Up do until Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Creating for Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Creating foreach Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Using while Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Altering Loop Execution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Using the break Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Using the continue Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Back to the Rock, Paper, Scissors Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Designing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 The Final Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Description: