SECOND EDITION Don Jones Jeffery Hicks Richard Siddaway M A N N I N G Praise for the First Edition A thorough look at PowerShell from three of the best writers in the PowerShell community. —Mike Shepard, Solutions Architect Jack Henry & Associates The most wonderful PowerShell administrative guide. —Kais Ayari, PowerShell Expert, Microsoft Another great PowerShell book for my desk! —Thomas Lee, PowerShell Consultant, PS Partnership Hicks, Jones, and Siddaway have come together to deliver the ultimate PowerShell resource. —James Berkenbile, Principal Consultant Berkenbile Consulting I have many books on my shelves. This is one that will be on my desk! —Trent Whiteley, Software Engineer, Fiserv The authors know their audience and never lose sight of their readers. They use their knowledge and understanding in very clever ways to help readers understand even complex subjects. —Rolf Åberg, Platform Architect Active Directory, SEB An end-all, be-all resource in the working man’s hands. I can finally give Google a rest. —Eric Stoker, Network Administrator, Spokane Teacher’s Credit Union Hands-down the best PowerShell book to date. The authors are some of the most respected in the PowerShell community and this book illustrates why! These guys know this stuff inside and out. —Adam Bell, Solution Architect, ZOE Systems Pty Ltd. Priceless, practical guidance. —Klaus Schulte, System administrator, www.kvwl.de PowerShell in Depth Second Edition DON JONES JEFFERY HICKS RICHARD SIDDAWAY MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: [email protected] ©2015 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. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Karen Miller 20 Baldwin Road Copyeditor: Liz Welch PO Box 261 Proofreaders: Toma Mulligan, Andy Carroll Shelter Island, NY 11964 Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617292187 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 19 18 17 16 15 14 brief contents PART 1 POWERSHELL FUNDAMENTALS .......................................1 1 ■ Introduction 3 2 ■ PowerShell hosts 7 3 ■ Using the PowerShell help system 19 4 ■ The basics of PowerShell syntax 33 5 ■ Working with PSSnapins and modules 44 6 ■ Operators 52 7 ■ Working with objects 69 8 ■ The PowerShell pipeline 106 9 ■ Formatting 125 PART 2 POWERSHELL MANAGEMENT......................................143 10 ■ PowerShell Remoting 145 11 ■ Background jobs and scheduling 181 12 ■ Working with credentials 196 13 ■ Regular expressions 206 14 ■ Working with HTML and XML data 222 v vi BRIEF CONTENTS 15 ■ PSDrives and PSProviders 244 16 ■ Variables, arrays, hash tables, and script blocks 260 17 ■ PowerShell security 282 18 ■ Advanced PowerShell syntax 296 PART 3 POWERSHELL SCRIPTING AND AUTOMATION...............315 19 ■ PowerShell’s scripting language 317 20 ■ Basic scripts and functions 332 21 ■ Creating objects for ouput 344 22 ■ Scope 361 23 ■ PowerShell workflows 376 24 ■ Advanced syntax for scripts and functions 406 25 ■ Script modules and manifest modules 427 26 ■ Custom formatting views 439 27 ■ Custom type extensions 451 28 ■ Data language and internationalization 465 29 ■ Writing help 477 30 ■ Error handling techniques 484 31 ■ Debugging tools and techniques 496 32 ■ Functions that work like cmdlets 520 33 ■ Tips and tricks for creating reports 539 PART 4 ADVANCED POWERSHELL...........................................549 34 ■ Working with the Component Object Model (COM) 551 35 ■ Working with .NET Framework objects 560 36 ■ Accessing databases 573 37 ■ Proxy functions 581 38 ■ Building a GUI 593 39 ■ WMI and CIM 613 40 ■ Working with the web 640 41 ■ Desired State Configuration 656 contents preface xxi acknowledgments xxiii about this book xxv PART 1 POWERSHELL FUNDAMENTALS............................1 1 Introduction 3 1.1 Who this book is for 3 1.2 What this book will teach you 4 1.3 What this book won’t teach you 4 1.4 Where we drew the line 5 1.5 Beyond PowerShell 5 1.6 Ready? 6 2 PowerShell hosts 7 2.1 32-bit vs. 64-bit, and administrator vs. not 8 2.2 The console 12 2.3 The PowerShell ISE 14 2.4 Command history buffer vs. PowerShell’s history 17 ix x CONTENTS 2.5 Transcripts 17 2.6 Summary 18 3 Using the PowerShell help system 19 3.1 The help commands 19 3.2 Where’s the help? 20 3.3 Saving help 23 3.4 Using the help 24 3.5 “About” help files 27 3.6 Provider help 28 3.7 Interpreting command help 28 3.8 Common parameters 31 3.9 Summary 32 4 The basics of PowerShell syntax 33 4.1 Commands 34 Aliases: nicknames for commands 35 ■ Command name tab completion 36 4.2 Parameters 36 Truncating parameter names 39 ■ Parameter name tab completion 39 4.3 Typing trick: line continuation 40 4.4 Parenthetical commands and expressions 41 4.5 Script blocks 42 4.6 Summary 42 5 Working with PSSnapins and modules 44 5.1 There’s only one shell 44 5.2 PSSnapins vs. modules 45 5.3 Loading, autoloading, and profiles 46 5.4 Using extensions 46 Discovering extensions 46 ■ Loading extensions 48 Discovering extensions’ additions 48 ■ Managing extensions 49 5.5 Command name conflicts 50 5.6 Managing module autoloading 51 5.7 Summary 51 CONTENTS xi 6 Operators 52 6.1 Logical and comparison operators 53 The –contains operator 55 ■ The -in and -notin operators 56 Boolean, or logical, operators 57 ■ Bitwise operators 58 6.2 Arithmetic operators 61 6.3 Other operators 62 String and array manipulation operators 62 Object type operators 63 ■ Format operator 64 Miscellaneous operators 65 6.4 Math operators 67 6.5 Summary 68 7 Working with objects 69 7.1 Introduction to objects 70 7.2 Members: properties, methods, and events 71 7.3 Sorting objects 77 7.4 Selecting objects 78 Use 1: choosing properties 78 ■ Use 2: choosing a subset of objects 80 ■ Use 3: making custom properties 82 Use 4: extracting and expanding properties 84 Use 5: choosing properties and a subset of objects 88 7.5 Filtering objects 88 Simplified syntax 89 ■ Full syntax 90 The Where method 90 7.6 Grouping objects 94 7.7 Measuring objects 96 7.8 Enumerating objects 97 Full syntax 97 ■ Simplified syntax 98 The ForEach method 99 7.9 Importing, exporting, and converting objects 100 7.10 Comparing objects 104 7.11 Summary 105 8 The PowerShell pipeline 106 8.1 How the pipeline works 106 The old way of piping 107 ■ The PowerShell way of piping 107 8.2 Parameter binding ByValue 108