
Mastering Windows PowerShell Scripting Third Edition Automate and manage your environment using PowerShell Core 6.0 Chris Dent BIRMINGHAM - MUMBAI Mastering Windows PowerShell Scripting Third Edition Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Vijin Boricha Acquisition Editor: Meeta Rajani Content Development Editor: Nithin George Varghese Technical Editor: Rutuja Patade Copy Editor: Safis Editing Project Coordinator: Drashti Panchal Proofreader: Safis Editing Indexer: Pratik Shirodkar Graphics: Tom Scaria Production Coordinator: Jisha Chirayil First published: April 2015 Second edition: October 2017 Third edition: February 2019 Production reference: 1280219 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78953-666-9 www.packtpub.com mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content Packt.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author Chris Dent is an automation specialist with a deep interest in the PowerShell language. Chris is often found lurking and answering questions about PowerShell in both the UK and virtual PowerShell user groups. Chris has been developing in PowerShell since 2007 and has released several modules over the years. My thanks, first and foremost, must go to my wife and two children for their forbearance as I have written this book. I want to express my thanks to the technical reviewers, Paul Broadwith and Graham Beer, for their invaluable comments and feedback throughout this journey. Finally, I would like to thank the members of the Virtual PowerShell User Group for putting up with my weekend rambling and musing as I work through each of the chapters. About the reviewers Paul Broadwith is a senior technology professional freelancing in Scotland, with over 25 years of experience in diverse sectors, from manufacturing and financial services to the public sector and managed IT services. He has been involved in the open source and PowerShell communities for several years. His love of not doing things twice motivates him to automate as much as possible with PowerShell, and you will find much of it on GitHub. You'll find him working between there and the Chocolatey community, where he works on several open source projects. In what's left of his spare time (which isn't much), you can find him blogging at pauby.com and tweeting from @pauby. Graham Beer is an experienced IT professional with excellent PowerShell skills and a flair for automation with Microsoft and AWS products. He is a regular contributor to 4sysops articles and was named in SQLShack's top 50 PowerShell bloggers of 2018. He recently had a chapter published in The PowerShell Conference Book about extending type data. He co- founded a PowerShell user group in the South of England, which has been running for over a year. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Section 1: Section 1: Exploring PowerShell Fundamentals Chapter 1: Introduction to PowerShell 8 Technical requirements 9 What is PowerShell? 9 PowerShell editors 10 Getting help 10 Updatable help 11 The Get-Help command 12 Syntax 13 Examples 14 Parameter 14 Detailed and full switches 15 Save-Help 16 Update-Help 16 About help files 18 Command naming and discovery 19 Verbs 19 Nouns 19 Finding commands 20 Aliases 21 Parameters and parameter sets 22 Parameters 22 Optional parameters 22 Optional positional parameters 23 Mandatory parameters 23 Mandatory positional parameters 23 Switch parameters 24 Common parameters 24 Parameter values 25 Parameter sets 26 Confirm, WhatIf, and Force 27 Confirm parameter 27 ConfirmPreference 28 WhatIf parameter 30 WhatIfPreference 30 Force parameter 31 Introduction to providers 31 Drives using providers 32 Table of Contents Using providers 34 Introduction to splatting 35 Splatting to avoid escaped end-of-line 36 Splatting to avoid repetition 38 Splatting and positional parameters 39 Summary 39 Chapter 2: Modules and Snap-ins 40 Introducing modules 40 What is the PowerShell Gallery? 42 The Get-Module command 43 PSModulePath in Windows PowerShell 43 PSModulePath in PowerShell Core 44 Get-Module, PSCompatibility, and PSEdition 45 The Import-Module command 45 The Remove-Module command 47 The Find-Module command 47 The Install-Module command 48 The Update-Module command 49 The Save-Module command 49 PowerShell Core and the WindowsCompatibility module 49 The compatibility session 50 Add-WindowsPSModulePath 51 Get-WinModule and Import-WinModule 52 Copy-WinModule 52 Invoke-WinCommand 53 Introducing snap-ins 53 Using snap-ins 54 Summary 54 Chapter 3: Working with Objects in PowerShell 55 Pipelines 56 Standard output 56 Non-standard output 56 The object pipeline 57 Members 57 The Get-Member command 58 Accessing properties 59 Using methods 60 Access modifiers 61 The Add-Member command 63 Enumerating and filtering 65 The ForEach-Object command 65 Where-Object command 66 Selecting and sorting 67 The Select-Object command 67 [ ii ] Table of Contents The Sort-Object command 70 Grouping and measuring 73 The Group-Object command 73 The Measure-Object command 76 Comparing 77 Importing, exporting, and converting 79 The Export-Csv command 79 The Import-Csv command 81 Export-Clixml and Import-Clixml 83 Summary 84 Chapter 4: Operators 85 Arithmetic operators 86 Operator precedence 86 Addition and subtraction operators 87 Addition operators 87 Subtraction operator 88 Multiplication, division, and remainder operators 88 Multiplication operator 89 Division operator 89 Remainder operator 90 Shift left and shift right operators 90 Assignment operators 93 Assign, add and assign, and subtract and assign 93 Multiply and assign, divide and assign, and modulus and assign 95 Comparison operators 96 Case-sensitivity 96 Comparison operators and arrays 97 Equal to and not equal to 98 Like and not like 99 Greater than and less than 99 Contains and in 100 Regular expression-based operators 101 Match and not match 101 Replace 102 Split 103 Binary operators 104 Binary and 104 Binary or 105 Binary exclusive or 105 Binary not 106 Logical operators 106 And 106 Or 107 Exclusive or 107 Not 107 [ iii ] Table of Contents Type operators 108 As 108 is and isnot 109 Redirection operators 109 Redirection to a file 110 Redirecting streams to standard output 112 Redirection to null 113 Other operators 114 Call 114 Comma 114 Format 115 Increment and decrement 116 Join 117 Summary 117 Section 2: Section 2: Working with Data Chapter 5: Variables, Arrays, and Hashtables 119 Naming and creating variables 120 Variable commands 121 Clear 122 Get 122 New 122 Remove 123 Set 123 Variable provider 124 Variable scope 125 Local and global scope 126 Private scope 127 Script scope 128 Type and type conversion 129 Objects assigned to variables 131 Arrays 132 Creating an array 133 Arrays with a type 133 Adding elements to an array 134 Selecting elements from an array 135 Changing element values in an array 136 Removing elements from an array 136 Removing elements by index 137 Removing elements by value 138 Clearing an array 138 Filling variables from arrays 138 Multi-dimensional and jagged arrays 139 Hashtables 140 [ iv ] Table of Contents Creating a hashtable 141 Adding and changing elements to a hashtable 141 Selecting elements from a hashtable 143 Enumerating a hashtable 144 Removing elements from
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages50 Page
-
File Size-