HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
Also available in other formats.
Register your product to gain access to bonus material or receive a coupon.
Your hands-on guide to Windows PowerShell scripting fundamentals
Expand your expertise--and teach yourself the fundamentals of Windows PowerShell scripting, including features available in Windows PowerShell 5. If you are an IT professional, power user, or consultant, you’ll get the guidance, exercises, and code you need to master core techniques for automating Windows setup, deployment, and management.
Discover how to:
Introduction xix
Chapter 1: Overview of Windows PowerShell 5.0 1
Understanding Windows PowerShell 1
Using cmdlets 3
Installing Windows PowerShell 3
Deploying Windows PowerShell to down-level operating systems 3
Using command-line utilities 4
Security issues with Windows PowerShell 6
Controlling execution of Windows PowerShell cmdlets 6
Confirming actions 7
Suspending confirmation of cmdlets 8
Working with Windows PowerShell 10
Accessing Windows PowerShell 10
Configuring the Windows PowerShell console 11
Supplying options for cmdlets 11
Working with the help options 12
Exploring commands: Step-by-step exercises 19
Chapter 1 quick reference 22
Chapter 2: Using Windows PowerShell cmdlets 23
Understanding the basics of cmdlets 23
Using the Get-ChildItem cmdlet 24
Obtaining a directory listing 24
Formatting a directory listing by using the Format-List cmdlet 26
Using the Format-Wide cmdlet 27
Formatting a directory listing by using Format-Table 29
Formatting output with Out-GridView 31
Taking advantage of the power of Get-Command 36
Searching for cmdlets by using wildcard characters 36
Using the Get-Member cmdlet 44
Using the Get-Member cmdlet to examine properties and methods 45
Using the New-Object cmdlet 50
Creating and using the wshShell object 50
Using the Show-Command cmdlet 52
Windows PowerShell cmdlet naming helps you learn 54
Windows PowerShell verb grouping 55
Windows PowerShell verb distribution 55
Creating a Windows PowerShell profile 57
Working with cmdlets: Step-by-step exercises 59
Chapter 2 quick reference 63
Chapter 3: Understanding and using Windows PowerShell providers 65
Understanding Windows PowerShell providers 65
Understanding the alias provider 66
Understanding the certificate provider 69
Understanding the environment provider 76
Understanding the filesystem provider 80
Understanding the function provider 85
Using the registry provider to manage the Windows registry 87
The two registry drives 88
The short way to create a new registry key 95
Dealing with a missing registry property 98
Understanding the variable provider 99
Exploring Windows PowerShell providers: Step-by-step exercises 103
Chapter 3 quick reference 107
Chapter 4: Using Windows PowerShell remoting and jobs 109
Understanding Windows PowerShell remoting 109
Classic remoting 109
WinRM 114
Using Windows PowerShell jobs 122
Using Windows PowerShell remoting and jobs: Step-by-step exercises 132
Chapter 4 quick reference 135
Chapter 5: Using Windows PowerShell scripts 137
Why write Windows PowerShell scripts? 137
The fundamentals of scripting 139
Running Windows PowerShell scripts 139
Turning on Windows PowerShell scripting support 140
Transitioning from command line to script 143
Manually running Windows PowerShell scripts 145
Understanding variables and constants 148
Using the While statement 154
Constructing the While statement in Windows PowerShell 154
A practical example of using the While statement 156
Using special features of Windows PowerShell 157
Using the Do...While statement 157
Using the range operator 158
Operating over an array 158
Casting to ASCII values 159
Using the Do...Until statement 160
Comparing the Windows PowerShell Do...Until statement with VBScript 160
Using the Windows PowerShell Do statement 161
The For statement 162
Using the For statement 163
Using the Foreach statement 164
Exiting the Foreach statement early 166
Using the If statement 168
Using assignment and comparison operators 169
Evaluating multiple conditions 170
The Switch statement 171
Using the Switch statement 172
Controlling matching behavior 174
Creating multiple folders: Step-by-step exercises 174
Chapter 5 quick reference 177
Chapter 6: Working with functions 179
Understanding functions 179
Using functions to provide ease of code reuse 186
Including functions in the Windows PowerShell environment 188
Using dot-sourcing 188
Using dot-sourced functions 190
Adding help for functions 191
Using a here-string object for help 192
Using two input parameters 194
Using a type constraint in a function 198
Using more than two input parameters 200
Using functions to encapsulate business logic 202
Using functions to provide ease of modification 204
Understanding filters 209
Creating a function: Step-by-step exercises 213
Chapter 6 quick reference 216
Chapter 7: Creating advanced functions and modules 217
The [cmdletbinding] attribute 217
Easy verbose messages 218
Automatic parameter checks 219
Adding support for the -WhatIf switch parameter 222
Adding support for the -Confirm switch parameter 223
Specifying the default parameter set 224
The Parameter attribute 224
The Mandatory parameter property 225
The Position parameter property 226
The ParameterSetName parameter property 227
The ValueFromPipeline property 228
The HelpMessage property 229
Understanding modules 230
Locating and loading modules 230
Installing modules 235
Creating a module 246
Creating an advanced function and installing a module: Step-by-step exercises 253
Chapter 7 quick reference 257
Chapter 8: Using the Windows PowerShell ISE 259
Running the Windows PowerShell ISE 259
Navigating the Windows PowerShell ISE 260
Working with the script pane 263
Using tab expansion and IntelliSense 264
Working with Windows PowerShell ISE snippets 266
Using Windows PowerShell ISE snippets to create code 266
Creating new Windows PowerShell ISE snippets 268
Removing user-defined Windows PowerShell ISE snippets 269
Using the Commands add-on and snippets: Step-by-step exercises 270
Chapter 8 quick reference 274
Chapter 9: Working with Windows PowerShell profiles 275
Six different Windows PowerShell profiles 275
Understanding the six Windows PowerShell profiles 276
Examining the $profile variable 276
Determining whether a specific profile exists 278
Creating a new profile 279
Design considerations for profiles 279
Using one or more profiles 281
Using the All Users, All Hosts profile 283
Using your own file 284
Grouping similar functionality into a module 285
Where to store the profile module 285
Creating and adding functionality to a profile: Step-by-step exercises 286
Chapter 9 quick reference 289
Chapter 10: Using WMI 291
Understanding the WMI model 292
Working with objects and namespaces 292
Listing WMI providers 297
Working with WMI classes 298
Querying WMI 301
Obtaining service information: Step-by-step exercises 306
Chapter 10 quick reference 312
Chapter 11: Querying WMI 313
Alternate ways to connect to WMI 313
Returning selective data from all instances 321
Selecting multiple properties 322
Choosing specific instances 325
Using an operator 327
Shortening the syntax 330
Working with software: Step-by-step exercises 332
Chapter 11 quick reference 339
Chapter 12: Remoting WMI 341
Using WMI against remote systems 341
Supplying alternate credentials for the remote connection 342
Using Windows PowerShell remoting to run WMI 345
Using CIM classes to query WMI classes 346
Working with remote results 348
Reducing data via Windows PowerShell parameters 352
Reducing data via WQL query 353
Running WMI jobs 355
Using Windows PowerShell remoting and WMI: Step-by-step exercises 357
Chapter 12 quick reference 360
Chapter 13: Calling WMI methods on WMI classes 361
Using WMI cmdlets to execute instance methods 361
Using the Terminate method directly 363
Using the Invoke-WmiMethod cmdlet 365
Using the [wmi] type accelerator 366
Using WMI cmdlets to work with static methods 367
Executing instance methods: Step-by-step exercises 370
Chapter 13 quick reference 373
Chapter 14: Using the CIM cmdlets 375
Using the CIM cmdlets to explore WMI classes 375
Using the Get-CimClass cmdlet and the -ClassName parameter 375
Finding WMI class methods 377
Filtering classes by qualifier 379
Retrieving WMI instances 383
Reducing returned properties and instances 383
Cleaning up output from the command 384
Working with associations 385
Retrieving WMI instances: Step-by-step exercises 392
Chapter 14 quick reference 394
Chapter 15: Working with Active Directory 395
Creating objects in Active Directory 395
Creating an OU 395
ADSI providers 397
LDAP names 399
Creating users 405
What is user account control? 408
Working with users 409
Creating multiple OUs: Step-by-step exercises 423
Chapter 15 quick reference 429
Chapter 16: Working with the AD DS module 431
Understanding the Active Directory module 431
Installing the Active Directory module 431
Getting started with the Active Directory module 433
Using the Active Directory module 433
Finding the FSMO role holders 435
Discovering Active Directory 439
Renaming Active Directory sites 442
Managing users 443
Creating a user 446
Finding and unlocking Active Directory user accounts 447
Finding disabled users 449
Finding unused user accounts 451
Updating Active Directory objects: Step-by-step exercises 454
Chapter 16 quick reference 457
Chapter 17: Deploying Active Directory by using Windows PowerShell 459
Using the Active Directory module to deploy a new forest 459
Adding a new domain controller to an existing domain 465
Adding a read-only domain controller 468
Installing domain controller prerequisites and adding to a forest: Step-by-step exercises 470
Chapter 17 quick reference 472
Chapter 18: Debugging scripts 473
Understanding debugging in Windows PowerShell 473
Understanding the three different types of errors 473
Using the Set-PSDebug cmdlet 479
Tracing the script 479
Stepping through the script 483
Enabling strict mode 488
Debugging the script 492
Setting breakpoints 492
Setting a breakpoint on a line number 492
Setting a breakpoint on a variable 495
Setting a breakpoint on a command 499
Responding to breakpoints 501
Listing breakpoints 503
Enabling and disabling breakpoints 504
Deleting breakpoints 504
Debugging a function: Step-by-step exercises 505
Chapter 18 quick reference 509
Chapter 19: Handling errors 511
Handling missing parameters 511
Creating a default value for a parameter 512
Making the parameter mandatory 513
Limiting choices 514
Using PromptForChoice to limit selections 514
Using Test-Connection to identify computer connectivity 516
Using the -contains operator to examine the contents of an array 517
Using the -contains operator to test for properties 519
Handling missing rights 521
Using an attempt-and-fail approach 522
Checking for rights and exiting gracefully 522
Handling missing WMI providers 523
Handling incorrect data types 532
Handling out-of-bounds errors 536
Using a boundary-checking function 536
Placing limits on the parameter 537
Using Try...Catch...Finally 538
Catching multiple errors 541
Using PromptForChoice to limit selections and using Try…Catch…Finally: Step-by-step exercises 544
Chapter 19 quick reference 546
Chapter 20: Using the Windows PowerShell workflow 547
Why use workflows? 547
Workflow requirements 548
A simple workflow 548
Parallel PowerShell 549
Workflow activities 552
Windows PowerShell cmdlets as activities 553
Disallowed core cmdlets 554
Non-automatic cmdlet activities 554
Parallel activities 555
Checkpointing Windows PowerShell workflow 556
Understanding checkpoints 556
Placing checkpoints 556
Adding checkpoints 556
Adding a sequence activity to a workflow 559
Creating a workflow and adding checkpoints: Step-by-step exercises 561
Chapter 20 quick reference 563
Chapter 21: Managing Windows PowerShell DSC 565
Understanding Desired State Configuration 565
The DSC process 566
Configuration parameters 568
Setting dependencies 570
Controlling configuration drift 571
Modifying environment variables 573
Creating a DSC configuration and adding a dependency: Step-by-step exercises 576
Chapter 21 quick reference 580
Chapter 22: Using the PowerShell Gallery 581
Exploring the PowerShell Gallery 581
Configuring and using PowerShell Get 583
Installing a module from the PowerShell Gallery 585
Configuring trusted installation locations 586
Uninstalling a module 586
Searching for and installing modules from the PowerShell Gallery: Step-by-step exercises 587
Chapter 22 quick reference 589
Appendix A: Windows PowerShell scripting best practices 591
Appendix B: Regular expressions quick reference 599
Index 603
About the author 631