Windows PowerShell in Action Windows PowerShell in Action BRUCE PAYETTE MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, please go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Sound View Court 3B Fax: (609) 877-8256 Greenwich, CT 06830 email: [email protected] ©2007 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Benjamin Berg Sound View Court 3B Typesetter: Gordan Salinovic Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1932394-90-7 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 11 10 09 08 07 To my wife, Tina, for all her love and support brief contents Part 1 Learning PowerShell 1 1 Welcome to PowerShell 3 2 The basics 25 3 Working with types 55 4 Operators and expressions 87 5 Advanced operators and variables 115 6 Flow control in scripts 147 7 Functions and scripts 177 8 Scriptblocks and objects 214 9 Errors, exceptions, and script debugging 251 Part 2 Using PowerShell 295 10 Processing text, files, and XML 297 11 Getting fancy—.NET and WinForms 344 12 Windows objects: COM and WMI 392 13 Security, security, security 440 vii contents foreword xv preface xvii acknowledgments xix about this book xx Part 1 LEARNING POWERSHELL 1 1 Welcome to PowerShell 3 1.1 What is PowerShell? 5 Shells, command-lines, and scripting languages 5 ✦ Why a new shell? Why now? 7 ✦ The last mile problem 7 1.2 Soul of a new language 8 Learning from history 8 ✦ Leveraging .NET 9 1.3 Brushing up on objects 10 Reviewing object-oriented programming 11 Objects in PowerShell 12 1.4 Dude! Where’s my code? 13 Installing and starting PowerShell 13 ✦ Command editing 15 Command completion 16 ✦ Evaluating basic expressions 17 Processing data 18 1.5 Summary 23 2 The basics 25 2.1 Command concepts and terminology 27 Commands and cmdlets 27 ✦ Command categories 30 Aliases and elastic syntax 34 2.2 Parsing and PowerShell 37 How PowerShell parses 37 ✦ Quoting 38 ✦ Expression mode and command mode parsing 41 ✦ Statement termination 43 ix
Description: