Windows Powershell 2 for Dummies
Total Page:16
File Type:pdf, Size:1020Kb
Windows PowerShell™ 2 FOR DUMmIES‰ by Steve Seguis Windows PowerShell™ 2 For Dummies® Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 07030-5774 www.wiley.com Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit- ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http:// www.wiley.com/go/permissions. Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/ or its affi liates in the United States and other countries, and may not be used without written permission. Windows PowerShell is a trademark of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITH- OUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZA- TION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit www.wiley.com/techsupport. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2009931743 ISBN: 978-0-470-37198-5 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 About the Author Steve Seguis lives with his amazing wife, Annalene, in New York City, New York. He is a twelve-year Windows Systems Engineer veteran and specializes in systems automation. He was a Microsoft Most Valuable Professional (MVP) for Windows Server — Admin Framework from 2004–2007. He is also a con- tributing writer and technical editor for Windows IT Pro and, most recently, has published a book on Windows Server 2008 Administration. Dedication To my parents, Romeo and Lourdes, who gave me the opportunities that have allowed me to pursue my dreams and become who I am today. Author’s Acknowledgments I’ve always been a fan of the For Dummies books, which has often resulted in one or two chuckles from my colleagues due to the incorrect perception that somehow reading a For Dummies book implies a lack of intelligence. The reality is that I’m a fan of making complex things simple and I like books that focus on getting me the information I need in an easy, digestible format. The For Dummies books have been doing this for years, and ever since I read my fi rst For Dummies book (specifi cally C For Dummies by Dan Gookin, over a dozen years ago), I was captured by the ease at which I was able to gain knowledge while having the occasional laugh. I never in my wildest imagina- tion thought I’d ever have the opportunity to write one myself . that is until my agent, David Fugate, got me in touch with the good people over at Wiley Publishing and got this journey started. Thanks David! I’d like to thank Greg Croy, Executive Editor, for getting my proposal for this book approved. He actually retired before I was done writing the book, but kudos to him for getting the ball rolling. Thanks goes out to Blair Pottenger, Project Editor, for keeping me well-informed, answering all my questions, and putting in a lot of work to get the book fi nished. Of course, I’d also like to thank Katie Mohr, Acquisitions Editor, who took over Greg’s role in this proj- ect after he retired. Katie went on maternity leave just before we got done with the book, so congratulations Katie on the new baby. The project had hit a bit of a plateau half way through, but when she took over we were able to regroup and get everything back on track. I have to thank my very patient and supportive wife, Annalene, who puts up with me disappearing into the cubby hole I call my home offi ce for late night writing sessions and generally dealing with all my quirks. We somehow work together to stay sane despite our lives going at 100 miles an hour. I also have to thank my parents and my family for understanding how busy I get, generally staying out of my hair (what hair I have left), and letting me pursue my interests even though they continue to say that I need to slow down a bit and get some more sleep. Finally, I’d like to thank the guys over at Microsoft for creating this awesome scripting language called Windows PowerShell. We’ve come a long way since batch fi les and as a long-time Windows administrator, I bow to your great- ness. Windows PowerShell is truly empowering and more Windows folks in every company need to embrace it. Publisher’s Acknowledgments We’re proud of this book; please send us your comments through our online registration form located at http://dummies.custhelp.com. For other comments, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. Some of the people who helped bring this book to market include the following: Acquisition, Editorial, and Media Composition Services Development Project Coordinator: Lynsey Stanford Project Editor: Blair J. Pottenger Layout and Graphics: Melanee Habig, Executive Editor: Greg Croy Melissa K. Jester Acquisitions Editor: Katie Mohr Proofreaders: Melissa Cossell, Copy Editors: Virginia Sanders, Kathy Simpson Christopher M. Jones Technical Editor: David Dalan Indexer: Potomac Indexing, LLC Editorial Manager: Kevin Kirschner Media Development Project Manager: Laura Moss-Hollister Media Development Assistant Project Manager: Jenny Swisher Media Development Associate Producer: Josh Frank Sr. Editorial Assistant: Cherie Case Cartoons: Rich Tennant (www.the5thwave. com) Publishing and Editorial for Technology Dummies Richard Swadley, Vice President and Executive Group Publisher Andy Cummings, Vice President and Publisher Mary Bednarek, Executive Acquisitions Director Mary C. Corder, Editorial Director Publishing for Consumer Dummies Diane Graves Steele, Vice President and Publisher Composition Services Debbie Stailey, Director of Composition Services Contents at a Glance Introduction ................................................................ 1 Part I: Get ting a Bird’s-Eye View of PowerShell 2 .......... 9 Chapter 1: The Windows PowerShell Rap Sheet ..........................................................11 Chapter 2: Customizing and Shortcutting the Environment ......................................21 Chapter 3: A Pinch of Shell, a Pound of Power ............................................................37 Part II: PowerShell’s Basic Structure and Syntax ..........47 Chapter 4: Shelling Out Commands and Scripts ..........................................................49 Chapter 5: When Dollars Turn into Variables ..............................................................61 Chapter 6: A Bit of Logic to Save the Day .....................................................................77 Chapter 7: Working on a Pipeline ..................................................................................89 Part III: Complex Data Description and Sharing ........... 97 Chapter 8: Working with Windows Management Instrumentation ...........................99 Chapter 9: Bringing Strings into the Limelight ...........................................................117 Chapter 10: I’ll Take Numbers for $100, Please ..........................................................137 Chapter 11: Grouping Data Using Arrays and Hash Tables ......................................147