Windows Powershell Cookbook Second Edition
Second Edition Lee Holmes download
https://ebookbell.com/product/windows-powershell-cookbook-second-
edition-second-edition-lee-holmes-2539686
Explore and download more ebooks at ebookbell.com
Here are some recommended products that we believe you will be
interested in. You can click the link to download.
Windows Powershell Cookbook The Complete Guide To Scripting Microsofts
Command Shell 3rd Edition Lee Holmes
https://ebookbell.com/product/windows-powershell-cookbook-the-
complete-guide-to-scripting-microsofts-command-shell-3rd-edition-lee-
holmes-49431868
Windows Powershell Cookbook Lee Holmes
https://ebookbell.com/product/windows-powershell-cookbook-lee-
holmes-2467022
Windows Server Automation With Powershell Cookbook Powerful Ways To
Automate Manage And Administrate Windows Server 2022 Using Powershell
72 5th Edition 5th Edition Thomas Lee
https://ebookbell.com/product/windows-server-automation-with-
powershell-cookbook-powerful-ways-to-automate-manage-and-administrate-
windows-server-2022-using-powershell-72-5th-edition-5th-edition-
thomas-lee-49114086
Windows Server Automation With Powershell Cookbook Powerful Ways To
Automate And Manage Windows Administrative Tasks 4th Ed Thomas Lee
https://ebookbell.com/product/windows-server-automation-with-
powershell-cookbook-powerful-ways-to-automate-and-manage-windows-
administrative-tasks-4th-ed-thomas-lee-55391036
Windows Server 2016 Automation With Powershell Cookbook Automate
Manual Administrative Tasks With Ease 2nd Edition Lee
https://ebookbell.com/product/windows-server-2016-automation-with-
powershell-cookbook-automate-manual-administrative-tasks-with-
ease-2nd-edition-lee-20640372
Windows Server 2012 Automation With Powershell Cookbook Ed Goad
https://ebookbell.com/product/windows-server-2012-automation-with-
powershell-cookbook-ed-goad-4148828
Windows Server Automation With Powershell Cookbook Powerful Ways To
Automate Manage And Administrate Windows Server 5th Ed 5th Thomas Lee
https://ebookbell.com/product/windows-server-automation-with-
powershell-cookbook-powerful-ways-to-automate-manage-and-administrate-
windows-server-5th-ed-5th-thomas-lee-47604122
Windows Server 2019 Automation With Powershell Cookbook Third Edition
Thomas Lee
https://ebookbell.com/product/windows-server-2019-automation-with-
powershell-cookbook-third-edition-thomas-lee-49851834
Microsoft Sharepoint 2010 And Windows Powershell 20 Expert Cookbook
Yaroslav Pentsarskyy
https://ebookbell.com/product/microsoft-sharepoint-2010-and-windows-
powershell-20-expert-cookbook-yaroslav-pentsarskyy-2421432
Windows PowerShell Cookbook
SECOND EDITION
Windows PowerShell Cookbook
Lee Holmes
Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo
Windows PowerShell Cookbook, Second Edition
by Lee Holmes
Copyright © 2010 Lee Holmes. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor: Mike Hendrickson
Production Editor: Teresa Elsey
Copyeditor: Genevieve d’Entremont
Proofreader: Teresa Elsey
Indexer: Newgen North America, Inc.
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Printing History:
October 2007: First Edition.
August 2010: Second Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Windows PowerShell Cookbook, the image of a box turtle, and related trade dress
are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-0-596-80150-2
[M]
1281554603
Table of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Foreword to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
Part I. Tour
A Guided Tour of Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Part II. Fundamentals
1. The Windows PowerShell Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.1 Run Programs, Scripts, and Existing Tools 19
1.2 Resolve Errors Calling Native Executables 21
1.3 Run a PowerShell Command 23
1.4 Invoke a Long-Running or Background Command 24
1.5 Notify Yourself of Job Completion 27
1.6 Customize Your Shell, Profile, and Prompt 28
1.7 Find a Command to Accomplish a Task 31
1.8 Get Help on a Command 32
1.9 Program: Search Help for Text 34
1.10 Program: View PowerShell’s HTML Help 36
1.11 Launch PowerShell at a Specific Location 37
1.12 Invoke a PowerShell Command or Script from Outside PowerShell 38
1.13 Customize the Shell to Improve Your Productivity 40
1.14 Program: Learn Aliases for Common Commands 42
1.15 Program: Learn Aliases for Common Parameters 44
1.16 Access and Manage Your Console History 46
1.17 Program: Create Scripts from Session History 48
v
1.18 Invoke a Command from Your Session History 49
1.19 Program: Search Formatted Output for a Pattern 51
1.20 Interactively View and Process Command Output 52
1.21 Store the Output of a Command into a File 54
1.22 Add Information to the End of a File 55
1.23 Record a Transcript of Your Shell Session 55
1.24 Extend Your Shell with Additional Commands 56
1.25 Use Commands from Customized Shells 57
1.26 Save State Between Sessions 59
2. Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.1 Filter Items in a List or Command Output 64
2.2 Group and Pivot Data by Name 65
2.3 Program: Simplify Most Where-Object Filters 68
2.4 Program: Interactively Filter Lists of Objects 70
2.5 Work with Each Item in a List or Command Output 72
2.6 Automate Data-Intensive Tasks 74
2.7 Program: Simplify Most Foreach-Object Pipelines 78
2.8 Intercept Stages of the Pipeline 80
2.9 Automatically Capture Pipeline Output 81
2.10 Capture and Redirect Binary Process Output 83
3. Variables and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.1 Display the Properties of an Item as a List 90
3.2 Display the Properties of an Item as a Table 92
3.3 Store Information in Variables 94
3.4 Access Environment Variables 95
3.5 Program: Retain Changes to Environment Variables Set by a Batch
File 98
3.6 Control Access and Scope of Variables and Other Items 100
3.7 Program: Create a Dynamic Variable 102
3.8 Work with .NET Objects 104
3.9 Create an Instance of a .NET Object 108
3.10 Program: Create Instances of Generic Objects 110
3.11 Reduce Typing for Long Class Names 113
3.12 Use a COM Object 115
3.13 Learn About Types and Objects 115
3.14 Get Detailed Documentation About Types and Objects 117
3.15 Add Custom Methods and Properties to Objects 119
3.16 Create and Initialize Custom Objects 121
3.17 Add Custom Methods and Properties to Types 125
vi | Table of Contents
4. Looping and Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
4.1 Make Decisions with Comparison and Logical Operators 131
4.2 Adjust Script Flow Using Conditional Statements 133
4.3 Manage Large Conditional Statements with Switches 135
4.4 Repeat Operations with Loops 136
4.5 Add a Pause or Delay 139
5. Strings and Unstructured Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
5.1 Create a String 141
5.2 Create a Multiline or Formatted String 143
5.3 Place Special Characters in a String 144
5.4 Insert Dynamic Information in a String 144
5.5 Prevent a String from Including Dynamic Information 146
5.6 Place Formatted Information in a String 147
5.7 Search a String for Text or a Pattern 148
5.8 Replace Text in a String 151
5.9 Split a String on Text or a Pattern 152
5.10 Combine Strings into a Larger String 154
5.11 Convert a String to Upper/Lowercase 156
5.12 Trim a String 157
5.13 Format a Date for Output 158
5.14 Program: Convert Text Streams to Objects 160
5.15 Generate Large Reports and Text Streams 164
5.16 Generate Source Code and Other Repetitive Text 166
6. Calculations and Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
6.1 Perform Simple Arithmetic 171
6.2 Perform Complex Arithmetic 173
6.3 Measure Statistical Properties of a List 175
6.4 Work with Numbers as Binary 177
6.5 Simplify Math with Administrative Constants 180
6.6 Convert Numbers Between Bases 181
7. Lists, Arrays, and Hashtables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
7.1 Create an Array or List of Items 183
7.2 Create a Jagged or Multidimensional Array 185
7.3 Access Elements of an Array 186
7.4 Visit Each Element of an Array 188
7.5 Sort an Array or List of Items 189
7.6 Determine Whether an Array Contains an Item 190
7.7 Combine Two Arrays 191
7.8 Find Items in an Array That Match a Value 192
7.9 Compare Two Lists 193
Table of Contents | vii
7.10 Remove Elements from an Array 193
7.11 Find Items in an Array Greater or Less Than a Value 194
7.12 Use the ArrayList Class for Advanced Array Tasks 195
7.13 Create a Hashtable or Associative Array 197
7.14 Sort a Hashtable by Key or Value 198
8. Utility Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
8.1 Get the System Date and Time 201
8.2 Measure the Duration of a Command 202
8.3 Read and Write from the Windows Clipboard 203
8.4 Generate a Random Number or Object 206
8.5 Program: Search the Windows Start Menu 208
8.6 Program: Show Colorized Script Content 209
Part III. Common Tasks
9. Simple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
9.1 Get the Content of a File 217
9.2 Search a File for Text or a Pattern 219
9.3 Parse and Manage Text-Based Logfiles 222
9.4 Parse and Manage Binary Files 224
9.5 Create a Temporary File 227
9.6 Search and Replace Text in a File 228
9.7 Program: Get the Encoding of a File 231
9.8 Program: View the Hexadecimal Representation of Content 233
10. Structured Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
10.1 Access Information in an XML File 237
10.2 Perform an XPath Query Against XML 240
10.3 Convert Objects to XML 242
10.4 Modify Data in an XML File 243
10.5 Easily Import and Export Your Structured Data 245
10.6 Store the Output of a Command in a CSV or Delimited File 247
10.7 Import CSV and Delimited Data from a File 248
10.8 Use Excel to Manage Command Output 249
10.9 Parse and Interpret PowerShell Scripts 251
11. Code Reuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
11.1 Write a Script 255
11.2 Write a Function 258
11.3 Find a Verb Appropriate for a Command Name 260
11.4 Write a Script Block 261
viii | Table of Contents
11.5 Return Data from a Script, Function, or Script Block 263
11.6 Package Common Commands in a Module 265
11.7 Write Commands That Maintain State 268
11.8 Selectively Export Commands from a Module 270
11.9 Diagnose and Interact with Internal Module State 272
11.10 Handle Cleanup Tasks When a Module Is Removed 274
11.11 Access Arguments of a Script, Function, or Script Block 276
11.12 Add Validation to Parameters 280
11.13 Accept Script Block Parameters with Local Variables 284
11.14 Dynamically Compose Command Parameters 286
11.15 Provide -WhatIf, -Confirm, and Other Cmdlet Features 287
11.16 Add Help to Scripts or Functions 290
11.17 Add Custom Tags to a Function or Script Block 292
11.18 Access Pipeline Input 295
11.19 Write Pipeline-Oriented Scripts with Cmdlet Keywords 296
11.20 Write a Pipeline-Oriented Function 300
11.21 Organize Scripts for Improved Readability 301
11.22 Invoke Dynamically Named Commands 303
11.23 Program: Enhance or Extend an Existing Cmdlet 304
12. Internet-Enabled Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
12.1 Download a File from the Internet 313
12.2 Download a Web Page from the Internet 314
12.3 Program: Get-PageUrls 318
12.4 Connect to a Web Service 321
12.5 Program: Connect-WebService 323
12.6 Export Command Output as a Web Page 326
12.7 Send an Email 327
12.8 Program: Send-MailMessage 328
12.9 Program: Interact with Internet Protocols 329
13. User Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
13.1 Read a Line of User Input 335
13.2 Read a Key of User Input 336
13.3 Program: Display a Menu to the User 337
13.4 Display Messages and Output to the User 339
13.5 Provide Progress Updates on Long-Running Tasks 342
13.6 Write Culture-Aware Scripts 344
13.7 Support Other Languages in Script Output 347
13.8 Program: Invoke a Script Block with Alternate Culture Settings 349
13.9 Access Features of the Host’s User Interface 350
13.10 Program: Add a Graphical User Interface to Your Script 352
13.11 Interact with UI Frameworks and STA Objects 355
Table of Contents | ix
14. Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
14.1 Prevent Common Scripting Errors 360
14.2 Trace Script Execution 362
14.3 Set a Script Breakpoint 365
14.4 Debug a Script When It Encounters an Error 368
14.5 Create a Conditional Breakpoint 370
14.6 Investigate System State While Debugging 372
14.7 Program: Watch an Expression for Changes 375
14.8 Program: Get Script Code Coverage 377
15. Tracing and Error Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
15.1 Determine the Status of the Last Command 381
15.2 View the Errors Generated by a Command 383
15.3 Manage the Error Output of Commands 385
15.4 Program: Resolve an Error 386
15.5 Configure Debug, Verbose, and Progress Output 387
15.6 Handle Warnings, Errors, and Terminating Errors 389
15.7 Output Warnings, Errors, and Terminating Errors 392
15.8 Program: Analyze a Script’s Performance Profile 393
16. Environmental Awareness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
16.1 View and Modify Environment Variables 399
16.2 Access Information About Your Command’s Invocation 401
16.3 Program: Investigate the InvocationInfo Variable 403
16.4 Find Your Script’s Name 405
16.5 Find Your Script’s Location 406
16.6 Find the Location of Common System Paths 407
16.7 Get the Current Location 409
16.8 Safely Build File Paths Out of Their Components 411
16.9 Interact with PowerShell’s Global Environment 411
16.10 Determine PowerShell Version Information 412
17. Extend the Reach of Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
17.1 Automate Programs Using COM Scripting Interfaces 415
17.2 Program: Query a SQL Data Source 416
17.3 Access Windows Performance Counters 419
17.4 Access Windows API Functions 422
17.5 Program: Invoke Simple Windows API Calls 428
17.6 Define or Extend a .NET Class 431
17.7 Add Inline C# to Your PowerShell Script 434
17.8 Access a .NET SDK Library 436
17.9 Create Your Own PowerShell Cmdlet 438
17.10 Add PowerShell Scripting to Your Own Program 440
x | Table of Contents
18. Security and Script Signing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
18.1 Enable Scripting Through an Execution Policy 446
18.2 Disable Warnings for UNC Paths 449
18.3 Sign a PowerShell Script, Module, or Formatting File 450
18.4 Program: Create a Self-Signed Certificate 452
18.5 Manage PowerShell Security in an Enterprise 453
18.6 Block Scripts by Publisher, Path, or Hash 455
18.7 Verify the Digital Signature of a PowerShell Script 457
18.8 Securely Handle Sensitive Information 458
18.9 Securely Request Usernames and Passwords 460
18.10 Program: Start a Process as Another User 461
18.11 Program: Run a Temporarily Elevated Command 463
18.12 Securely Store Credentials on Disk 465
18.13 Access User and Machine Certificates 467
18.14 Program: Search the Certificate Store 468
18.15 Add and Remove Certificates 470
18.16 Manage Security Descriptors in SDDL Form 471
19. Integrated Scripting Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
19.1 Debug a Script 475
19.2 Customize Text and User Interface Colors 477
19.3 Connect to a Remote Computer 479
19.4 Extend ISE Functionality Through Its Object Model 479
19.5 Add an Item to the Tools Menu 481
Part IV. Administrator Tasks
20. Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
20.1 Determine the Current Location 486
20.2 Get the Files in a Directory 487
20.3 Find All Files Modified Before a Certain Date 488
20.4 Clear the Content of a File 489
20.5 Manage and Change the Attributes of a File 490
20.6 Find Files That Match a Pattern 491
20.7 Manage Files That Include Special Characters 494
20.8 Program: Get Disk Usage Information 495
20.9 Monitor a File for Changes 497
20.10 Get the Version of a DLL or Executable 497
20.11 Program: Get the MD5 or SHA1 Hash of a File 498
20.12 Create a Directory 501
20.13 Remove a File or Directory 502
20.14 Rename a File or Directory 502
Table of Contents | xi
20.15 Move a File or Directory 504
20.16 Program: Move or Remove a Locked File 504
20.17 Get the ACL of a File or Directory 506
20.18 Set the ACL of a File or Directory 508
20.19 Program: Add Extended File Properties to Files 509
20.20 Program: Create a Filesystem Hard Link 511
20.21 Program: Create a ZIP Archive 513
21. The Windows Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
21.1 Navigate the Registry 517
21.2 View a Registry Key 518
21.3 Modify or Remove a Registry Key Value 519
21.4 Create a Registry Key Value 520
21.5 Remove a Registry Key 521
21.6 Safely Combine Related Registry Modifications 522
21.7 Add a Site to an Internet Explorer Security Zone 524
21.8 Modify Internet Explorer Settings 526
21.9 Program: Search the Windows Registry 527
21.10 Get the ACL of a Registry Key 529
21.11 Set the ACL of a Registry Key 530
21.12 Work with the Registry of a Remote Computer 531
21.13 Program: Get Registry Items from Remote Machines 533
21.14 Program: Get Properties of Remote Registry Keys 535
21.15 Program: Set Properties of Remote Registry Keys 537
21.16 Discover Registry Settings for Programs 539
22. Comparing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
22.1 Compare the Output of Two Commands 543
22.2 Determine the Differences Between Two Files 545
22.3 Verify Integrity of File Sets 545
23. Event Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
23.1 List All Event Logs 549
23.2 Get the Newest Entries from an Event Log 551
23.3 Find Event Log Entries with Specific Text 552
23.4 Retrieve and Filter Event Log Entries 553
23.5 Find Event Log Entries by Their Frequency 556
23.6 Back Up an Event Log 558
23.7 Create or Remove an Event Log 560
23.8 Write to an Event Log 561
23.9 Run a PowerShell Script for Windows Event Log Entries 562
23.10 Clear or Maintain an Event Log 563
23.11 Access Event Logs of a Remote Machine 565
xii | Table of Contents
24. Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
24.1 List Currently Running Processes 570
24.2 Launch the Application Associated with a Document 571
24.3 Launch a Process 572
24.4 Stop a Process 573
24.5 Debug a Process 575
25. System Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
25.1 List All Running Services 577
25.2 Manage a Running Service 579
25.3 Configure a Service 580
26. Active Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
26.1 Test Active Directory Scripts on a Local Installation 582
26.2 Create an Organizational Unit 585
26.3 Get the Properties of an Organizational Unit 586
26.4 Modify Properties of an Organizational Unit 587
26.5 Delete an Organizational Unit 587
26.6 Get the Children of an Active Directory Container 588
26.7 Create a User Account 589
26.8 Program: Import Users in Bulk to Active Directory 590
26.9 Search for a User Account 592
26.10 Get and List the Properties of a User Account 593
26.11 Modify Properties of a User Account 594
26.12 Change a User Password 594
26.13 Create a Security or Distribution Group 595
26.14 Search for a Security or Distribution Group 596
26.15 Get the Properties of a Group 597
26.16 Find the Owner of a Group 598
26.17 Modify Properties of a Security or Distribution Group 599
26.18 Add a User to a Security or Distribution Group 600
26.19 Remove a User from a Security or Distribution Group 600
26.20 List a User’s Group Membership 601
26.21 List the Members of a Group 602
26.22 List the Users in an Organizational Unit 602
26.23 Search for a Computer Account 603
26.24 Get and List the Properties of a Computer Account 604
27. Enterprise Computer Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
27.1 Join a Computer to a Domain or Workgroup 607
27.2 Remove a Computer from a Domain 608
27.3 Program: List Logon or Logoff Scripts for a User 609
27.4 Program: List Startup or Shutdown Scripts for a Machine 610
Table of Contents | xiii
27.5 Deploy PowerShell-Based Logon Scripts 612
27.6 Enable or Disable the Windows Firewall 612
27.7 Open or Close Ports in the Windows Firewall 613
27.8 Program: List All Installed Software 614
27.9 Uninstall an Application 616
27.10 Manage Computer Restore Points 617
27.11 Reboot or Shut Down a Computer 619
27.12 Determine Whether a Hotfix Is Installed 620
27.13 Manage Scheduled Tasks on a Computer 621
27.14 Retrieve Printer Information 622
27.15 Retrieve Printer Queue Statistics 623
27.16 Manage Printers and Print Queues 624
27.17 Program: Summarize System Information 626
27.18 Renew a DHCP Lease 627
27.19 Assign a Static IP Address 629
27.20 List All IP Addresses for a Computer 630
27.21 List Network Adapter Properties 631
28. Windows Management Instrumentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
28.1 Access Windows Management Instrumentation Data 635
28.2 Modify the Properties of a WMI Instance 637
28.3 Invoke a Method on a WMI Class 639
28.4 Program: Determine Properties Available to WMI Filters 640
28.5 Program: Search for WMI Classes 642
28.6 Use .NET to Perform Advanced WMI Tasks 645
28.7 Improve the Performance of Large-Scale WMI Operations 646
28.8 Convert a VBScript WMI Script to PowerShell 647
29. Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
29.1 Find Commands That Support Their Own Remoting 652
29.2 Program: Invoke a PowerShell Expression on a Remote Machine 653
29.3 Test Connectivity Between Two Computers 656
29.4 Limit Networking Scripts to Hosts That Respond 658
29.5 Enable PowerShell Remoting on a Computer 659
29.6 Enable Remote Desktop on a Computer 661
29.7 Program: Remotely Enable PowerShell Remoting 662
29.8 Configure User Permissions for Remoting 664
29.9 Enable Remoting to Workgroup Computers 665
29.10 Interactively Manage a Remote Computer 667
29.11 Invoke a Command on a Remote Computer 669
29.12 Implicitly Invoke Commands from a Remote Computer 673
29.13 Create Sessions with Full Network Access 676
29.14 Pass Variables to Remote Sessions 680
xiv | Table of Contents
29.15 Configure Advanced Remoting Options 681
29.16 Invoke a Command on Many Computers 683
29.17 Run a Local Script on a Remote Computer 684
29.18 Program: Transfer a File to a Remote Computer 685
29.19 Determine Whether a Script Is Running on a Remote Computer 688
29.20 Program: Create a Task-Specific Remoting Endpoint 688
30. Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
30.1 Safely Experiment with Transactions 695
30.2 Change Error Recovery Behavior in Transactions 697
31. Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
31.1 Respond to Automatically Generated Events 700
31.2 Create and Respond to Custom Events 703
31.3 Create a Temporary Event Subscription 706
31.4 Forward Events from a Remote Computer 707
31.5 Investigate Internal Event Action State 708
31.6 Use a Script Block as a .NET Delegate or Event Handler 710
Part V. References
A. PowerShell Language and Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
B. Regular Expression Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
C. XPath Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
D. .NET String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
E. .NET DateTime Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781
F. Selected .NET Classes and Their Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
G. WMI Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
H. Selected COM Objects and Their Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
I. Selected Events and Their Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
J. Standard PowerShell Verbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
Table of Contents | xv
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
xvi | Table of Contents
Foreword
As someone who has written, or contributed to, more than a dozen books, I am well
aware of the incredible amount of work and monumental commitment of time and
resources involved with writing a book. That someone would choose to do this at
essentially the same time one is burning the midnight oil while developing one of the
most exciting products in Microsoft’s history bespeaks a most committed person.
However, more than simple commitment is involved. From my conversations with Lee,
I can tell that he is passionate about Windows PowerShell. He sees the revolutionary
changes introduced with the 2.0 release of the product. If Windows PowerShell 1.0 was
the concept, Windows PowerShell 2.0 is the answer. If Windows PowerShell 1.0 was
the vision, Windows PowerShell 2.0 is the reality. If Windows PowerShell 1.0 was for
early adopters, Windows PowerShell 2.0 is moving into the mainstream.
With the inclusion of Windows PowerShell 2.0 in Windows 7 and Windows Server
2008 R2, we are beginning to see the commitment Microsoft is making to the product.
That the SharePoint, SQL, Exchange, Active Directory Domain Services (AD DS), In-
ternet Information Server (IIS) teams, and others have all made cmdlets should tell you
that Windows PowerShell is not a passing fad. Windows PowerShell questions are
already cropping up on Microsoft Certification Examinations, and as a network ad-
ministrator or a consultant, you will need to learn Windows PowerShell.
Learning Windows PowerShell need not be tedious, boring, or exhausting. In fact, you
will be joining a community that is at least as passionate about Windows PowerShell
as Lee (or the rest of the Windows PowerShell team) or me (I write the Hey, Scripting
Guy! blog seven days a week—the only Microsoft blog updated daily, by the way).
What other product from Microsoft has inspired a half dozen songs to be written about
it? Not by the marketing department, but by people who fell in love with Windows
PowerShell, or, better yet, to use the community term: became addicted.
I attended a recent SQL Saturday in Charlotte, North Carolina, because I wanted to
meet and interact with members of the Windows PowerShell community. That is right:
there is a huge group of hardcore SQL administrators who are adopting Windows
PowerShell because of its cool server management capabilities. In addition, a project
known as the SQL Server PowerShell Extensions (SQLPSX) module (available from
CodePlex) has wrapped much of the SQL Management Objects (SMO) into more than
xvii
130 useful functions. This provides ease of use for people who are not experts with
SQL SMO and Windows PowerShell. By leveraging modules, the community is taking
advantage of one of the great new features of Windows PowerShell 2.0. In fact, there
are more than 200 Windows PowerShell projects on CodePlex. One person presenting
at SQL Saturday declared that the active Windows PowerShell community was one of
thegreatstrengthsofWindowsPowerShell.Youarenotalonewhenitcomestolearning
and implementing Windows PowerShell.
I do not own every Windows PowerShell book ever written. I have probably looked at
most Windows PowerShell books, but I found some of them redundant and some
others confusing. However, a few of the Windows PowerShell books are essential. Lee’s
1.0 version of this book fell into that category. I keep it within arm’s length of my desk
and grab it often. I have highlighted certain sections, dog-eared others, and placed
sticky tabs on the more essential pages. Over the last couple years, Lee’s Windows
PowerShell Cookbook has grown to look more like a skinny porcupine on a bad hair
day than a typical scripting book—and that is a good thing, because his book is not a
typical scripting book; it is a cookbook. Just like a “real cookbook” that contains recipes
for meals, this fascinating volume is what I find myself thumbing through when I am
hungry to try something new with Windows PowerShell.
In reviewing Lee’s upgraded Windows PowerShell Cookbook, I see that I will not be
placed on a diet of “foo” and “bar”; instead, there are tasty morsels such as Get-
PageUrls, a way-cool script that illustrates using regular expressions to extract URLs
from a downloaded web page. It even fixes relative URLs so that they include the server
from which they originated. All this happens faster than you can say “super useful”
three times.
I found Chapter 14 on debugging to be well worth a careful read. Lee has a number of
really good points, the premier one being: do not make the mistake in the first place.
This echoes my own best practice. Of course, mistakes are made, errors are introduced,
and that is when the debugger commands are called upon. Windows PowerShell 2.0
ships with some great debugging cmdlets, and Lee has some extremely cool scripts to
simplify the process, or at least to reduce some of the tedium. I really like the Watch-
Expression script because it automatically displays the values of expressions you wish
to track.
If Chapter 14 is worth a careful read, Chapter 18 is worth a sticky tab because you will
find yourself coming back to it often. Security and script signing is a subject of much
debate in the Windows PowerShell community. You will want to hear about security
from the horse’s mouth. A common question I hear when giving presentations on
Windows PowerShell is “How can I invoke a command as another user without switch-
ing contexts?” The genesis of this question is, of course, the Unix sudo command. Lee
has a useful script named Invoke-ElevatedCommand that allows you to accomplish this
task. Most excellent.
xviii | Foreword
One other thing you need to read about is the Windows PowerShell Integrated Scripting
Environment (ISE), in Chapter 19. A common request for years was for Microsoft to
write a script editor. For years, I have been telling people we did write a script editor—
Notepad. The Windows PowerShell ISE is much better than Notepad. Not only is the
Windows PowerShell ISE a great script editor in its own right, but the Windows
PowerShell team also exposed an object model that allows you to modify its behavior
and to configure it to work in the way you wish to work. Lee has a whole section in
Chapter 19 that talks about the ISE and how to modify it.
Working with files, directories, the registry, services, processes, WMI, remoting, trans-
actions, and event handling—it is all in this book. I am not going to go over all that,
because I do not want to spoil the plot. Suffice it to say that once this book sees print,
it will rapidly join its dog-eared younger brother in that small collection of Windows
PowerShell books that I consider essential.
—Ed Wilson
Microsoft Scripting Guy and author of Windows PowerShell 2.0 Best Practices
Foreword | xix
Foreword to the First Edition
When Lee asked me to write the foreword to his new book I was pleasantly surprised.
I was under the impression that forewords were written by people who were respected
and accomplished in their chosen field. Apparently, that isn’t the case at all. My closest
brush with accomplishment and respect came at a New Year’s celebration long ago and
involved hairspray and a butane lighter. I guess it doesn’t matter too much—I mean,
who reads the foreword to a scripting book anyway, right?
Lee wanted one of the Microsoft Scripting Guys to write the foreword. He wrote this
book for the same hard-working admin scripters who frequent the TechNet Script
Center. Lee thought it would make sense to have an original member of that team
provide some perspective on where Windows admin scripting has been and where,
with Windows PowerShell, it is going.
A lot has happened since Lee and I first spoke about this. I’ve left the Microsoft Scripting
Guys team to work on the WMI SDK, and the Scripting Guys name has become a bit
of a joke, given that the current driving force behind the team is a slight, half-sandwich-
eating lady named Jean Ross. For now, Jean is keeping Greg around to do menial labor
like packing up and shipping Dr. Scripto bobblehead dolls, but we’ll just see what
happens when he finally runs out of topics for his Hey, Scripting Guy column. The
future of scripting could very well be The Scripting Girl.
Glue, Enablers, and a WSH
Whenever I think “perspective” and “scripting”—which is far too often—I think Bob
Wells. Bob takes his scripting very seriously and has been promoting it inside and
outside of Microsoft for years. When I joined the Scripting Guys team, Bob would
preach to me about “glue” and “enablers.” It took some time before I understood why
he was talking about it so often and why finding just the right term for enablers was so
important to him. I now know that it’s because crisply defining these two concepts
establishes a simple, useful framework in which to think about admin scripting. The
glue part is the scripting language itself—the foreachs, ifs, and vars.
xxi
It’s what you use to orchestrate, or glue together, the set of subtasks you need to do to
complete a larger task. The enablers (and, no, we never came up with a better term for
them) are the instruments that actually accomplish each of the subtasks.
This table lists the glue and enablers that we, as Windows scripters, have had available
to us over the years.
Glue Enabler
Cmd.exe batch language Command-line tools (OS, ResKit, Support Tools)
WSH Command-line tools (OS, ResKit, Support Tools)
Automation-enabled COM objects (WMI, ADSI)
Windows PowerShell Command-line tools (OS, ResKit, Support Tools)
Automation-enabled COM objects (WMI, ADSI)
.NET Framework Class Library
Notice how each new environment lets you work with the enablers of the previous
environment. This is important because it lets you carry forward your hard-earned
knowledge. Objectively, we can say that WSH scripting is more powerful than batch
scripting because it provides access to more enablers. You can automate more tasks
because you have access to the additional functionality exposed by automatable COM
objects. Less objectively, you could argue that even if you’re only going to use
command-line tools as enablers, WSH is a better choice than batch because it provides
some really useful glue functionality; advances in available enablers make more things
possible while advances in glue (sometimes) make things more convenient.
WSH scripting is a pretty capable environment. The WMI and ADSI COM libraries
alone provide admins around the world with countless cycles of pain and elation. But
there’s always that pesky task that you just can’t do with WSH, or that requires you to
download a tool from some strangely named website at 2 a.m., when you really
shouldn’t be making decisions about what to install on your production servers. If only
VBScript included the infamous Win32 API among its enablers, then, like those strange
creatures known as developers, you could do anything.
Well, in developer land these days, the .NET Framework Class Library (FCL) is the
new Win32 API. So, what we really need is a scripting environment that includes the
FCL as an enabler. That’s exactly what Windows PowerShell does. In fact, Windows
PowerShell runs in the same environment as that library and, as a result, works seam-
lessly with it. I read a lot of press about the object-pipelining capabilities of Windows
PowerShell. Those capabilities are very cool and represent an excellent advance in the
gluedepartment—anadvancethatcertainlymakesworkingwiththeFCLmorenatural.
But the addition of the FCL as an enabler is the thing that makes Jeffrey et al.’s creation
objectively more powerful than WSH. And even if you don’t run into anything in the
FCL that you need right away, it’s comforting to know that when you make an
xxii | Foreword to the First Edition
investment and develop expertise in this latest environment, you gain access to all the
enablers that your developer counterparts currently have or will have in the foreseeable
future. It should also be comforting to know that if you spend the time to learn Win-
dows PowerShell, that knowledge should last you as long as the .NET Framework lasts
Microsoft.
Windows PowerShell follows in the tradition of WSH by improving on the glue aspect
of its predecessor. One of the real pain points of working with COM objects in WSH
was finding out what properties and methods were available. Unless you shelled out
thebucksforasmarteditor,youlostalotofproductivitycontextswitchingfromwriting
a script and consulting documentation. Not so when working with objects in Windows
PowerShell. Type this at a Windows PowerShell prompt:
$objShell = New-Object -com Shell.Application
$objShell | Get-Member
It does a scripter good, does it not?
That Lee Guy
Hopefully my rambling has convinced you that Windows PowerShell is a good thing
and that it’s worth your time to learn it. Now, why do I think you should learn it by
buying and reading this book?
First off, I should tell you that the Windows PowerShell team is a bunch of odd
ducks.* These folks are obsessed. From Jeffrey Snover on down, they are incredible
teachers who love and believe in their technology so much that it’s difficult to stop them
from teaching you! Even among that bunch of quackers, Lee stands out. Have you ever
heard the sound an Exchange server makes when it cringes? Well, ours cringe when
Lee comes to work and starts answering questions on our internal Windows PowerShell
mailing list. Lee has amassed unique knowledge about how to leverage Windows
PowerShell to address problems that arise in the real world. And he and O’Reilly have
done us a great service by capturing and sharing some of that knowledge in this book.
Windows system admin scripters are the coolest people on the planet. It continues to
be a pleasure to work for you, and I sincerely hope you enjoy the book.
—Dean Tsaltas
Microsoft Scripting Guy Emeritus
* Canadian ducks (Canuck ducks) in many cases.
Foreword to the First Edition | xxiii
Preface
In late 2002, Slashdot posted a story about a “next-generation shell” rumored to be in
development at Microsoft. As a longtime fan of the power unlocked by shells and their
scriptinglanguages,thepostimmediatelycapturedmyinterest.Couldthisshellprovide
the command-line power and productivity I’d long loved on Unix systems?
Since I had just joined Microsoft six months earlier, I jumped at the chance to finally
get to the bottom of a Slashdot-sourced Microsoft Mystery. The post talked about
strong integration with the .NET Framework, so I posted a query to an internal C#
mailing list. I got a response that the project was called “Monad,” which I then used
to track down an internal prototype build.
Prototype was a generous term. In its early stages, the build was primarily a proof of
concept. Want to clear the screen? No problem! Just lean on the Enter key until your
previous commands and output scroll out of view! But even at these early stages, it was
immediately clear that Monad marked a revolution in command-line shells. As with
many things of this magnitude, its beauty was self-evident. Monad passed full-
fidelity .NET objects between its commands. For even the most complex commands,
Monad abolished the (until now, standard) need for fragile text-based parsing. Simple
and powerful data manipulation tools supported this new model, creating a shell both
powerful and easy to use.
I joined the Monad development team shortly after that to help do my part to bring
this masterpiece of technology to the rest of the world. Since then, Monad has grown
to become a real, tangible product—now called Windows PowerShell.
So why write a book about it? And why this book?
Many users have picked up PowerShell for the sake of learning PowerShell. Any tangible
benefits come by way of side effect. Others, though, might prefer to opportunistically
learn a new technology as it solves their needs. How do you use PowerShell to navigate
the filesystem? How can you manage files and folders? Retrieve a web page?
This book focuses squarely on helping you learn PowerShell through task-based solu-
tions to your most pressing problems. Read a recipe, read a chapter, or read the entire
book—regardless, you’re bound to learn something.
xxv
Who This Book Is For
This book helps you use PowerShell to get things done. It contains hundreds of solutions
to specific, real-world problems. For systems management, you’ll find plenty of exam-
ples that show how to manage the filesystem, the Windows Registry, event logs, pro-
cesses, and more. For enterprise administration, you’ll find two entire chapters devoted
to WMI, Active Directory, and other enterprise-focused tasks.
Along the way, you’ll also learn an enormous amount about PowerShell: its features,
its commands, and its scripting language—but you’ll most importantly solve problems.
How This Book Is Organized
This book consists of five main sections: a guided tour of PowerShell, PowerShell fun-
damentals, common tasks, administrator tasks, and a detailed reference.
Part I: Tour
A Guided Tour of Windows PowerShell breezes through PowerShell at a high level. It
introduces PowerShell’s core features:
• An interactive shell
• A new command model
• An object-based pipeline
• A razor-sharp focus on administrators
• A consistent model for learning and discovery
• Ubiquitous scripting
• Integration with critical management technologies
• A consistent model for interacting with data stores
The tour lets you become familiar with PowerShell as a whole. This familiarity will
createamentalframeworkforyoutounderstandthesolutionsfromtherestofthebook.
Part II: Fundamentals
Chapters 1 through 8 cover the fundamentals that underpin the solutions in this book.
This section introduces you to the PowerShell interactive shell, fundamental pipeline
and object concepts, and many features of the PowerShell scripting language.
Part III: Common Tasks
Chapters 9 through 19 cover the tasks you will run into most commonly when starting
to tackle more complex problems in PowerShell. This includes working with simple
and structured files, Internet-connected scripts, code reuse, user interaction, and more.
xxvi | Preface
Part IV: Administrator Tasks
Chapters 20 through 31 focus on the most common tasks in systems and enterprise
management. Chapters 20 through 25 focus on individual systems: the filesystem, the
registry, event logs, processes, services, and more. Chapters 26 and 27 focus on Active
Directory, as well as the typical tasks most common in managing networked or domain-
joined systems.
Part V: References
Many books belch useless information into their appendixes simply to increase page
count. In this book, however, the detailed references underpin an integral and essential
resource for learning and using PowerShell. The appendixes cover:
• The PowerShell language and environment
• Regular expression syntax and PowerShell-focused examples
• XPath quick reference
• .NET string formatting syntax and PowerShell-focused examples
• .NET DateTime formatting syntax and PowerShell-focused examples
• Administrator-friendly .NET classes and their uses
• Administrator-friendly WMI classes and their uses
• Administrator-friendly COM objects and their uses
• Selected events and their uses
• PowerShell’s standard verbs
What You Need to Use This Book
The majority of this book requires only a working installation of Windows PowerShell.
Windows 7 and Windows Server 2008 R2 include Windows PowerShell by default. If
you do not yet have PowerShell installed, you may obtain it by following the download
link at http://www.microsoft.com/PowerShell. This link provides download instructions
for PowerShell on Windows XP, Windows Server 2003, and Windows Vista. For Win-
dows Server 2008, PowerShell comes installed as an optional component that you can
enable through the Control Panel like other optional components.
The Active Directory scripts given in Chapter 26 are most useful when applied to an
enterprise environment, but Recipe 26.1 shows how to install additional software
(Active Directory Lightweight Directory Services, or Active Directory Application
Mode) that lets you run these scripts against a local installation.
Preface | xxvii
Conventions Used in This Book
The following typographical conventions are used in this book:
Plain text
Indicates menu titles, menu options, menu buttons, and keyboard accelerators
Italic
Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames,
directories, and Unix utilities
Constant width
Indicates commands, options, switches, variables, attributes, keys, functions,
types, classes, namespaces, methods, modules, properties, parameters, values, ob-
jects, events, event handlers, tags, macros, or the output from commands
Constant width bold
Shows commands or other text that should be typed literally by the user
Constant width italic
Shows text that should be replaced with user-supplied values
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
Code Examples
Obtaining Code Examples
To obtain electronic versions of the programs and examples given in this book, visit
the Examples link at:
http://www.oreilly.com/catalog/9780596801519
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in
this book in your programs and documentation. You do not need to contact us for
permission unless you’re reproducing a significant portion of the code. For example,
writing a program that uses several chunks of code from this book does not require
permission. Selling or distributing a CD-ROM of examples from O’Reilly books does
require permission. Answering a question by citing this book and quoting example
xxviii | Preface
code does not require permission. Incorporating a significant amount of example code
from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title,
author, publisher, and ISBN. For example: “Windows PowerShell Cookbook by Lee
Holmes. Copyright 2010 Lee Holmes, 978-0-596-80150-2.”
If you feel your use of code examples falls outside fair use or the permission given, feel
free to contact us at permissions@oreilly.com.
Comments and Questions
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at:
http://www.oreilly.com/catalog/9780596801502
To comment or ask technical questions about this book, send email to:
bookquestions@oreilly.com
For more information about our books, conferences, Resource Centers, and the
O’Reilly Network, see our website at:
http://www.oreilly.com
Safari® Books Online
Safari Books Online is an on-demand digital library that lets you easily
search over 7,500 technology and creative reference books and videos to
find the answers you need quickly.
Withasubscription,youcanreadanypageandwatchanyvideofromourlibraryonline.
Read books on your cell phone and mobile devices. Access new titles before they are
available for print, and get exclusive access to manuscripts in development and post
feedback for the authors. Copy and paste code samples, download chapters, bookmark
key sections, and benefit from tons of other time-saving features.
O’Reilly Media has uploaded this book to the Safari Books Online service. To have full
digital access to this book and others on similar topics from O’Reilly and other pub-
lishers, sign up for free at http://my.safaribooksonline.com.
Preface | xxix
Acknowledgments
Writing is the task of crafting icebergs. The heft of the book you hold in your hands is
just a hint of the multiyear, multirelease effort it took to get it there. And by a cast much
larger than me.
The groundwork started decades ago. My parents nurtured my interest in computers
and software, supported an evening-only bulletin board service, put up with “viruses”
that told them to buy a new computer for Christmas, and even listened to me blather
about batch files or how PowerShell compares to Excel. Without their support, who
knows where I’d be.
My family and friends have helped keep me sane for two editions of the book now.
Ariel: you are the light of my life. Robin: thinking of you reminds me each day that
serendipity is still alive and well in this busy world. Thank you to all of my friends and
family for being there for me. You can have me back now. :)
I would not have written either edition of this book without the tremendous influence
of Guy Allen, visionary of the University of Toronto’s Professional Writing program.
Guy: your mentoring forever changed me, just as it molds thousands of others from
English hackers into writers.
Of course, members of the PowerShell team (both new and old) are the ones that made
this a book about PowerShell. Building this product with you has been a unique chal-
lenge and experience—but most of all, a distinct pleasure. In addition to the PowerShell
team, the entire PowerShell community defined this book’s focus. From MVPs, to early
adopters, to newsgroup lurkers: your support, questions, and feedback have been the
inspiration behind each page.
Converting thoughts into print always involves a cast of unsung heroes, even though
each author tries his best to convince the world how important these heroes are.
Thank you to the many technical reviewers who participated in O’Reilly’s Open Feed-
back Publishing System, especially Johannes Rössel, Aleksandar Nikolic, Jerome L.
Cruz, David Moravec, Richard Siddaway, and Andrew Tearle. I truly appreciate you
donating your nights and weekends to help craft something of which we can all be
proud.
To the awesome staff at O’Reilly—Mike Hendrickson, Genevieve d’Entremont, Teresa
Elsey, Laurel Ruma, the O’Reilly Tools Monks, and the production team—your pa-
tience and persistence helped craft a book that holds true to its original vision. You also
ensuredthatthebookdidn’tjustknockaroundinmyheadbutactuallygotoutthedoor.
This book would not have been possible without the support from each and every one
of you.
xxx | Preface
PART I
Tour
TOUR
A Guided Tour of Windows
PowerShell
Introduction
Windows PowerShell promises to revolutionize the world of system management and
command-line shells. From its object-based pipelines, to its administrator focus, to its
enormous reach into other Microsoft management technologies, PowerShell drastically
improves the productivity of administrators and power users alike.
When learning a new technology, it is natural to feel bewildered at first by all the
unfamiliar features and functionality. This perhaps rings especially true for users new
to Windows PowerShell because it may be their first experience with a fully featured
command-line shell. Or worse, they’ve heard stories of PowerShell’s fantastic
integrated scripting capabilities and fear being forced into a world of programming that
they’ve actively avoided until now.
Fortunately, these fears are entirely misguided; PowerShell is a shell that both grows
with you and grows on you. Let’s take a tour to see what it is capable of:
• PowerShell works with standard Windows commands and applications. You don’t
have to throw away what you already know and use.
• PowerShell introduces a powerful new type of command. PowerShell commands
(called cmdlets) share a common Verb-Noun syntax and offer many usability im-
provements over standard commands.
• PowerShell understands objects. Working directly with richly structured objects
makes working with (and combining) PowerShell commands immensely easier
than working in the plain-text world of traditional shells.
• PowerShell caters to administrators. Even with all its advances, PowerShell focuses
strongly on its use as an interactive shell: the experience of entering commands in
a running PowerShell application.
3
• PowerShell supports discovery. Using three simple commands, you can learn and
discover almost anything PowerShell has to offer.
• PowerShell enables ubiquitous scripting. With a fully fledged scripting language
that works directly from the command line, PowerShell lets you automate tasks
with ease.
• PowerShell bridges many technologies. By letting you work with .NET, COM,
WMI, XML, and Active Directory, PowerShell makes working with these previ-
ously isolated technologies easier than ever before.
• PowerShell simplifies management of data stores. Through its provider model,
PowerShell lets you manage data stores using the same techniques you already use
to manage files and folders.
We’ll explore each of these pillars in this introductory tour of PowerShell. If you are
running Windows 7 or Windows 2008 R2, version two of PowerShell is already
installed. If not, visit the download link at http://www.microsoft.com/PowerShell to in-
stall it. PowerShell and its supporting technologies are together referred to as the Win-
dows Management Framework.
An Interactive Shell
At its core, PowerShell is first and foremost an interactive shell. While it supports
scripting and other powerful features, its focus as a shell underpins everything.
Getting started in PowerShell is a simple matter of launching PowerShell.exe rather than
cmd.exe—the shells begin to diverge as you explore the intermediate and advanced
functionality, but you can be productive in PowerShell immediately.
To launch Windows PowerShell, do one of the following:
• Click Start→All Programs→Accessories→Windows PowerShell
• Click Start→Run, and then type “PowerShell”
A PowerShell prompt window opens that’s nearly identical to the traditional command
prompt window of Windows XP, Windows Server 2003, and their many ancestors.
The PS C:Documents and SettingsLee> prompt indicates that PowerShell is ready for
input, as shown in Figure T-1.
Once you’ve launched your PowerShell prompt, you can enter DOS-style and Unix-
style commands to navigate around the filesystem just as you would with any Windows
or Unix command prompt—as in the interactive session shown in Example T-1. In this
example, we use the pushd, cd, dir, pwd, and popd commands to store the current loca-
tion, navigate around the filesystem, list items in the current directory, and then return
to the original location. Try it!
4 | A Guided Tour of Windows PowerShell
Example T-1. Entering many standard DOS- and Unix-style file manipulation commands produces
the same results you get when you use them with any other Windows shell
PS C:Documents and SettingsLee> function Prompt { "PS > " }
PS > pushd .
PS > cd 
PS > dir
Directory: C:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 11/2/2006 4:36 AM $WINDOWS.~BT
d---- 5/8/2007 8:37 PM Blurpark
d---- 11/29/2006 2:47 PM Boot
d---- 11/28/2006 2:10 PM DECCHECK
d---- 10/7/2006 4:30 PM Documents and Settings
d---- 5/21/2007 6:02 PM F&SC-demo
d---- 4/2/2007 7:21 PM Inetpub
d---- 5/20/2007 4:59 PM Program Files
d---- 5/21/2007 7:26 PM temp
d---- 5/21/2007 8:55 PM Windows
-a--- 1/7/2006 10:37 PM 0 autoexec.bat
Figure T-1. Windows PowerShell, ready for input
A Guided Tour of Windows PowerShell | 5
-ar-s 11/29/2006 1:39 PM 8192 BOOTSECT.BAK
-a--- 1/7/2006 10:37 PM 0 config.sys
-a--- 5/1/2007 8:43 PM 33057 RUU.log
-a--- 4/2/2007 7:46 PM 2487 secedit.INTEG.RAW
PS > popd
PS > pwd
Path
----
C:Documents and SettingsLee
In this example, our first command customizes the prompt. In cmd.exe, customizing
the prompt looks like prompt $P$G. In bash, it looks like PS1="[h] w> ". In PowerShell,
you define a function that returns whatever you want displayed. Recipe 11.2 introduces
functions and how to write them.
The pushd command is an alternative name (alias) to the much more descriptively
named PowerShell command Push-Location. Likewise, the cd, dir, popd, and pwd com-
mands all have more memorable counterparts.
Although navigating around the filesystem is helpful, so is running the tools you know
and love, such as ipconfig and notepad. Type the command name and you’ll see results
like those shown in Example T-2.
Example T-2. Windows tools and applications such as ipconfig run in PowerShell just as they do in
cmd.exe
PS > ipconfig
Windows IP Configuration
Ethernet adapter Wireless Network Connection 4:
Connection-specific DNS Suffix . : hsd1.wa.comcast.net.
IP Address. . . . . . . . . . . . : 192.168.1.100
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1
PS > notepad
(notepad launches)
Entering ipconfig displays the IP addresses of your current network connections. En-
tering notepad runs—as you’d expect—the Notepad editor that ships with Windows.
Try them both on your own machine.
6 | A Guided Tour of Windows PowerShell
Structured Commands (Cmdlets)
In addition to supporting traditional Windows executables, PowerShell introduces a
powerful new type of command called a cmdlet (pronounced command-let). All cmdlets
are named in a Verb-Noun pattern, such as Get-Process, Get-Content, and Stop-Process.
PS > Get-Process -Name lsass
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
668 13 6228 1660 46 932 lsass
In this example, you provide a value to the ProcessName parameter to get a specific
process by name.
Once you know the handful of common verbs in PowerShell, learning
how to work with new nouns becomes much easier. While you may
never have worked with a certain object before (such as a Service), the
standard Get, Set, Start, and Stop actions still apply. For a list of these
common verbs, see Table J-1 in Appendix J.
You don’t always have to type these full cmdlet names, however. PowerShell lets you
use the Tab key to auto-complete cmdlet names and parameter names:
PS > Get-Pr<TAB> -N<TAB> lsass
For quick interactive use, even that may be too much typing. To help improve your
efficiency, PowerShell defines aliases for all common commands and lets you define
your own. In addition to alias names, PowerShell only requires that you type enough
of the parameter name to disambiguate it from the rest of the parameters in that cmdlet.
PowerShell is also case-insensitive. Using the built-in gps alias (which represents the
Get-Process cmdlet) along with parameter shortening, you can instead type:
PS > gps -n lsass
Going even further, PowerShell supports positional parameters on cmdlets. Positional
parameters let you provide parameter values in a certain position on the command line,
rather than having to specify them by name. The Get-Process cmdlet takes a process
name as its first positional parameter. This parameter even supports wildcards:
PS > gps l*s
A Guided Tour of Windows PowerShell | 7
Deep Integration of Objects
PowerShell begins to flex more of its muscle as you explore the way it handles structured
data and richly functional objects. For example, the following command generates a
simple text string. Since nothing captures that output, PowerShell displays it to you:
PS > "Hello World"
Hello World
The string you just generated is, in fact, a fully functional object from the .NET Frame-
work. For example, you can access its Length property, which tells you how many
characters are in the string. To access a property, you place a dot between the object
and its property name:
PS > "Hello World".Length
11
All PowerShell commands that produce output generate that output as objects as well.
For example, the Get-Process cmdlet generates a System.Diagnostics.Process object,
which you can store in a variable. In PowerShell, variable names start with a $ character.
If you have an instance of Notepad running, the following command stores a reference
to it:
$process = Get-Process notepad
Since this is a fully functional Process object from the .NET Framework, you can call
methods on that object to perform actions on it. This command calls the Kill() meth-
od, which stops a process. To access a method, you place a dot between the object and
its method name:
$process.Kill()
PowerShell supports this functionality more directly through the Stop-Process cmdlet,
but this example demonstrates an important point about your ability to interact with
these rich objects.
Administrators as First-Class Users
While PowerShell’s support for objects from the .NET Framework quickens the pulse
of most users, PowerShell continues to focus strongly on administrative tasks. For ex-
ample, PowerShell supports MB (for megabyte) and GB (for gigabyte) as some of its
standard administrative constants. For example, how many disks will it take to back
up a 40 GB hard drive to CD-ROM?
PS > 40GB / 650MB
63.0153846153846
8 | A Guided Tour of Windows PowerShell
Although the .NET Framework is traditionally a development platform, it contains a
wealth of functionality useful for administrators too! In fact, it makes PowerShell a
great calendar. For example, is 2008 a leap year? PowerShell can tell you:
PS > [DateTime]::IsLeapYear(2008)
True
Going further, how might you determine how much time remains until summer? The
following command converts "06/21/2011" (the start of summer) to a date, and then
subtracts the current date from that. It stores the result in the $result variable, and
then accesses the TotalDays property.
PS > $result = [DateTime] "06/21/2011" - [DateTime]::Now
PS > $result.TotalDays
283.0549285662616
Composable Commands
Whenever a command generates output, you can use a pipeline character (|) to pass
that output directly to another command as input. If the second command understands
the objects produced by the first command, it can operate on the results. You can chain
together many commands this way, creating powerful compositions out of a few simple
operations. For example, the following command gets all items in the Path1 directory
and moves them to the Path2 directory:
Get-Item Path1* | Move-Item -Destination Path2
You can create even more complex commands by adding additional cmdlets to the
pipeline. In Example T-3, the first command gets all processes running on the system.
It passes those to the Where-Object cmdlet, which runs a comparison against each in-
coming item. In this case, the comparison is $_.Handles -ge 500, which checks whether
the Handles property of the current object (represented by the $_ variable) is greater
than or equal to 500. For each object in which this comparison holds true, you pass the
results to the Sort-Object cmdlet, asking it to sort items by their Handles property.
Finally, you pass the objects to the Format-Table cmdlet to generate a table that contains
the Handles, Name, and Description of the process.
Example T-3. You can build more complex PowerShell commands by using pipelines to link cmdlets,
as shown in this example with Get-Process, Where-Object, Sort-Object, and Format-Table
PS > Get-Process |
Where-Object { $_.Handles -ge 500 } |
Sort-Object Handles |
Format-Table Handles,Name,Description -Auto
Handles Name Description
------- ---- -----------
588 winlogon
592 svchost
A Guided Tour of Windows PowerShell | 9
667 lsass
725 csrss
742 System
964 WINWORD Microsoft Office Word
1112 OUTLOOK Microsoft Office Outlook
2063 svchost
Techniques to Protect You from Yourself
While aliases, wildcards, and composable pipelines are powerful, their use in com-
mands that modify system information can easily be nerve-wracking. After all, what
does this command do? Think about it, but don’t try it just yet:
PS > gps [b-t]*[c-r] | Stop-Process
It appears to stop all processes that begin with the letters b through t and end with the
letters c through r. How can you be sure? Let PowerShell tell you. For commands that
modify data, PowerShell supports -WhatIf and -Confirm parameters that let you see
what a command would do:
PS > gps [b-t]*[c-r] | Stop-Process -whatif
What if: Performing operation "Stop-Process" on Target "ctfmon (812)".
What if: Performing operation "Stop-Process" on Target "Ditto (1916)".
What if: Performing operation "Stop-Process" on Target "dsamain (316)".
What if: Performing operation "Stop-Process" on Target "ehrecvr (1832)".
What if: Performing operation "Stop-Process" on Target "ehSched (1852)".
What if: Performing operation "Stop-Process" on Target "EXCEL (2092)".
What if: Performing operation "Stop-Process" on Target "explorer (1900)".
(...)
In this interaction, using the -WhatIf parameter with the Stop-Process pipelined com-
mand lets you preview which processes on your system will be stopped before you
actually carry out the operation.
Note that this example is not a dare! In the words of one reviewer:
Not only did it stop everything, but on Vista, it forced a shutdown with only one minute
warning!
It was very funny though … At least I had enough time to save everything first!
Common Discovery Commands
While reading through a guided tour is helpful, I find that most learning happens in an
ad hoc fashion. To find all commands that match a given wildcard, use the
Get-Command cmdlet. For example, by entering the following, you can find out which
PowerShell commands (and Windows applications) contain the word process.
10 | A Guided Tour of Windows PowerShell
PS > Get-Command *process*
CommandType Name Definition
----------- ---- ----------
Cmdlet Get-Process Get-Process [[-Name] <Str...
Application qprocess.exe c:windowssystem32qproc...
Cmdlet Stop-Process Stop-Process [-Id] <Int32...
To see what a command such as Get-Process does, use the Get-Help cmdlet, like this:
PS > Get-Help Get-Process
Since PowerShell lets you work with objects from the .NET Framework, it provides the
Get-Member cmdlet to retrieve information about the properties and methods that an
object, such as a .NET System.String, supports. Piping a string to the Get-Member com-
mand displays its type name and its members:
PS > "Hello World" | Get-Member
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
(...)
PadLeft Method System.String PadLeft(Int32 tota...
PadRight Method System.String PadRight(Int32 tot...
Remove Method System.String Remove(Int32 start...
Replace Method System.String Replace(Char oldCh...
Split Method System.String[] Split(Params Cha...
StartsWith Method System.Boolean StartsWith(String...
Substring Method System.String Substring(Int32 st...
ToCharArray Method System.Char[] ToCharArray(), Sys...
ToLower Method System.String ToLower(), System....
ToLowerInvariant Method System.String ToLowerInvariant()
ToString Method System.String ToString(), System...
ToUpper Method System.String ToUpper(), System....
ToUpperInvariant Method System.String ToUpperInvariant()
Trim Method System.String Trim(Params Char[]...
TrimEnd Method System.String TrimEnd(Params Cha...
TrimStart Method System.String TrimStart(Params C...
Chars ParameterizedProperty System.Char Chars(Int32 index) {...
Length Property System.Int32 Length {get;}
Ubiquitous Scripting
PowerShell makes no distinction between the commands typed at the command line
and the commands written in a script. Your favorite cmdlets work in scripts and your
favorite scripting techniques (e.g., the foreach statement) work directly on the com-
mand line. For example, to add up the handle count for all running processes:
PS > $handleCount = 0
PS > foreach($process in Get-Process) { $handleCount += $process.Handles }
PS > $handleCount
19403
A Guided Tour of Windows PowerShell | 11
While PowerShell provides a command (Measure-Object) to measure statistics about
collections, this short example shows how PowerShell lets you apply techniques that
normally require a separate scripting or programming language.
In addition to using PowerShell scripting keywords, you can also create and work di-
rectlywithobjectsfromthe.NETFrameworkthatyoumaybefamiliarwith.PowerShell
becomes almost like the C# immediate mode in Visual Studio. Example T-4 shows
how PowerShell lets you easily interact with the .NET Framework.
Example T-4. Using objects from the .NET Framework to retrieve a web page and process its content
PS > $webClient = New-Object System.Net.WebClient
PS > $content = $webClient.DownloadString("http://blogs.msdn.com/PowerShell/rss.aspx")
PS > $content.Substring(0,1000)
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedS
tylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://pu
rl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slas
h/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Windo
(...)
Ad Hoc Development
By blurring the lines between interactive administration and writing scripts, the history
buffers of PowerShell sessions quickly become the basis for ad hoc script development.
In this example, you call the Get-History cmdlet to retrieve the history of your session.
For each item, you get its CommandLine property (the thing you typed) and send the
output to a new script file.
PS > Get-History | Foreach-Object { $_.CommandLine } > c:tempscript.ps1
PS > notepad c:tempscript.ps1
(save the content you want to keep)
PS > c:tempscript.ps1
If this is the first time you’ve run a script in PowerShell, you will need
to configure your Execution Policy. For more information about select-
ing an execution policy, see Recipe 18.1.
For more detail about saving your session history into a script, see Recipe 1.17.
Bridging Technologies
We’ve seen how PowerShell lets you fully leverage the .NET Framework in your tasks,
but its support for common technologies stretches even further. As Example T-5 (con-
tinued from Example T-4) shows, PowerShell supports XML.
12 | A Guided Tour of Windows PowerShell
Example T-5. Working with XML content in PowerShell
PS > $xmlContent = [xml] $content
PS > $xmlContent
xml xml-stylesheet rss
--- -------------- ---
version="1.0" encoding... type="text/xsl" href="... rss
PS > $xmlContent.rss
version : 2.0
dc : http://purl.org/dc/elements/1.1/
slash : http://purl.org/rss/1.0/modules/slash/
wfw : http://wellformedweb.org/CommentAPI/
channel : channel
PS > $xmlContent.rss.channel.item | select Title
title
-----
CMD.exe compatibility
Time Stamping Log Files
Microsoft Compute Cluster now has a PowerShell Provider and Cmdlets
The Virtuous Cycle: .NET Developers using PowerShell
(...)
PowerShell also lets you work with Windows Management Instrumentation (WMI):
PS > Get-WmiObject Win32_Bios
SMBIOSBIOSVersion : ASUS A7N8X Deluxe ACPI BIOS Rev 1009
Manufacturer : Phoenix Technologies, LTD
Name : Phoenix - AwardBIOS v6.00PG
SerialNumber : xxxxxxxxxxx
Version : Nvidia - 42302e31
Or, as Example T-6 shows, Active Directory Service Interfaces (ADSI).
Example T-6. Working with Active Directory in PowerShell
PS > [ADSI] "WinNT://./Administrator" | Format-List *
UserFlags : {66113}
MaxStorage : {-1}
PasswordAge : {19550795}
PasswordExpired : {0}
LoginHours : {255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255}
FullName : {}
Description : {Built-in account for administering the compu
ter/domain}
A Guided Tour of Windows PowerShell | 13
BadPasswordAttempts : {0}
LastLogin : {5/21/2007 3:00:00 AM}
HomeDirectory : {}
LoginScript : {}
Profile : {}
HomeDirDrive : {}
Parameters : {}
PrimaryGroupID : {513}
Name : {Administrator}
MinPasswordLength : {0}
MaxPasswordAge : {3710851}
MinPasswordAge : {0}
PasswordHistoryLength : {0}
AutoUnlockInterval : {1800}
LockoutObservationInterval : {1800}
MaxBadPasswordsAllowed : {0}
RasPermissions : {1}
objectSid : {1 5 0 0 0 0 0 5 21 0 0 0 121 227 252 83 122
130 50 34 67 23 10 50 244 1 0 0}
Or, as Example T-7 shows, even scripting traditional COM objects.
Example T-7. Working with COM objects in PowerShell
PS > $firewall = New-Object -com HNetCfg.FwMgr
PS > $firewall.LocalPolicy.CurrentProfile
Type : 1
FirewallEnabled : True
ExceptionsNotAllowed : False
NotificationsDisabled : False
UnicastResponsesToMulticastBroadcastDisabled : False
RemoteAdminSettings : System.__ComObject
IcmpSettings : System.__ComObject
GloballyOpenPorts : {Media Center Extender Serv
ice, Remote Media Center Ex
perience, Adam Test Instanc
e, QWAVE...}
Services : {File and Printer Sharing,
UPnP Framework, Remote Desk
top}
AuthorizedApplications : {Remote Assistance, Windows
Messenger, Media Center, T
rillian...}
Namespace Navigation Through Providers
Another avenue PowerShell offers for working with the system is providers. PowerShell
providers let you navigate and manage data stores using the same techniques you al-
ready use to work with the filesystem, as illustrated in Example T-8.
14 | A Guided Tour of Windows PowerShell
Example T-8. Navigating the filesystem
PS > Set-Location c:
PS > Get-ChildItem
Directory: C:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 11/2/2006 4:36 AM $WINDOWS.~BT
d---- 5/8/2007 8:37 PM Blurpark
d---- 11/29/2006 2:47 PM Boot
d---- 11/28/2006 2:10 PM DECCHECK
d---- 10/7/2006 4:30 PM Documents and Settings
d---- 5/21/2007 6:02 PM F&SC-demo
d---- 4/2/2007 7:21 PM Inetpub
d---- 5/20/2007 4:59 PM Program Files
d---- 5/21/2007 11:47 PM temp
d---- 5/21/2007 8:55 PM Windows
-a--- 1/7/2006 10:37 PM 0 autoexec.bat
-ar-s 11/29/2006 1:39 PM 8192 BOOTSECT.BAK
-a--- 1/7/2006 10:37 PM 0 config.sys
-a--- 5/1/2007 8:43 PM 33057 RUU.log
-a--- 4/2/2007 7:46 PM 2487 secedit.INTEG.RAW
This also works on the registry, as shown in Example T-9.
Example T-9. Navigating the registry
PS > Set-Location HKCU:SoftwareMicrosoftWindows
PS > Get-ChildItem
Hive: HKEY_CURRENT_USERSoftwareMicrosoftWindows
SKC VC Name Property
--- -- ---- --------
30 1 CurrentVersion {ISC}
3 1 Shell {BagMRU Size}
4 2 ShellNoRoam {(default), BagMRU Size}
PS > Set-Location CurrentVersionRun
PS > Get-ItemProperty .
(...)
FolderShare : "C:Program FilesFolderShareFolderShare.exe" /
background
TaskSwitchXP : d:leetoolsTaskSwitchXP.exe
ctfmon.exe : C:WINDOWSsystem32ctfmon.exe
Ditto : C:Program FilesDittoDitto.exe
(...)
A Guided Tour of Windows PowerShell | 15
Or even the machine’s certificate store, as Example T-10 illustrates.
Example T-10. Navigating the certificate store
PS > Set-Location cert:CurrentUserRoot
PS > Get-ChildItem
Directory: Microsoft.PowerShell.SecurityCertificate::CurrentUserRoot
Thumbprint Subject
---------- -------
CDD4EEAE6000AC7F40C3802C171E30148030C072 CN=Microsoft Root Certificate...
BE36A4562FB2EE05DBB3D32323ADF445084ED656 CN=Thawte Timestamping CA, OU...
A43489159A520F0D93D032CCAF37E7FE20A8B419 CN=Microsoft Root Authority, ...
9FE47B4D05D46E8066BAB1D1BFC9E48F1DBE6B26 CN=PowerShell Local Certifica...
7F88CD7223F3C813818C994614A89C99FA3B5247 CN=Microsoft Authenticode(tm)...
245C97DF7514E7CF2DF8BE72AE957B9E04741E85 OU=Copyright (c) 1997 Microso...
(...)
Much, Much More
As exciting as this guided tour was, it barely scratches the surface of how you can use
PowerShell to improve your productivity and systems management skills. For more
information about getting started in PowerShell, see Chapter 1.
16 | A Guided Tour of Windows PowerShell
PART II
Fundamentals
Chapter 1, The Windows PowerShell Interactive Shell
Chapter 2, Pipelines
Chapter 3, Variables and Objects
Chapter 4, Looping and Flow Control
Chapter 5, Strings and Unstructured Text
Chapter 6, Calculations and Math
Chapter 7, Lists, Arrays, and Hashtables
Chapter 8, Utility Tasks
Other documents randomly have
different content
Sure of all friends the blackest we can find
Are those ingrates who stab our peace of mind.”
A not uncommon and much more agreeable verse sets forth the
duties of man towards woman in so far as matrimony is concerned:
—
“Adam alone in Paradise did grieve
And thought Eden a desert without Eve,
Until God pitying his lonesome state
Crown’d all his wishes with a lovely mate.
Then why should men think mean, or slight her,
That could not live in Paradise without her.”
Samplers bearing the foregoing verse are usually decorated with a
picture of our first parents and the Tree of Knowledge, supported by
a demon and angel.
The parent or teacher sometimes spoke through the sampler, as
thus, in Lucia York’s, dated 1725:—
“Oh child most dear
Incline thy ear
And hearken to God’s voice.”
Or again:—
“Return the kindness that you do receive
As far as your ability gives leave.”
Mary Lounds.
“Humility I’d recommend
Good nature, too, with ease,
Be generous, good, and kind to all,
You’ll never fail to please.”
Susanna Hayes.
Samplers Expatiating upon Virtue or Vice,
Wealth or Poverty, Happiness or Misery
Amongst these may be noted:—
“Happy is he, the only man,
Who out of choice does all he can
Who business loves and others better makes
By prudent industry and pains he takes.
God’s blessing here he’ll have and man’s esteem,
And when he dies his works will follow him.”
Of those dealing with wealth or poverty none, perhaps, is more
incisive than this:—
“The world’s a city full of crooked streets,
And Death’s the market-place where all men meet;
If life was merchandise that men could buy
The rich would always live, the poor alone would die.”
An American sampler has the following from Burns’s “Grace before
Meat”:—
“Some men have meat who cannot eat
And some have none who need it.
But we have meat and we can eat,
And so the Lord be thanked.”
Larger Image
Plate VIII.—Sampler by Mary Postle. Dated 1747.
Mrs C. J. Longman.
An early specimen of a bordered
Sampler, dated 1747, the rows being
relegated to a small space in the centre,
where they are altogether an
insignificant feature in comparison with
the border. Some of the ornament to
which we have been accustomed in the
rows survives, as for instance the pinks,
but a new one is introduced, namely, the
strawberry. Here are also the Noah’s Ark
animals, trees, etc., which henceforward
become common objects and soon
transform the face of the Sampler. The
border itself is in evident imitation of the
worsted flower work with which curtains,
quilts, and other articles were freely
adorned in the early eighteenth century.
Inscriptions having an Interest owing to their
Quaintness
The following dates from 1740, and has as appendix the line, “God
prosper the war”:—
“The sick man fasts because he cannot eat
The poor man fasts because he hath no meat
The miser fasts to increase his store
The glutton fasts because he can eat no more
The hypocrite fasts because he’d be condemned
The just man fasts cause he hath offended.”
An American version of this ends with:—
“Praise God from whom all blessings flow
We have meat enow.”
That self-conceit was not always considered a failing, is evident from
the following verses:—
“This needlework of mine may tell
That when a child I learned well
And by my elders I was taught
Not to spend my time for nought,”
which is concentrated and intensified in one of Frances Johnson,
worked in 1797:—
“In reading this if any faults you see
Mend them yourself and find no fault in me.”
In a much humbler strain is this from an old sampler in Mrs
Longman’s collection:—
“When I was young I little thought
That wit must be so dearly bought
But now experience tells me how
If I must thrive, then I must bowe
And bend unto another will,
That I might learn both arte & skill.”
Owing to the portrayal of an insect, which was not infrequently met
with in days gone by, upon the face of the sampler which bears the
following lines, it has been suggested that they were presumably
written by that creature:—
“Dear Debby
I love you sincerely
My heart retains a grateful sense of your past kindness
When will the hours of our
Separation be at an end?
Preserve in your bosom the remembrance
of your affectionate
Deborah Jane Berkin.”
The following, coming about the date when the abolition of the slave
trade was imminent, may have reference to it:—
“THERE’S mercy in each ray of light, that mortal eye e’er
saw,
There’s mercy in each breath of air, that mortal lips can
draw,
There’s mercy both for bird, and beast, in God’s indulgent
plan,
There’s mercy for each creeping thing—But man has none
for man.”
Elizabeth Jane
Gates Aged 12 years, 1829.
Riddle samplers, such as that of Ann Witty, do not often occur:—
“I had
both
Money
and a
Friend
by both I set great store
I lent my to my
and took his word
therefor
I asked my of my
and nought but words I
got
I lost my
and
my
for sue him, I would not.”
Here, too, is an “Acrostick,” the first letters of whose lines spell the
name of the young lady who “ended” it “Anno Dom. 1749.”
“A virgin that’s Industrious Merits Praise,
Nature she Imitates in Various Ways,
Now forms the Pink, now gives the Rose its blaze.
Young Buds, she folds, in tender Leaves of green,
Omits no shade to beautify her Scene,
Upon the Canvas, see, the Letters rise,
Neatly they shine with intermingled dies,
Glide into Words, and strike us with Surprize.”
E. W.
As illustrations of tales the sampler of Sarah Young (Fig. 15) is an
unusual example. It deals with Sir Richard Steele’s story of the loves
of Inkle and Yarico. Inkle, represented as a strapping big sailor, was
cast away in the Spanish Main, where he met and loved Yarico, an
Indian girl, but showed his baseness by selling her for a slave when
he reached Barbadoes in a vessel which rescued him. The story
evidently had a considerable, if fleeting, popularity, for it was
dramatised.
The Design, Ornament and Colouring of
Samplers
Whilst important clues to the age of a sampler may be gathered
from its form and legend, its design and colouring are factors from
which almost as much may be learnt.
Design can be more easily learned from considering in detail the
illustrations, which have been mainly chosen for their typifying one
or other form of it, but certain general features are so usually
present that they may be summarised here.
No one with any knowledge of design can look through the
specimens of samplers selected for this volume without noting, first,
that it is, in the earlier specimens, appropriate to the subject,
decorative in treatment, and lends itself to a variety of treatment
with the needle. Secondly, that the decoration is not English in
origin, but is usually derived from foreign sources. Indeed, if we are
to believe an old writer of the Jacobean time, the designs were
“Collected with much praise and industrie,
From scorching Spaine and freezing Muscovie,
From fertile France and pleasant Italie,
From Poland, Sweden, Denmarke, Germanie,
And some of these rare patternes have been set
Beyond the boundes of faithlesse Mahomet,
From spacious China and those Kingdomes East
And from great Mexico, the Indies West.
Thus are these workes farre fetch’t and dearly bought,
And consequently good for ladyes thought.”
Thirdly, that after maintaining a remarkable uniformity until the end
of the seventeenth century, design falls away, and with rare
exceptions continuously declines until it reaches a mediocrity to
which the term can hardly be applied.
Larger Image
Fig. 15.—Sampler by Sarah Young. About 1750.
Mrs Head.
The same features are noticeable in the colouring. The samplers of
the Caroline period are in the main marked by a softness and
delicacy, with a preference for tender and harmonious shades of
pinks, greens, and blues, but these quickly pass out of the schemes
of colouring until their revival a few years ago through the influence
of Japan and the perspicuity, of Sir Lazenby Liberty. This delicacy is
not, as some suppose, due to time having softened the colours, for
examination shows that fading has seldom taken place, in fact one
of the most remarkable traits of the earlier samplers is the wonderful
condition of their colouring (see Mrs Longman’s sampler of 1656,
Plate IV., as an example). Towards the end of the seventeenth
century the adoption of a groundwork of roughish close-textured
canvas of a canary hue also militated against this ensemble of the
colour scheme, which is now and again too vivid, especially in the
reds, a fact which may, in part, be due to their retaining their
original tint with a persistency that has not endured with the other
dyes.
During the early Georgian era sampler workers seem to have passed
through a stage of affection for deep reds, blues, and greens, with
which they worked almost all their lettering. The same colours are
met with in the large embroidered curtains of the time; it is probably
due to the influence of the tapestries and the Chinese embroideries
then so much in vogue.
In the opening years of the eighteenth century a pride in lettering
gave rise to a series of samplers of little interest or artistic value,
consisting, as they did, of nothing else than long sentences, not
readily readable, and worked in silks in colours of every imaginable
hue used indiscriminately, even in a single word, without any
thought bestowed on harmony or effect of colouring.
Later on, towards the middle of the century, more sober schemes of
colour set in, consisting in the abandonment of reds and the
employment of little else than blues, greens, yellows, and blacks
(see Plate IX.), which are attractive through their quietness and
unity. Subsequently but little praise can be bestowed upon samplers
so far as their design is concerned. Occasionally, as in that of Mr
Ruskin’s ancestress (Plate X.), a result which is satisfactory, both in
colour and design, is arrived at, but this is generally due to individual
taste rather than to tuition or example. In this respect samplers only
follow in the wake of all the other arts—furniture and silversmiths’
work, perhaps, excepted, as regards both of which the taste
displayed was also individual rather than national.
An evil which cankered later sampler ornamentation was a desire for
novelty and variety. The earliest samplers exhibit few signs of
attempts at invention in design. A comparison of any number of
them shows ideas repeated again and again with the slightest
variation. The same floral motives are adapted in almost every
instance, and one and all may well have been employed since the
days when they arrived from the Far East, brought, it may be, by the
Crusaders. But it is in no derogatory spirit that I call attention to this
lack of originality. A craftsman is doing a worthier thing in
assimilating designs which have shown their fitness by centuries of
use, patterns which are examples of fine decorative ornament that
really beautifies the object to which it is applied, than in inventing
weak and imperfect originals. No architect is accused of plagiarism if
he introduces the pointed arch, and the great designs of the past are
free and out of copyright. The Greek fret, or the Persian rose, is as
much the property of anyone as the daisy or the snowdrop, and it
was far better to make sound decorative pieces of embroidery on the
lines of these than to attempt, as was done later on, feeble originals,
which have nothing ornamental or decorative in their composition.
The workers of the East, when perfection was arrived at in a design,
did not hesitate to reproduce it again and again for centuries.
Larger Image
Plate IX.—Sampler by E. Philips. Dated 1761.
Author’s Collection.
Were it not that this Sampler was
produced by little Miss Philips at the
tender age of seven, there would be a
probability that it was unique through its
containing a portrait of the producer. For
in no other example have we so many
evidences pointing to its being a record
of actual facts. For instance, there is
clearly shown a gentleman pointing to
his wife (in a hooped costume), and
having round him his five girls of various
ages, the youngest in the care of a
nurse. In the upper left corner is his son
in charge of a tutor, whilst on the right
are two maid-servants, one being a
woman of colour. This fashion for black
servants is further emphasised by the
negro boy with the dog. That these
should be present in this family is not
remarkable, for by the lower illustration it
is evident that Mr Philips was a traveller
who had crossed the seas in his ship to
where alligators, black swans and other
rare birds abounded. The work was
executed in 1761, the second year of
George the Third, whose monogram and
crown are supported by two soldiers in
the costume of the period. It has been
most dexterously carried out by the
young lady, and it is conceived in a
delicate harmony of greens and blues
which was not uncommon at that time.
Size, 19 × 12½. An adaptation of this
Sampler has been utilised as the drop
scene to the play of “Peter Pan.”
But the mistress of a ladies’ improving school would hardly like her
pupils to copy time after time the same designs—designs which
perhaps resembled those of a rival establishment. Such a one would
be oblivious to the fact that an ornamentalist is born not made, that
the best design is traditional, and that pupils would be far more
worthily employed in perpetuating ornamentation which had been
invented by races intuitively gifted for such a purpose, than in
attempting feeble products of her own brain. So, too, results show
that she was, as a rule, unaware that good design is better displayed
in simplicity than in pretentiousness. As that authority on design, the
late Lewis Day, wrote in his volume on Embroidery, “The
combination of a good designer and worker in the same person is an
ideal very occasionally to be met with, and any attempt to realise it
generally fails.”
Samplers show in increasing numbers as the end approaches that
their designers were ignorant of most of the elementary rules of
ornamentation in needlework, such, for instance, as that the pictorial
is not a suitable subject for reproduction, nor the delineation of the
human figure, nor that the floral and vegetable kingdom, whilst
lending itself better than aught else, should be treated from the
decorative, and not the realistic point of view.
We will now pass on to consider generally the forms of decoration
most usually met with.
Sampler Design: the Human Figure
Whilst embroideries in imitation of tapestries deal almost entirely
with the portrayal of the human figure, samplers of the same period,
and that the best, for the most part avoid it. This is somewhat
remarkable, for the design of the Renaissance, which was universally
practised at the time upon which we are dwelling, was almost
entirely given up to weaving it into other forms, and the volumes
which treat of embroidery show how frequently it occurs in foreign
pieces of needlework. The omission is a curious one, but the reason
for it is, apparently, not far to seek. If we examine the earlier pieces
we shall see that practically one type of figure only presents itself.
Save in exceptional pieces, such as Mrs Longman’s early piece (Plate
IV.), where the figures are clearly copied from one of the small
tapestry pieces so in vogue at that date (1656), or Mrs Millett’s piece
(Fig. 16), the figures which appear upon samplers are all cast in one
mould, and in no way improve but rather mar the composition.
This last-named drawn-work sampler is a specimen altogether apart
for beauty of design and workmanship. Doubts have been expressed
as to its English origin, but portions of the ornament, such as the
acorn, and the Stuart S in the lowest row, are thoroughly English;
besides, as we have seen, design in almost every one of the
seventeenth-century samplers is infected with foreign motives. The
uppermost panel is supposed to represent Abraham, Sarah, and the
Angel. To the left is the tent, with the folds worked in relief, in a
stitch so fine as to defy ordinary eyesight. Sarah, who holds up a
hand in astonishment at the angel’s announcement, has her head-
dress, collar, and skirt in relief, the latter being sewn with
microscopic fleurs-de-lis. The winged angel to the left of Abraham
has a skirt composed of tiny scallops, which may represent feathers.
A rabbit browses in front of the tent. The centre of the second row is
occupied by a veiled mermaid, her tail covered with scalloped scale
in relief. She holds in either hand a cup and a mask. The lettering in
the two flanking panels is “S.I.D. 1649 A.I.” The decorative motive of
the outer panels is peapods in relief, some open and disclosing peas.
Roses and tulips fill the larger square below, and these are followed
by a row (reversed) of tulips and acorns. Four other rows complete
the sampler, which only measures 18½ × 6¾. In order to give it a
larger size the lowest row is not reproduced. I have seen another
drawn-work sampler which antedates that just described by a year.
It is of somewhat coarse texture but is good in design, and bears in
a panel at the side initials and the date. The Victoria and Albert
Museum has also two somewhat similar drawn-work samplers—one
by Elizabeth Wood, dated 1666, which contains the Stuart S’s; the
other (undated) has the arms of James I.
Larger Image
Fig. 16.—Drawn-Work Sampler by S. I. D.
Dated 1649.
Mrs C. F. Millett.
Larger Image
Fig. 17.—Sampler by Jean Porter. 1709-10.
A type of figure prevalent in early samplers has puzzled collectors
who possess specimens containing it. It wears a close-fitting
costume and has arms extended, and has received the name of a
“Boxer,” presumably from its attitude and costume. It and a
companion are continuously depicted for nearly a century, finally
disappearing about 1742, but maintaining their attitude with less
variation than any other form of ornament, the only alteration being
in the form of the trophy which they hold in one hand. It is this
trophy, if we may use such a term, that negatives the idea of their
being combatant figures, and it almost with certainty places them in
the category of the Greek Erotes, the Roman Amores, or the Cupids
of the Renaissance. It is difficult to give a name to the trophy in
most of the samplers, and the worker was clearly often in doubt as
to its structure. In some it resembles a small vase with a lid, in
others a spray with branches or leaves on either side. In one of 1673
it takes the form of a four-petalled flower, and in one of 1679 that of
an acorn, which is repeated in samplers of 1684, 1693, and 1694,
this repetition being probably due to the acorn being a very favourite
subject for design under the Stuarts. In a sampler of 1693 acorns
are held in either hand. In one of 1742 (Fig. 18), the object held is a
kind of candelabra. The little figures themselves preserve a singular
uniformity of costume, which again points to their being the nude
Erotes, clothed, to suit the times, in a tight-fitting jerkin and
drawers. These are always of gayest colours. On occasions (as in a
sampler dated 1693) they don a coat, and have long wigs, bringing
them into line with the prevailing fashion.
When these figures disappear their place is taken by those of our
first parents in the Garden of Eden, the incongruity of which is well
depicted in the sampler illustrated in Fig. 17. This piece of work,
which took nearly a year to complete—it was begun on 14th May
1709, and finished on 6th April 1710—is unlike any other that I have
seen of that period, for it antedates, by nearly half a century, the
scenes from real life which afterwards became part and parcel of
every sampler. Adam and Eve became quite common objects on
samplers after 1760.[5]
Mention need only be made here of the dressed figures which occur
in samplers dated during the reign of George the Third. They are
sometimes quaint (as in Plates IX. and XI.), but they hardly come
into any scheme of decoration. The squareness of the stitch used in
later samplers renders any imitation of painting such as was
attempted altogether a failure.
Larger Image
Fig. 18.—Sampler. Name Illegible. Date 1742.
Formerly in the Author’s Collection.
Sampler Design: Animals
Animals in any true decorative sense hardly came into sampler
ornament. Whilst the tapestry pictures teem with them, so that one
wanting in a lion or stag is a rarity, in samplers, probably, the
difficulty of obtaining rounded forms with the stitch used in the large
grained canvas was a deterrent. The lion only being found on the
Fletwood sampler of 1654 (Fig. 44) and the stag, which in tapestry
pictures usurps the place of the unicorn, appears but rarely on
samplers before the middle of the eighteenth century, when it came
into fashion, and afterwards occurs with uninterrupted regularity so
long as samplers were made.
This neglect of animals is hardly to be deplored, for when they do
occur they are little else than caricatures (see, for instance, those in
Plate III.). Birds, which lend themselves to needlework, appear in
the later samplers (Plate XI. and Fig. 18), but hardly as part of any
decorative scheme.
Sampler Design: Flowers
With the practically insignificant exceptions which we have just
noticed, the ornamentation of the sampler was confined to floral and
geometrical motives, and whilst the latter were for the most part
used in drawn-work samplers, the former constituted the stock
whence the greater part of the decoration employed in the older
examples was derived.
Amongst the floral and vegetable kingdom the selection was a wide
one, but a few favourites came in for recognition in almost every
sampler, partly because of their decorative qualities, and partly from
their being national badges. With few exceptions they were those
which were to be met with in English seventeenth-century gardens,
and undoubtedly, in some instances, may have been adapted by the
makers from living specimens. Chief among the flowers was the
rose, white and red, single and double, the emblem for centuries
previously of two great parties in the State, a badge of the Tudor
kings, a part of the insignia of the realm, and occupying a foremost
place upon its coinage. In sampler ornamentation it is seldom used
either in profile or in bud, but generally full face, and more often as
a single than as a double flower. As a form of decoration it may have
been derived from foreign sources, but it clearly owed its popularity
to the national significance that attached to it.
The decorative value of the pink or carnation has been recognised
from the earliest times, and a piece of Persian ornament is hardly
complete without it. It is not surprising, therefore, that the old
sampler workers utilised it to the full, and in fact it appears oftener
than the rose in seventeenth-century specimens. Ten of the thirteen
exhibits of that century at The Fine Art Society’s Exhibition in 1900
contained it as against seven where the rose was figured. It
maintains this position throughout, and the most successful of the
borders of bordered samplers are those where it is utilised.
Specimens will be found in Plates III., IV., and VI.
The decorative value of the honeysuckle was hardly appreciated, and
it only appeared on samplers of the date of 1648 (Plate III.), 1662
(Plate V.), 1668, 1701, and 1711, in the Exhibition, and the undated
one reproduced in Fig. 4.
Larger Image
Plate X.—Sampler by Catherine Tweedall. Dated 1775.
Mrs Arthur Severn.
The Sampler is noteworthy not only on
account of its harmonious colour
scheme, its symmetry of parts, and the
excellence of its needlework, but as
having been wrought by a young lady
who afterwards became Mrs Ruskin, and
the grandmother of John Ruskin. Her
name, Cathrine Tweedall, is worked in
the lower circle, and is illegible in the
otherwise admirable reproduction, owing
to its being in a faded shade of the
fairest pink. The verse was probably
often read by her renowned grandson,
and may perchance have spurred his
determination to strive in the race in
which he won so “high a reward.” Mrs
Arthur Severn, to whom the Sampler
belongs, notes that the Jean Ross whose
name also appears upon it was the sister
of the great Arctic explorer. The date of
the Sampler is 1775.
Sampler workers were very faithful to the strawberry, which, after
appearing in almost every one of the seventeenth-century long
samplers, was a favourite object for the later borders, and it may be
seen almost unaltered in specimens separated in date by a century
at least. We give in Fig. 31 a very usual version of it. (See also Plate
XIII.)
Larger Image
Fig. 19.—Sampler by Mary Anderson. 1831.
Lady Sherborne.
Other fruits and flowers which now and again find a place are the
fig, which will be seen in Plate III.; the pineapple, the thistle (Fig.
21), and the tulip in samplers dated 1662, 1694, 1760, and 1825
(Plate XIII.).
Although the oak tree acquired political significance after the flight
of Charles II., that fact can in no way account for such prominence
being attached to its fruit and its foliage as, for instance, is the case
in samplers dated 1644 and 1648 (Plate III.), where varieties of
these are utilised in a most decorative fashion in several of the rows
of ornament, or in another of the following years (Fig. 16). But,
curiously enough, after appearing in almost every seventeenth-
century sampler, it disappeared entirely at the commencement of the
eighteenth century.
Sampler Design: Crowns, Coronets, Etc.
The crown seems to have been suddenly seized upon by sampler
makers as a form of decoration, and for half a century it was used
with a tiresome reiteration. It had, of course, been largely used in
Tudor decoration, and on the restoration of the monarchy it would
be given prominence. But it probably was also in vogue because it
lent itself to filling up spaces caused by alphabets not completing a
line, and also because it allowed of variation through the coronets
used by different ranks of nobility. We have seen in the sampler, Fig.
20, that the coronet of each order was used with a letter beneath,
indicating duke, earl, etc. On occasions crowns were also used with
some effect as a border. It is possible that the fashion for coronets
was derived from foreign samplers, where this form of decoration
was frequently used about the end of the seventeenth century,
doubtless owing to the abundance of ennobled personages; they
may well have come over with many other fancies which followed in
the train of the House of Hanover. The earliest sampler in the
Exhibition before referred to which bore a crown was one of 1693;
but the coronet was there placed in conjunction with the initials M.
D., and might be that of a titled lady who worked it. After that it
appeared in one dated 1705 (where it was clearly a royal one
connected with “Her Majesti Queen Anne”), and in samplers dated
1718, 1726, 1728 (1740, in which there were at least fifty varieties),
and so on almost yearly up to 1767, after which it gradually
disappeared, two only out of seventy subsequent samplers
containing it. These were dated 1798 and 1804. In countries where
almost every family bore a rank which warranted the use of a
coronet, there would be a reason for their appearance as part of
what would have to be embroidered on table linen, etc.
Larger Image
Fig. 20.—Sampler. Scottish (?). 18th Century.
Formerly in the Author’s Collection.
Note.—The bright colouring, coarse
canvas, and ornate lettering of this piece
suggest a Scottish origin. It dates from
about 1730, and is one of the earliest of
the bordered samplers, the border being
at present an altogether insignificant
addition. It is also one of the first
specimens of decoration with crowns and
coronets, the initials underneath
standing for king, duke, marquis, earl,
viscount, lord, count, and baron.
Fig. 21.—Sampler by J. H. [Jane Heath]. a.d. 1725.
Mr Ashby Sterry.
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.
More than just a book-buying platform, we strive to be a bridge
connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.
Join us on a journey of knowledge exploration, passion nurturing, and
personal growth every day!
ebookbell.com

Windows Powershell Cookbook Second Edition Second Edition Lee Holmes

  • 1.
    Windows Powershell CookbookSecond Edition Second Edition Lee Holmes download https://ebookbell.com/product/windows-powershell-cookbook-second- edition-second-edition-lee-holmes-2539686 Explore and download more ebooks at ebookbell.com
  • 2.
    Here are somerecommended products that we believe you will be interested in. You can click the link to download. Windows Powershell Cookbook The Complete Guide To Scripting Microsofts Command Shell 3rd Edition Lee Holmes https://ebookbell.com/product/windows-powershell-cookbook-the- complete-guide-to-scripting-microsofts-command-shell-3rd-edition-lee- holmes-49431868 Windows Powershell Cookbook Lee Holmes https://ebookbell.com/product/windows-powershell-cookbook-lee- holmes-2467022 Windows Server Automation With Powershell Cookbook Powerful Ways To Automate Manage And Administrate Windows Server 2022 Using Powershell 72 5th Edition 5th Edition Thomas Lee https://ebookbell.com/product/windows-server-automation-with- powershell-cookbook-powerful-ways-to-automate-manage-and-administrate- windows-server-2022-using-powershell-72-5th-edition-5th-edition- thomas-lee-49114086 Windows Server Automation With Powershell Cookbook Powerful Ways To Automate And Manage Windows Administrative Tasks 4th Ed Thomas Lee https://ebookbell.com/product/windows-server-automation-with- powershell-cookbook-powerful-ways-to-automate-and-manage-windows- administrative-tasks-4th-ed-thomas-lee-55391036
  • 3.
    Windows Server 2016Automation With Powershell Cookbook Automate Manual Administrative Tasks With Ease 2nd Edition Lee https://ebookbell.com/product/windows-server-2016-automation-with- powershell-cookbook-automate-manual-administrative-tasks-with- ease-2nd-edition-lee-20640372 Windows Server 2012 Automation With Powershell Cookbook Ed Goad https://ebookbell.com/product/windows-server-2012-automation-with- powershell-cookbook-ed-goad-4148828 Windows Server Automation With Powershell Cookbook Powerful Ways To Automate Manage And Administrate Windows Server 5th Ed 5th Thomas Lee https://ebookbell.com/product/windows-server-automation-with- powershell-cookbook-powerful-ways-to-automate-manage-and-administrate- windows-server-5th-ed-5th-thomas-lee-47604122 Windows Server 2019 Automation With Powershell Cookbook Third Edition Thomas Lee https://ebookbell.com/product/windows-server-2019-automation-with- powershell-cookbook-third-edition-thomas-lee-49851834 Microsoft Sharepoint 2010 And Windows Powershell 20 Expert Cookbook Yaroslav Pentsarskyy https://ebookbell.com/product/microsoft-sharepoint-2010-and-windows- powershell-20-expert-cookbook-yaroslav-pentsarskyy-2421432
  • 7.
  • 9.
    SECOND EDITION Windows PowerShellCookbook Lee Holmes Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo
  • 10.
    Windows PowerShell Cookbook,Second Edition by Lee Holmes Copyright © 2010 Lee Holmes. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Mike Hendrickson Production Editor: Teresa Elsey Copyeditor: Genevieve d’Entremont Proofreader: Teresa Elsey Indexer: Newgen North America, Inc. Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: October 2007: First Edition. August 2010: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Windows PowerShell Cookbook, the image of a box turtle, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-0-596-80150-2 [M] 1281554603
  • 11.
    Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Foreword to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Part I. Tour A Guided Tour of Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Part II. Fundamentals 1. The Windows PowerShell Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.1 Run Programs, Scripts, and Existing Tools 19 1.2 Resolve Errors Calling Native Executables 21 1.3 Run a PowerShell Command 23 1.4 Invoke a Long-Running or Background Command 24 1.5 Notify Yourself of Job Completion 27 1.6 Customize Your Shell, Profile, and Prompt 28 1.7 Find a Command to Accomplish a Task 31 1.8 Get Help on a Command 32 1.9 Program: Search Help for Text 34 1.10 Program: View PowerShell’s HTML Help 36 1.11 Launch PowerShell at a Specific Location 37 1.12 Invoke a PowerShell Command or Script from Outside PowerShell 38 1.13 Customize the Shell to Improve Your Productivity 40 1.14 Program: Learn Aliases for Common Commands 42 1.15 Program: Learn Aliases for Common Parameters 44 1.16 Access and Manage Your Console History 46 1.17 Program: Create Scripts from Session History 48 v
  • 12.
    1.18 Invoke aCommand from Your Session History 49 1.19 Program: Search Formatted Output for a Pattern 51 1.20 Interactively View and Process Command Output 52 1.21 Store the Output of a Command into a File 54 1.22 Add Information to the End of a File 55 1.23 Record a Transcript of Your Shell Session 55 1.24 Extend Your Shell with Additional Commands 56 1.25 Use Commands from Customized Shells 57 1.26 Save State Between Sessions 59 2. Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.1 Filter Items in a List or Command Output 64 2.2 Group and Pivot Data by Name 65 2.3 Program: Simplify Most Where-Object Filters 68 2.4 Program: Interactively Filter Lists of Objects 70 2.5 Work with Each Item in a List or Command Output 72 2.6 Automate Data-Intensive Tasks 74 2.7 Program: Simplify Most Foreach-Object Pipelines 78 2.8 Intercept Stages of the Pipeline 80 2.9 Automatically Capture Pipeline Output 81 2.10 Capture and Redirect Binary Process Output 83 3. Variables and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 3.1 Display the Properties of an Item as a List 90 3.2 Display the Properties of an Item as a Table 92 3.3 Store Information in Variables 94 3.4 Access Environment Variables 95 3.5 Program: Retain Changes to Environment Variables Set by a Batch File 98 3.6 Control Access and Scope of Variables and Other Items 100 3.7 Program: Create a Dynamic Variable 102 3.8 Work with .NET Objects 104 3.9 Create an Instance of a .NET Object 108 3.10 Program: Create Instances of Generic Objects 110 3.11 Reduce Typing for Long Class Names 113 3.12 Use a COM Object 115 3.13 Learn About Types and Objects 115 3.14 Get Detailed Documentation About Types and Objects 117 3.15 Add Custom Methods and Properties to Objects 119 3.16 Create and Initialize Custom Objects 121 3.17 Add Custom Methods and Properties to Types 125 vi | Table of Contents
  • 13.
    4. Looping andFlow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 4.1 Make Decisions with Comparison and Logical Operators 131 4.2 Adjust Script Flow Using Conditional Statements 133 4.3 Manage Large Conditional Statements with Switches 135 4.4 Repeat Operations with Loops 136 4.5 Add a Pause or Delay 139 5. Strings and Unstructured Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 5.1 Create a String 141 5.2 Create a Multiline or Formatted String 143 5.3 Place Special Characters in a String 144 5.4 Insert Dynamic Information in a String 144 5.5 Prevent a String from Including Dynamic Information 146 5.6 Place Formatted Information in a String 147 5.7 Search a String for Text or a Pattern 148 5.8 Replace Text in a String 151 5.9 Split a String on Text or a Pattern 152 5.10 Combine Strings into a Larger String 154 5.11 Convert a String to Upper/Lowercase 156 5.12 Trim a String 157 5.13 Format a Date for Output 158 5.14 Program: Convert Text Streams to Objects 160 5.15 Generate Large Reports and Text Streams 164 5.16 Generate Source Code and Other Repetitive Text 166 6. Calculations and Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 6.1 Perform Simple Arithmetic 171 6.2 Perform Complex Arithmetic 173 6.3 Measure Statistical Properties of a List 175 6.4 Work with Numbers as Binary 177 6.5 Simplify Math with Administrative Constants 180 6.6 Convert Numbers Between Bases 181 7. Lists, Arrays, and Hashtables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 7.1 Create an Array or List of Items 183 7.2 Create a Jagged or Multidimensional Array 185 7.3 Access Elements of an Array 186 7.4 Visit Each Element of an Array 188 7.5 Sort an Array or List of Items 189 7.6 Determine Whether an Array Contains an Item 190 7.7 Combine Two Arrays 191 7.8 Find Items in an Array That Match a Value 192 7.9 Compare Two Lists 193 Table of Contents | vii
  • 14.
    7.10 Remove Elementsfrom an Array 193 7.11 Find Items in an Array Greater or Less Than a Value 194 7.12 Use the ArrayList Class for Advanced Array Tasks 195 7.13 Create a Hashtable or Associative Array 197 7.14 Sort a Hashtable by Key or Value 198 8. Utility Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 8.1 Get the System Date and Time 201 8.2 Measure the Duration of a Command 202 8.3 Read and Write from the Windows Clipboard 203 8.4 Generate a Random Number or Object 206 8.5 Program: Search the Windows Start Menu 208 8.6 Program: Show Colorized Script Content 209 Part III. Common Tasks 9. Simple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 9.1 Get the Content of a File 217 9.2 Search a File for Text or a Pattern 219 9.3 Parse and Manage Text-Based Logfiles 222 9.4 Parse and Manage Binary Files 224 9.5 Create a Temporary File 227 9.6 Search and Replace Text in a File 228 9.7 Program: Get the Encoding of a File 231 9.8 Program: View the Hexadecimal Representation of Content 233 10. Structured Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 10.1 Access Information in an XML File 237 10.2 Perform an XPath Query Against XML 240 10.3 Convert Objects to XML 242 10.4 Modify Data in an XML File 243 10.5 Easily Import and Export Your Structured Data 245 10.6 Store the Output of a Command in a CSV or Delimited File 247 10.7 Import CSV and Delimited Data from a File 248 10.8 Use Excel to Manage Command Output 249 10.9 Parse and Interpret PowerShell Scripts 251 11. Code Reuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 11.1 Write a Script 255 11.2 Write a Function 258 11.3 Find a Verb Appropriate for a Command Name 260 11.4 Write a Script Block 261 viii | Table of Contents
  • 15.
    11.5 Return Datafrom a Script, Function, or Script Block 263 11.6 Package Common Commands in a Module 265 11.7 Write Commands That Maintain State 268 11.8 Selectively Export Commands from a Module 270 11.9 Diagnose and Interact with Internal Module State 272 11.10 Handle Cleanup Tasks When a Module Is Removed 274 11.11 Access Arguments of a Script, Function, or Script Block 276 11.12 Add Validation to Parameters 280 11.13 Accept Script Block Parameters with Local Variables 284 11.14 Dynamically Compose Command Parameters 286 11.15 Provide -WhatIf, -Confirm, and Other Cmdlet Features 287 11.16 Add Help to Scripts or Functions 290 11.17 Add Custom Tags to a Function or Script Block 292 11.18 Access Pipeline Input 295 11.19 Write Pipeline-Oriented Scripts with Cmdlet Keywords 296 11.20 Write a Pipeline-Oriented Function 300 11.21 Organize Scripts for Improved Readability 301 11.22 Invoke Dynamically Named Commands 303 11.23 Program: Enhance or Extend an Existing Cmdlet 304 12. Internet-Enabled Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 12.1 Download a File from the Internet 313 12.2 Download a Web Page from the Internet 314 12.3 Program: Get-PageUrls 318 12.4 Connect to a Web Service 321 12.5 Program: Connect-WebService 323 12.6 Export Command Output as a Web Page 326 12.7 Send an Email 327 12.8 Program: Send-MailMessage 328 12.9 Program: Interact with Internet Protocols 329 13. User Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 13.1 Read a Line of User Input 335 13.2 Read a Key of User Input 336 13.3 Program: Display a Menu to the User 337 13.4 Display Messages and Output to the User 339 13.5 Provide Progress Updates on Long-Running Tasks 342 13.6 Write Culture-Aware Scripts 344 13.7 Support Other Languages in Script Output 347 13.8 Program: Invoke a Script Block with Alternate Culture Settings 349 13.9 Access Features of the Host’s User Interface 350 13.10 Program: Add a Graphical User Interface to Your Script 352 13.11 Interact with UI Frameworks and STA Objects 355 Table of Contents | ix
  • 16.
    14. Debugging .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 14.1 Prevent Common Scripting Errors 360 14.2 Trace Script Execution 362 14.3 Set a Script Breakpoint 365 14.4 Debug a Script When It Encounters an Error 368 14.5 Create a Conditional Breakpoint 370 14.6 Investigate System State While Debugging 372 14.7 Program: Watch an Expression for Changes 375 14.8 Program: Get Script Code Coverage 377 15. Tracing and Error Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 15.1 Determine the Status of the Last Command 381 15.2 View the Errors Generated by a Command 383 15.3 Manage the Error Output of Commands 385 15.4 Program: Resolve an Error 386 15.5 Configure Debug, Verbose, and Progress Output 387 15.6 Handle Warnings, Errors, and Terminating Errors 389 15.7 Output Warnings, Errors, and Terminating Errors 392 15.8 Program: Analyze a Script’s Performance Profile 393 16. Environmental Awareness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 16.1 View and Modify Environment Variables 399 16.2 Access Information About Your Command’s Invocation 401 16.3 Program: Investigate the InvocationInfo Variable 403 16.4 Find Your Script’s Name 405 16.5 Find Your Script’s Location 406 16.6 Find the Location of Common System Paths 407 16.7 Get the Current Location 409 16.8 Safely Build File Paths Out of Their Components 411 16.9 Interact with PowerShell’s Global Environment 411 16.10 Determine PowerShell Version Information 412 17. Extend the Reach of Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 17.1 Automate Programs Using COM Scripting Interfaces 415 17.2 Program: Query a SQL Data Source 416 17.3 Access Windows Performance Counters 419 17.4 Access Windows API Functions 422 17.5 Program: Invoke Simple Windows API Calls 428 17.6 Define or Extend a .NET Class 431 17.7 Add Inline C# to Your PowerShell Script 434 17.8 Access a .NET SDK Library 436 17.9 Create Your Own PowerShell Cmdlet 438 17.10 Add PowerShell Scripting to Your Own Program 440 x | Table of Contents
  • 17.
    18. Security andScript Signing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 18.1 Enable Scripting Through an Execution Policy 446 18.2 Disable Warnings for UNC Paths 449 18.3 Sign a PowerShell Script, Module, or Formatting File 450 18.4 Program: Create a Self-Signed Certificate 452 18.5 Manage PowerShell Security in an Enterprise 453 18.6 Block Scripts by Publisher, Path, or Hash 455 18.7 Verify the Digital Signature of a PowerShell Script 457 18.8 Securely Handle Sensitive Information 458 18.9 Securely Request Usernames and Passwords 460 18.10 Program: Start a Process as Another User 461 18.11 Program: Run a Temporarily Elevated Command 463 18.12 Securely Store Credentials on Disk 465 18.13 Access User and Machine Certificates 467 18.14 Program: Search the Certificate Store 468 18.15 Add and Remove Certificates 470 18.16 Manage Security Descriptors in SDDL Form 471 19. Integrated Scripting Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 19.1 Debug a Script 475 19.2 Customize Text and User Interface Colors 477 19.3 Connect to a Remote Computer 479 19.4 Extend ISE Functionality Through Its Object Model 479 19.5 Add an Item to the Tools Menu 481 Part IV. Administrator Tasks 20. Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 20.1 Determine the Current Location 486 20.2 Get the Files in a Directory 487 20.3 Find All Files Modified Before a Certain Date 488 20.4 Clear the Content of a File 489 20.5 Manage and Change the Attributes of a File 490 20.6 Find Files That Match a Pattern 491 20.7 Manage Files That Include Special Characters 494 20.8 Program: Get Disk Usage Information 495 20.9 Monitor a File for Changes 497 20.10 Get the Version of a DLL or Executable 497 20.11 Program: Get the MD5 or SHA1 Hash of a File 498 20.12 Create a Directory 501 20.13 Remove a File or Directory 502 20.14 Rename a File or Directory 502 Table of Contents | xi
  • 18.
    20.15 Move aFile or Directory 504 20.16 Program: Move or Remove a Locked File 504 20.17 Get the ACL of a File or Directory 506 20.18 Set the ACL of a File or Directory 508 20.19 Program: Add Extended File Properties to Files 509 20.20 Program: Create a Filesystem Hard Link 511 20.21 Program: Create a ZIP Archive 513 21. The Windows Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 21.1 Navigate the Registry 517 21.2 View a Registry Key 518 21.3 Modify or Remove a Registry Key Value 519 21.4 Create a Registry Key Value 520 21.5 Remove a Registry Key 521 21.6 Safely Combine Related Registry Modifications 522 21.7 Add a Site to an Internet Explorer Security Zone 524 21.8 Modify Internet Explorer Settings 526 21.9 Program: Search the Windows Registry 527 21.10 Get the ACL of a Registry Key 529 21.11 Set the ACL of a Registry Key 530 21.12 Work with the Registry of a Remote Computer 531 21.13 Program: Get Registry Items from Remote Machines 533 21.14 Program: Get Properties of Remote Registry Keys 535 21.15 Program: Set Properties of Remote Registry Keys 537 21.16 Discover Registry Settings for Programs 539 22. Comparing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 22.1 Compare the Output of Two Commands 543 22.2 Determine the Differences Between Two Files 545 22.3 Verify Integrity of File Sets 545 23. Event Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549 23.1 List All Event Logs 549 23.2 Get the Newest Entries from an Event Log 551 23.3 Find Event Log Entries with Specific Text 552 23.4 Retrieve and Filter Event Log Entries 553 23.5 Find Event Log Entries by Their Frequency 556 23.6 Back Up an Event Log 558 23.7 Create or Remove an Event Log 560 23.8 Write to an Event Log 561 23.9 Run a PowerShell Script for Windows Event Log Entries 562 23.10 Clear or Maintain an Event Log 563 23.11 Access Event Logs of a Remote Machine 565 xii | Table of Contents
  • 19.
    24. Processes .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 24.1 List Currently Running Processes 570 24.2 Launch the Application Associated with a Document 571 24.3 Launch a Process 572 24.4 Stop a Process 573 24.5 Debug a Process 575 25. System Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577 25.1 List All Running Services 577 25.2 Manage a Running Service 579 25.3 Configure a Service 580 26. Active Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 26.1 Test Active Directory Scripts on a Local Installation 582 26.2 Create an Organizational Unit 585 26.3 Get the Properties of an Organizational Unit 586 26.4 Modify Properties of an Organizational Unit 587 26.5 Delete an Organizational Unit 587 26.6 Get the Children of an Active Directory Container 588 26.7 Create a User Account 589 26.8 Program: Import Users in Bulk to Active Directory 590 26.9 Search for a User Account 592 26.10 Get and List the Properties of a User Account 593 26.11 Modify Properties of a User Account 594 26.12 Change a User Password 594 26.13 Create a Security or Distribution Group 595 26.14 Search for a Security or Distribution Group 596 26.15 Get the Properties of a Group 597 26.16 Find the Owner of a Group 598 26.17 Modify Properties of a Security or Distribution Group 599 26.18 Add a User to a Security or Distribution Group 600 26.19 Remove a User from a Security or Distribution Group 600 26.20 List a User’s Group Membership 601 26.21 List the Members of a Group 602 26.22 List the Users in an Organizational Unit 602 26.23 Search for a Computer Account 603 26.24 Get and List the Properties of a Computer Account 604 27. Enterprise Computer Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607 27.1 Join a Computer to a Domain or Workgroup 607 27.2 Remove a Computer from a Domain 608 27.3 Program: List Logon or Logoff Scripts for a User 609 27.4 Program: List Startup or Shutdown Scripts for a Machine 610 Table of Contents | xiii
  • 20.
    27.5 Deploy PowerShell-BasedLogon Scripts 612 27.6 Enable or Disable the Windows Firewall 612 27.7 Open or Close Ports in the Windows Firewall 613 27.8 Program: List All Installed Software 614 27.9 Uninstall an Application 616 27.10 Manage Computer Restore Points 617 27.11 Reboot or Shut Down a Computer 619 27.12 Determine Whether a Hotfix Is Installed 620 27.13 Manage Scheduled Tasks on a Computer 621 27.14 Retrieve Printer Information 622 27.15 Retrieve Printer Queue Statistics 623 27.16 Manage Printers and Print Queues 624 27.17 Program: Summarize System Information 626 27.18 Renew a DHCP Lease 627 27.19 Assign a Static IP Address 629 27.20 List All IP Addresses for a Computer 630 27.21 List Network Adapter Properties 631 28. Windows Management Instrumentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633 28.1 Access Windows Management Instrumentation Data 635 28.2 Modify the Properties of a WMI Instance 637 28.3 Invoke a Method on a WMI Class 639 28.4 Program: Determine Properties Available to WMI Filters 640 28.5 Program: Search for WMI Classes 642 28.6 Use .NET to Perform Advanced WMI Tasks 645 28.7 Improve the Performance of Large-Scale WMI Operations 646 28.8 Convert a VBScript WMI Script to PowerShell 647 29. Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651 29.1 Find Commands That Support Their Own Remoting 652 29.2 Program: Invoke a PowerShell Expression on a Remote Machine 653 29.3 Test Connectivity Between Two Computers 656 29.4 Limit Networking Scripts to Hosts That Respond 658 29.5 Enable PowerShell Remoting on a Computer 659 29.6 Enable Remote Desktop on a Computer 661 29.7 Program: Remotely Enable PowerShell Remoting 662 29.8 Configure User Permissions for Remoting 664 29.9 Enable Remoting to Workgroup Computers 665 29.10 Interactively Manage a Remote Computer 667 29.11 Invoke a Command on a Remote Computer 669 29.12 Implicitly Invoke Commands from a Remote Computer 673 29.13 Create Sessions with Full Network Access 676 29.14 Pass Variables to Remote Sessions 680 xiv | Table of Contents
  • 21.
    29.15 Configure AdvancedRemoting Options 681 29.16 Invoke a Command on Many Computers 683 29.17 Run a Local Script on a Remote Computer 684 29.18 Program: Transfer a File to a Remote Computer 685 29.19 Determine Whether a Script Is Running on a Remote Computer 688 29.20 Program: Create a Task-Specific Remoting Endpoint 688 30. Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693 30.1 Safely Experiment with Transactions 695 30.2 Change Error Recovery Behavior in Transactions 697 31. Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699 31.1 Respond to Automatically Generated Events 700 31.2 Create and Respond to Custom Events 703 31.3 Create a Temporary Event Subscription 706 31.4 Forward Events from a Remote Computer 707 31.5 Investigate Internal Event Action State 708 31.6 Use a Script Block as a .NET Delegate or Event Handler 710 Part V. References A. PowerShell Language and Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715 B. Regular Expression Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765 C. XPath Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773 D. .NET String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777 E. .NET DateTime Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781 F. Selected .NET Classes and Their Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787 G. WMI Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795 H. Selected COM Objects and Their Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803 I. Selected Events and Their Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807 J. Standard PowerShell Verbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815 Table of Contents | xv
  • 22.
    Index . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821 xvi | Table of Contents
  • 23.
    Foreword As someone whohas written, or contributed to, more than a dozen books, I am well aware of the incredible amount of work and monumental commitment of time and resources involved with writing a book. That someone would choose to do this at essentially the same time one is burning the midnight oil while developing one of the most exciting products in Microsoft’s history bespeaks a most committed person. However, more than simple commitment is involved. From my conversations with Lee, I can tell that he is passionate about Windows PowerShell. He sees the revolutionary changes introduced with the 2.0 release of the product. If Windows PowerShell 1.0 was the concept, Windows PowerShell 2.0 is the answer. If Windows PowerShell 1.0 was the vision, Windows PowerShell 2.0 is the reality. If Windows PowerShell 1.0 was for early adopters, Windows PowerShell 2.0 is moving into the mainstream. With the inclusion of Windows PowerShell 2.0 in Windows 7 and Windows Server 2008 R2, we are beginning to see the commitment Microsoft is making to the product. That the SharePoint, SQL, Exchange, Active Directory Domain Services (AD DS), In- ternet Information Server (IIS) teams, and others have all made cmdlets should tell you that Windows PowerShell is not a passing fad. Windows PowerShell questions are already cropping up on Microsoft Certification Examinations, and as a network ad- ministrator or a consultant, you will need to learn Windows PowerShell. Learning Windows PowerShell need not be tedious, boring, or exhausting. In fact, you will be joining a community that is at least as passionate about Windows PowerShell as Lee (or the rest of the Windows PowerShell team) or me (I write the Hey, Scripting Guy! blog seven days a week—the only Microsoft blog updated daily, by the way). What other product from Microsoft has inspired a half dozen songs to be written about it? Not by the marketing department, but by people who fell in love with Windows PowerShell, or, better yet, to use the community term: became addicted. I attended a recent SQL Saturday in Charlotte, North Carolina, because I wanted to meet and interact with members of the Windows PowerShell community. That is right: there is a huge group of hardcore SQL administrators who are adopting Windows PowerShell because of its cool server management capabilities. In addition, a project known as the SQL Server PowerShell Extensions (SQLPSX) module (available from CodePlex) has wrapped much of the SQL Management Objects (SMO) into more than xvii
  • 24.
    130 useful functions.This provides ease of use for people who are not experts with SQL SMO and Windows PowerShell. By leveraging modules, the community is taking advantage of one of the great new features of Windows PowerShell 2.0. In fact, there are more than 200 Windows PowerShell projects on CodePlex. One person presenting at SQL Saturday declared that the active Windows PowerShell community was one of thegreatstrengthsofWindowsPowerShell.Youarenotalonewhenitcomestolearning and implementing Windows PowerShell. I do not own every Windows PowerShell book ever written. I have probably looked at most Windows PowerShell books, but I found some of them redundant and some others confusing. However, a few of the Windows PowerShell books are essential. Lee’s 1.0 version of this book fell into that category. I keep it within arm’s length of my desk and grab it often. I have highlighted certain sections, dog-eared others, and placed sticky tabs on the more essential pages. Over the last couple years, Lee’s Windows PowerShell Cookbook has grown to look more like a skinny porcupine on a bad hair day than a typical scripting book—and that is a good thing, because his book is not a typical scripting book; it is a cookbook. Just like a “real cookbook” that contains recipes for meals, this fascinating volume is what I find myself thumbing through when I am hungry to try something new with Windows PowerShell. In reviewing Lee’s upgraded Windows PowerShell Cookbook, I see that I will not be placed on a diet of “foo” and “bar”; instead, there are tasty morsels such as Get- PageUrls, a way-cool script that illustrates using regular expressions to extract URLs from a downloaded web page. It even fixes relative URLs so that they include the server from which they originated. All this happens faster than you can say “super useful” three times. I found Chapter 14 on debugging to be well worth a careful read. Lee has a number of really good points, the premier one being: do not make the mistake in the first place. This echoes my own best practice. Of course, mistakes are made, errors are introduced, and that is when the debugger commands are called upon. Windows PowerShell 2.0 ships with some great debugging cmdlets, and Lee has some extremely cool scripts to simplify the process, or at least to reduce some of the tedium. I really like the Watch- Expression script because it automatically displays the values of expressions you wish to track. If Chapter 14 is worth a careful read, Chapter 18 is worth a sticky tab because you will find yourself coming back to it often. Security and script signing is a subject of much debate in the Windows PowerShell community. You will want to hear about security from the horse’s mouth. A common question I hear when giving presentations on Windows PowerShell is “How can I invoke a command as another user without switch- ing contexts?” The genesis of this question is, of course, the Unix sudo command. Lee has a useful script named Invoke-ElevatedCommand that allows you to accomplish this task. Most excellent. xviii | Foreword
  • 25.
    One other thingyou need to read about is the Windows PowerShell Integrated Scripting Environment (ISE), in Chapter 19. A common request for years was for Microsoft to write a script editor. For years, I have been telling people we did write a script editor— Notepad. The Windows PowerShell ISE is much better than Notepad. Not only is the Windows PowerShell ISE a great script editor in its own right, but the Windows PowerShell team also exposed an object model that allows you to modify its behavior and to configure it to work in the way you wish to work. Lee has a whole section in Chapter 19 that talks about the ISE and how to modify it. Working with files, directories, the registry, services, processes, WMI, remoting, trans- actions, and event handling—it is all in this book. I am not going to go over all that, because I do not want to spoil the plot. Suffice it to say that once this book sees print, it will rapidly join its dog-eared younger brother in that small collection of Windows PowerShell books that I consider essential. —Ed Wilson Microsoft Scripting Guy and author of Windows PowerShell 2.0 Best Practices Foreword | xix
  • 27.
    Foreword to theFirst Edition When Lee asked me to write the foreword to his new book I was pleasantly surprised. I was under the impression that forewords were written by people who were respected and accomplished in their chosen field. Apparently, that isn’t the case at all. My closest brush with accomplishment and respect came at a New Year’s celebration long ago and involved hairspray and a butane lighter. I guess it doesn’t matter too much—I mean, who reads the foreword to a scripting book anyway, right? Lee wanted one of the Microsoft Scripting Guys to write the foreword. He wrote this book for the same hard-working admin scripters who frequent the TechNet Script Center. Lee thought it would make sense to have an original member of that team provide some perspective on where Windows admin scripting has been and where, with Windows PowerShell, it is going. A lot has happened since Lee and I first spoke about this. I’ve left the Microsoft Scripting Guys team to work on the WMI SDK, and the Scripting Guys name has become a bit of a joke, given that the current driving force behind the team is a slight, half-sandwich- eating lady named Jean Ross. For now, Jean is keeping Greg around to do menial labor like packing up and shipping Dr. Scripto bobblehead dolls, but we’ll just see what happens when he finally runs out of topics for his Hey, Scripting Guy column. The future of scripting could very well be The Scripting Girl. Glue, Enablers, and a WSH Whenever I think “perspective” and “scripting”—which is far too often—I think Bob Wells. Bob takes his scripting very seriously and has been promoting it inside and outside of Microsoft for years. When I joined the Scripting Guys team, Bob would preach to me about “glue” and “enablers.” It took some time before I understood why he was talking about it so often and why finding just the right term for enablers was so important to him. I now know that it’s because crisply defining these two concepts establishes a simple, useful framework in which to think about admin scripting. The glue part is the scripting language itself—the foreachs, ifs, and vars. xxi
  • 28.
    It’s what youuse to orchestrate, or glue together, the set of subtasks you need to do to complete a larger task. The enablers (and, no, we never came up with a better term for them) are the instruments that actually accomplish each of the subtasks. This table lists the glue and enablers that we, as Windows scripters, have had available to us over the years. Glue Enabler Cmd.exe batch language Command-line tools (OS, ResKit, Support Tools) WSH Command-line tools (OS, ResKit, Support Tools) Automation-enabled COM objects (WMI, ADSI) Windows PowerShell Command-line tools (OS, ResKit, Support Tools) Automation-enabled COM objects (WMI, ADSI) .NET Framework Class Library Notice how each new environment lets you work with the enablers of the previous environment. This is important because it lets you carry forward your hard-earned knowledge. Objectively, we can say that WSH scripting is more powerful than batch scripting because it provides access to more enablers. You can automate more tasks because you have access to the additional functionality exposed by automatable COM objects. Less objectively, you could argue that even if you’re only going to use command-line tools as enablers, WSH is a better choice than batch because it provides some really useful glue functionality; advances in available enablers make more things possible while advances in glue (sometimes) make things more convenient. WSH scripting is a pretty capable environment. The WMI and ADSI COM libraries alone provide admins around the world with countless cycles of pain and elation. But there’s always that pesky task that you just can’t do with WSH, or that requires you to download a tool from some strangely named website at 2 a.m., when you really shouldn’t be making decisions about what to install on your production servers. If only VBScript included the infamous Win32 API among its enablers, then, like those strange creatures known as developers, you could do anything. Well, in developer land these days, the .NET Framework Class Library (FCL) is the new Win32 API. So, what we really need is a scripting environment that includes the FCL as an enabler. That’s exactly what Windows PowerShell does. In fact, Windows PowerShell runs in the same environment as that library and, as a result, works seam- lessly with it. I read a lot of press about the object-pipelining capabilities of Windows PowerShell. Those capabilities are very cool and represent an excellent advance in the gluedepartment—anadvancethatcertainlymakesworkingwiththeFCLmorenatural. But the addition of the FCL as an enabler is the thing that makes Jeffrey et al.’s creation objectively more powerful than WSH. And even if you don’t run into anything in the FCL that you need right away, it’s comforting to know that when you make an xxii | Foreword to the First Edition
  • 29.
    investment and developexpertise in this latest environment, you gain access to all the enablers that your developer counterparts currently have or will have in the foreseeable future. It should also be comforting to know that if you spend the time to learn Win- dows PowerShell, that knowledge should last you as long as the .NET Framework lasts Microsoft. Windows PowerShell follows in the tradition of WSH by improving on the glue aspect of its predecessor. One of the real pain points of working with COM objects in WSH was finding out what properties and methods were available. Unless you shelled out thebucksforasmarteditor,youlostalotofproductivitycontextswitchingfromwriting a script and consulting documentation. Not so when working with objects in Windows PowerShell. Type this at a Windows PowerShell prompt: $objShell = New-Object -com Shell.Application $objShell | Get-Member It does a scripter good, does it not? That Lee Guy Hopefully my rambling has convinced you that Windows PowerShell is a good thing and that it’s worth your time to learn it. Now, why do I think you should learn it by buying and reading this book? First off, I should tell you that the Windows PowerShell team is a bunch of odd ducks.* These folks are obsessed. From Jeffrey Snover on down, they are incredible teachers who love and believe in their technology so much that it’s difficult to stop them from teaching you! Even among that bunch of quackers, Lee stands out. Have you ever heard the sound an Exchange server makes when it cringes? Well, ours cringe when Lee comes to work and starts answering questions on our internal Windows PowerShell mailing list. Lee has amassed unique knowledge about how to leverage Windows PowerShell to address problems that arise in the real world. And he and O’Reilly have done us a great service by capturing and sharing some of that knowledge in this book. Windows system admin scripters are the coolest people on the planet. It continues to be a pleasure to work for you, and I sincerely hope you enjoy the book. —Dean Tsaltas Microsoft Scripting Guy Emeritus * Canadian ducks (Canuck ducks) in many cases. Foreword to the First Edition | xxiii
  • 31.
    Preface In late 2002,Slashdot posted a story about a “next-generation shell” rumored to be in development at Microsoft. As a longtime fan of the power unlocked by shells and their scriptinglanguages,thepostimmediatelycapturedmyinterest.Couldthisshellprovide the command-line power and productivity I’d long loved on Unix systems? Since I had just joined Microsoft six months earlier, I jumped at the chance to finally get to the bottom of a Slashdot-sourced Microsoft Mystery. The post talked about strong integration with the .NET Framework, so I posted a query to an internal C# mailing list. I got a response that the project was called “Monad,” which I then used to track down an internal prototype build. Prototype was a generous term. In its early stages, the build was primarily a proof of concept. Want to clear the screen? No problem! Just lean on the Enter key until your previous commands and output scroll out of view! But even at these early stages, it was immediately clear that Monad marked a revolution in command-line shells. As with many things of this magnitude, its beauty was self-evident. Monad passed full- fidelity .NET objects between its commands. For even the most complex commands, Monad abolished the (until now, standard) need for fragile text-based parsing. Simple and powerful data manipulation tools supported this new model, creating a shell both powerful and easy to use. I joined the Monad development team shortly after that to help do my part to bring this masterpiece of technology to the rest of the world. Since then, Monad has grown to become a real, tangible product—now called Windows PowerShell. So why write a book about it? And why this book? Many users have picked up PowerShell for the sake of learning PowerShell. Any tangible benefits come by way of side effect. Others, though, might prefer to opportunistically learn a new technology as it solves their needs. How do you use PowerShell to navigate the filesystem? How can you manage files and folders? Retrieve a web page? This book focuses squarely on helping you learn PowerShell through task-based solu- tions to your most pressing problems. Read a recipe, read a chapter, or read the entire book—regardless, you’re bound to learn something. xxv
  • 32.
    Who This BookIs For This book helps you use PowerShell to get things done. It contains hundreds of solutions to specific, real-world problems. For systems management, you’ll find plenty of exam- ples that show how to manage the filesystem, the Windows Registry, event logs, pro- cesses, and more. For enterprise administration, you’ll find two entire chapters devoted to WMI, Active Directory, and other enterprise-focused tasks. Along the way, you’ll also learn an enormous amount about PowerShell: its features, its commands, and its scripting language—but you’ll most importantly solve problems. How This Book Is Organized This book consists of five main sections: a guided tour of PowerShell, PowerShell fun- damentals, common tasks, administrator tasks, and a detailed reference. Part I: Tour A Guided Tour of Windows PowerShell breezes through PowerShell at a high level. It introduces PowerShell’s core features: • An interactive shell • A new command model • An object-based pipeline • A razor-sharp focus on administrators • A consistent model for learning and discovery • Ubiquitous scripting • Integration with critical management technologies • A consistent model for interacting with data stores The tour lets you become familiar with PowerShell as a whole. This familiarity will createamentalframeworkforyoutounderstandthesolutionsfromtherestofthebook. Part II: Fundamentals Chapters 1 through 8 cover the fundamentals that underpin the solutions in this book. This section introduces you to the PowerShell interactive shell, fundamental pipeline and object concepts, and many features of the PowerShell scripting language. Part III: Common Tasks Chapters 9 through 19 cover the tasks you will run into most commonly when starting to tackle more complex problems in PowerShell. This includes working with simple and structured files, Internet-connected scripts, code reuse, user interaction, and more. xxvi | Preface
  • 33.
    Part IV: AdministratorTasks Chapters 20 through 31 focus on the most common tasks in systems and enterprise management. Chapters 20 through 25 focus on individual systems: the filesystem, the registry, event logs, processes, services, and more. Chapters 26 and 27 focus on Active Directory, as well as the typical tasks most common in managing networked or domain- joined systems. Part V: References Many books belch useless information into their appendixes simply to increase page count. In this book, however, the detailed references underpin an integral and essential resource for learning and using PowerShell. The appendixes cover: • The PowerShell language and environment • Regular expression syntax and PowerShell-focused examples • XPath quick reference • .NET string formatting syntax and PowerShell-focused examples • .NET DateTime formatting syntax and PowerShell-focused examples • Administrator-friendly .NET classes and their uses • Administrator-friendly WMI classes and their uses • Administrator-friendly COM objects and their uses • Selected events and their uses • PowerShell’s standard verbs What You Need to Use This Book The majority of this book requires only a working installation of Windows PowerShell. Windows 7 and Windows Server 2008 R2 include Windows PowerShell by default. If you do not yet have PowerShell installed, you may obtain it by following the download link at http://www.microsoft.com/PowerShell. This link provides download instructions for PowerShell on Windows XP, Windows Server 2003, and Windows Vista. For Win- dows Server 2008, PowerShell comes installed as an optional component that you can enable through the Control Panel like other optional components. The Active Directory scripts given in Chapter 26 are most useful when applied to an enterprise environment, but Recipe 26.1 shows how to install additional software (Active Directory Lightweight Directory Services, or Active Directory Application Mode) that lets you run these scripts against a local installation. Preface | xxvii
  • 34.
    Conventions Used inThis Book The following typographical conventions are used in this book: Plain text Indicates menu titles, menu options, menu buttons, and keyboard accelerators Italic Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities Constant width Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, ob- jects, events, event handlers, tags, macros, or the output from commands Constant width bold Shows commands or other text that should be typed literally by the user Constant width italic Shows text that should be replaced with user-supplied values This icon signifies a tip, suggestion, or general note. This icon indicates a warning or caution. Code Examples Obtaining Code Examples To obtain electronic versions of the programs and examples given in this book, visit the Examples link at: http://www.oreilly.com/catalog/9780596801519 Using Code Examples This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example xxviii | Preface
  • 35.
    code does notrequire permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Windows PowerShell Cookbook by Lee Holmes. Copyright 2010 Lee Holmes, 978-0-596-80150-2.” If you feel your use of code examples falls outside fair use or the permission given, feel free to contact us at permissions@oreilly.com. Comments and Questions Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at: http://www.oreilly.com/catalog/9780596801502 To comment or ask technical questions about this book, send email to: bookquestions@oreilly.com For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at: http://www.oreilly.com Safari® Books Online Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly. Withasubscription,youcanreadanypageandwatchanyvideofromourlibraryonline. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, download chapters, bookmark key sections, and benefit from tons of other time-saving features. O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other pub- lishers, sign up for free at http://my.safaribooksonline.com. Preface | xxix
  • 36.
    Acknowledgments Writing is thetask of crafting icebergs. The heft of the book you hold in your hands is just a hint of the multiyear, multirelease effort it took to get it there. And by a cast much larger than me. The groundwork started decades ago. My parents nurtured my interest in computers and software, supported an evening-only bulletin board service, put up with “viruses” that told them to buy a new computer for Christmas, and even listened to me blather about batch files or how PowerShell compares to Excel. Without their support, who knows where I’d be. My family and friends have helped keep me sane for two editions of the book now. Ariel: you are the light of my life. Robin: thinking of you reminds me each day that serendipity is still alive and well in this busy world. Thank you to all of my friends and family for being there for me. You can have me back now. :) I would not have written either edition of this book without the tremendous influence of Guy Allen, visionary of the University of Toronto’s Professional Writing program. Guy: your mentoring forever changed me, just as it molds thousands of others from English hackers into writers. Of course, members of the PowerShell team (both new and old) are the ones that made this a book about PowerShell. Building this product with you has been a unique chal- lenge and experience—but most of all, a distinct pleasure. In addition to the PowerShell team, the entire PowerShell community defined this book’s focus. From MVPs, to early adopters, to newsgroup lurkers: your support, questions, and feedback have been the inspiration behind each page. Converting thoughts into print always involves a cast of unsung heroes, even though each author tries his best to convince the world how important these heroes are. Thank you to the many technical reviewers who participated in O’Reilly’s Open Feed- back Publishing System, especially Johannes Rössel, Aleksandar Nikolic, Jerome L. Cruz, David Moravec, Richard Siddaway, and Andrew Tearle. I truly appreciate you donating your nights and weekends to help craft something of which we can all be proud. To the awesome staff at O’Reilly—Mike Hendrickson, Genevieve d’Entremont, Teresa Elsey, Laurel Ruma, the O’Reilly Tools Monks, and the production team—your pa- tience and persistence helped craft a book that holds true to its original vision. You also ensuredthatthebookdidn’tjustknockaroundinmyheadbutactuallygotoutthedoor. This book would not have been possible without the support from each and every one of you. xxx | Preface
  • 37.
  • 39.
    TOUR A Guided Tourof Windows PowerShell Introduction Windows PowerShell promises to revolutionize the world of system management and command-line shells. From its object-based pipelines, to its administrator focus, to its enormous reach into other Microsoft management technologies, PowerShell drastically improves the productivity of administrators and power users alike. When learning a new technology, it is natural to feel bewildered at first by all the unfamiliar features and functionality. This perhaps rings especially true for users new to Windows PowerShell because it may be their first experience with a fully featured command-line shell. Or worse, they’ve heard stories of PowerShell’s fantastic integrated scripting capabilities and fear being forced into a world of programming that they’ve actively avoided until now. Fortunately, these fears are entirely misguided; PowerShell is a shell that both grows with you and grows on you. Let’s take a tour to see what it is capable of: • PowerShell works with standard Windows commands and applications. You don’t have to throw away what you already know and use. • PowerShell introduces a powerful new type of command. PowerShell commands (called cmdlets) share a common Verb-Noun syntax and offer many usability im- provements over standard commands. • PowerShell understands objects. Working directly with richly structured objects makes working with (and combining) PowerShell commands immensely easier than working in the plain-text world of traditional shells. • PowerShell caters to administrators. Even with all its advances, PowerShell focuses strongly on its use as an interactive shell: the experience of entering commands in a running PowerShell application. 3
  • 40.
    • PowerShell supportsdiscovery. Using three simple commands, you can learn and discover almost anything PowerShell has to offer. • PowerShell enables ubiquitous scripting. With a fully fledged scripting language that works directly from the command line, PowerShell lets you automate tasks with ease. • PowerShell bridges many technologies. By letting you work with .NET, COM, WMI, XML, and Active Directory, PowerShell makes working with these previ- ously isolated technologies easier than ever before. • PowerShell simplifies management of data stores. Through its provider model, PowerShell lets you manage data stores using the same techniques you already use to manage files and folders. We’ll explore each of these pillars in this introductory tour of PowerShell. If you are running Windows 7 or Windows 2008 R2, version two of PowerShell is already installed. If not, visit the download link at http://www.microsoft.com/PowerShell to in- stall it. PowerShell and its supporting technologies are together referred to as the Win- dows Management Framework. An Interactive Shell At its core, PowerShell is first and foremost an interactive shell. While it supports scripting and other powerful features, its focus as a shell underpins everything. Getting started in PowerShell is a simple matter of launching PowerShell.exe rather than cmd.exe—the shells begin to diverge as you explore the intermediate and advanced functionality, but you can be productive in PowerShell immediately. To launch Windows PowerShell, do one of the following: • Click Start→All Programs→Accessories→Windows PowerShell • Click Start→Run, and then type “PowerShell” A PowerShell prompt window opens that’s nearly identical to the traditional command prompt window of Windows XP, Windows Server 2003, and their many ancestors. The PS C:Documents and SettingsLee> prompt indicates that PowerShell is ready for input, as shown in Figure T-1. Once you’ve launched your PowerShell prompt, you can enter DOS-style and Unix- style commands to navigate around the filesystem just as you would with any Windows or Unix command prompt—as in the interactive session shown in Example T-1. In this example, we use the pushd, cd, dir, pwd, and popd commands to store the current loca- tion, navigate around the filesystem, list items in the current directory, and then return to the original location. Try it! 4 | A Guided Tour of Windows PowerShell
  • 41.
    Example T-1. Enteringmany standard DOS- and Unix-style file manipulation commands produces the same results you get when you use them with any other Windows shell PS C:Documents and SettingsLee> function Prompt { "PS > " } PS > pushd . PS > cd PS > dir Directory: C: Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 11/2/2006 4:36 AM $WINDOWS.~BT d---- 5/8/2007 8:37 PM Blurpark d---- 11/29/2006 2:47 PM Boot d---- 11/28/2006 2:10 PM DECCHECK d---- 10/7/2006 4:30 PM Documents and Settings d---- 5/21/2007 6:02 PM F&SC-demo d---- 4/2/2007 7:21 PM Inetpub d---- 5/20/2007 4:59 PM Program Files d---- 5/21/2007 7:26 PM temp d---- 5/21/2007 8:55 PM Windows -a--- 1/7/2006 10:37 PM 0 autoexec.bat Figure T-1. Windows PowerShell, ready for input A Guided Tour of Windows PowerShell | 5
  • 42.
    -ar-s 11/29/2006 1:39PM 8192 BOOTSECT.BAK -a--- 1/7/2006 10:37 PM 0 config.sys -a--- 5/1/2007 8:43 PM 33057 RUU.log -a--- 4/2/2007 7:46 PM 2487 secedit.INTEG.RAW PS > popd PS > pwd Path ---- C:Documents and SettingsLee In this example, our first command customizes the prompt. In cmd.exe, customizing the prompt looks like prompt $P$G. In bash, it looks like PS1="[h] w> ". In PowerShell, you define a function that returns whatever you want displayed. Recipe 11.2 introduces functions and how to write them. The pushd command is an alternative name (alias) to the much more descriptively named PowerShell command Push-Location. Likewise, the cd, dir, popd, and pwd com- mands all have more memorable counterparts. Although navigating around the filesystem is helpful, so is running the tools you know and love, such as ipconfig and notepad. Type the command name and you’ll see results like those shown in Example T-2. Example T-2. Windows tools and applications such as ipconfig run in PowerShell just as they do in cmd.exe PS > ipconfig Windows IP Configuration Ethernet adapter Wireless Network Connection 4: Connection-specific DNS Suffix . : hsd1.wa.comcast.net. IP Address. . . . . . . . . . . . : 192.168.1.100 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.1 PS > notepad (notepad launches) Entering ipconfig displays the IP addresses of your current network connections. En- tering notepad runs—as you’d expect—the Notepad editor that ships with Windows. Try them both on your own machine. 6 | A Guided Tour of Windows PowerShell
  • 43.
    Structured Commands (Cmdlets) Inaddition to supporting traditional Windows executables, PowerShell introduces a powerful new type of command called a cmdlet (pronounced command-let). All cmdlets are named in a Verb-Noun pattern, such as Get-Process, Get-Content, and Stop-Process. PS > Get-Process -Name lsass Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 668 13 6228 1660 46 932 lsass In this example, you provide a value to the ProcessName parameter to get a specific process by name. Once you know the handful of common verbs in PowerShell, learning how to work with new nouns becomes much easier. While you may never have worked with a certain object before (such as a Service), the standard Get, Set, Start, and Stop actions still apply. For a list of these common verbs, see Table J-1 in Appendix J. You don’t always have to type these full cmdlet names, however. PowerShell lets you use the Tab key to auto-complete cmdlet names and parameter names: PS > Get-Pr<TAB> -N<TAB> lsass For quick interactive use, even that may be too much typing. To help improve your efficiency, PowerShell defines aliases for all common commands and lets you define your own. In addition to alias names, PowerShell only requires that you type enough of the parameter name to disambiguate it from the rest of the parameters in that cmdlet. PowerShell is also case-insensitive. Using the built-in gps alias (which represents the Get-Process cmdlet) along with parameter shortening, you can instead type: PS > gps -n lsass Going even further, PowerShell supports positional parameters on cmdlets. Positional parameters let you provide parameter values in a certain position on the command line, rather than having to specify them by name. The Get-Process cmdlet takes a process name as its first positional parameter. This parameter even supports wildcards: PS > gps l*s A Guided Tour of Windows PowerShell | 7
  • 44.
    Deep Integration ofObjects PowerShell begins to flex more of its muscle as you explore the way it handles structured data and richly functional objects. For example, the following command generates a simple text string. Since nothing captures that output, PowerShell displays it to you: PS > "Hello World" Hello World The string you just generated is, in fact, a fully functional object from the .NET Frame- work. For example, you can access its Length property, which tells you how many characters are in the string. To access a property, you place a dot between the object and its property name: PS > "Hello World".Length 11 All PowerShell commands that produce output generate that output as objects as well. For example, the Get-Process cmdlet generates a System.Diagnostics.Process object, which you can store in a variable. In PowerShell, variable names start with a $ character. If you have an instance of Notepad running, the following command stores a reference to it: $process = Get-Process notepad Since this is a fully functional Process object from the .NET Framework, you can call methods on that object to perform actions on it. This command calls the Kill() meth- od, which stops a process. To access a method, you place a dot between the object and its method name: $process.Kill() PowerShell supports this functionality more directly through the Stop-Process cmdlet, but this example demonstrates an important point about your ability to interact with these rich objects. Administrators as First-Class Users While PowerShell’s support for objects from the .NET Framework quickens the pulse of most users, PowerShell continues to focus strongly on administrative tasks. For ex- ample, PowerShell supports MB (for megabyte) and GB (for gigabyte) as some of its standard administrative constants. For example, how many disks will it take to back up a 40 GB hard drive to CD-ROM? PS > 40GB / 650MB 63.0153846153846 8 | A Guided Tour of Windows PowerShell
  • 45.
    Although the .NETFramework is traditionally a development platform, it contains a wealth of functionality useful for administrators too! In fact, it makes PowerShell a great calendar. For example, is 2008 a leap year? PowerShell can tell you: PS > [DateTime]::IsLeapYear(2008) True Going further, how might you determine how much time remains until summer? The following command converts "06/21/2011" (the start of summer) to a date, and then subtracts the current date from that. It stores the result in the $result variable, and then accesses the TotalDays property. PS > $result = [DateTime] "06/21/2011" - [DateTime]::Now PS > $result.TotalDays 283.0549285662616 Composable Commands Whenever a command generates output, you can use a pipeline character (|) to pass that output directly to another command as input. If the second command understands the objects produced by the first command, it can operate on the results. You can chain together many commands this way, creating powerful compositions out of a few simple operations. For example, the following command gets all items in the Path1 directory and moves them to the Path2 directory: Get-Item Path1* | Move-Item -Destination Path2 You can create even more complex commands by adding additional cmdlets to the pipeline. In Example T-3, the first command gets all processes running on the system. It passes those to the Where-Object cmdlet, which runs a comparison against each in- coming item. In this case, the comparison is $_.Handles -ge 500, which checks whether the Handles property of the current object (represented by the $_ variable) is greater than or equal to 500. For each object in which this comparison holds true, you pass the results to the Sort-Object cmdlet, asking it to sort items by their Handles property. Finally, you pass the objects to the Format-Table cmdlet to generate a table that contains the Handles, Name, and Description of the process. Example T-3. You can build more complex PowerShell commands by using pipelines to link cmdlets, as shown in this example with Get-Process, Where-Object, Sort-Object, and Format-Table PS > Get-Process | Where-Object { $_.Handles -ge 500 } | Sort-Object Handles | Format-Table Handles,Name,Description -Auto Handles Name Description ------- ---- ----------- 588 winlogon 592 svchost A Guided Tour of Windows PowerShell | 9
  • 46.
    667 lsass 725 csrss 742System 964 WINWORD Microsoft Office Word 1112 OUTLOOK Microsoft Office Outlook 2063 svchost Techniques to Protect You from Yourself While aliases, wildcards, and composable pipelines are powerful, their use in com- mands that modify system information can easily be nerve-wracking. After all, what does this command do? Think about it, but don’t try it just yet: PS > gps [b-t]*[c-r] | Stop-Process It appears to stop all processes that begin with the letters b through t and end with the letters c through r. How can you be sure? Let PowerShell tell you. For commands that modify data, PowerShell supports -WhatIf and -Confirm parameters that let you see what a command would do: PS > gps [b-t]*[c-r] | Stop-Process -whatif What if: Performing operation "Stop-Process" on Target "ctfmon (812)". What if: Performing operation "Stop-Process" on Target "Ditto (1916)". What if: Performing operation "Stop-Process" on Target "dsamain (316)". What if: Performing operation "Stop-Process" on Target "ehrecvr (1832)". What if: Performing operation "Stop-Process" on Target "ehSched (1852)". What if: Performing operation "Stop-Process" on Target "EXCEL (2092)". What if: Performing operation "Stop-Process" on Target "explorer (1900)". (...) In this interaction, using the -WhatIf parameter with the Stop-Process pipelined com- mand lets you preview which processes on your system will be stopped before you actually carry out the operation. Note that this example is not a dare! In the words of one reviewer: Not only did it stop everything, but on Vista, it forced a shutdown with only one minute warning! It was very funny though … At least I had enough time to save everything first! Common Discovery Commands While reading through a guided tour is helpful, I find that most learning happens in an ad hoc fashion. To find all commands that match a given wildcard, use the Get-Command cmdlet. For example, by entering the following, you can find out which PowerShell commands (and Windows applications) contain the word process. 10 | A Guided Tour of Windows PowerShell
  • 47.
    PS > Get-Command*process* CommandType Name Definition ----------- ---- ---------- Cmdlet Get-Process Get-Process [[-Name] <Str... Application qprocess.exe c:windowssystem32qproc... Cmdlet Stop-Process Stop-Process [-Id] <Int32... To see what a command such as Get-Process does, use the Get-Help cmdlet, like this: PS > Get-Help Get-Process Since PowerShell lets you work with objects from the .NET Framework, it provides the Get-Member cmdlet to retrieve information about the properties and methods that an object, such as a .NET System.String, supports. Piping a string to the Get-Member com- mand displays its type name and its members: PS > "Hello World" | Get-Member TypeName: System.String Name MemberType Definition ---- ---------- ---------- (...) PadLeft Method System.String PadLeft(Int32 tota... PadRight Method System.String PadRight(Int32 tot... Remove Method System.String Remove(Int32 start... Replace Method System.String Replace(Char oldCh... Split Method System.String[] Split(Params Cha... StartsWith Method System.Boolean StartsWith(String... Substring Method System.String Substring(Int32 st... ToCharArray Method System.Char[] ToCharArray(), Sys... ToLower Method System.String ToLower(), System.... ToLowerInvariant Method System.String ToLowerInvariant() ToString Method System.String ToString(), System... ToUpper Method System.String ToUpper(), System.... ToUpperInvariant Method System.String ToUpperInvariant() Trim Method System.String Trim(Params Char[]... TrimEnd Method System.String TrimEnd(Params Cha... TrimStart Method System.String TrimStart(Params C... Chars ParameterizedProperty System.Char Chars(Int32 index) {... Length Property System.Int32 Length {get;} Ubiquitous Scripting PowerShell makes no distinction between the commands typed at the command line and the commands written in a script. Your favorite cmdlets work in scripts and your favorite scripting techniques (e.g., the foreach statement) work directly on the com- mand line. For example, to add up the handle count for all running processes: PS > $handleCount = 0 PS > foreach($process in Get-Process) { $handleCount += $process.Handles } PS > $handleCount 19403 A Guided Tour of Windows PowerShell | 11
  • 48.
    While PowerShell providesa command (Measure-Object) to measure statistics about collections, this short example shows how PowerShell lets you apply techniques that normally require a separate scripting or programming language. In addition to using PowerShell scripting keywords, you can also create and work di- rectlywithobjectsfromthe.NETFrameworkthatyoumaybefamiliarwith.PowerShell becomes almost like the C# immediate mode in Visual Studio. Example T-4 shows how PowerShell lets you easily interact with the .NET Framework. Example T-4. Using objects from the .NET Framework to retrieve a web page and process its content PS > $webClient = New-Object System.Net.WebClient PS > $content = $webClient.DownloadString("http://blogs.msdn.com/PowerShell/rss.aspx") PS > $content.Substring(0,1000) <?xml version="1.0" encoding="UTF-8" ?> <?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedS tylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://pu rl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slas h/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Windo (...) Ad Hoc Development By blurring the lines between interactive administration and writing scripts, the history buffers of PowerShell sessions quickly become the basis for ad hoc script development. In this example, you call the Get-History cmdlet to retrieve the history of your session. For each item, you get its CommandLine property (the thing you typed) and send the output to a new script file. PS > Get-History | Foreach-Object { $_.CommandLine } > c:tempscript.ps1 PS > notepad c:tempscript.ps1 (save the content you want to keep) PS > c:tempscript.ps1 If this is the first time you’ve run a script in PowerShell, you will need to configure your Execution Policy. For more information about select- ing an execution policy, see Recipe 18.1. For more detail about saving your session history into a script, see Recipe 1.17. Bridging Technologies We’ve seen how PowerShell lets you fully leverage the .NET Framework in your tasks, but its support for common technologies stretches even further. As Example T-5 (con- tinued from Example T-4) shows, PowerShell supports XML. 12 | A Guided Tour of Windows PowerShell
  • 49.
    Example T-5. Workingwith XML content in PowerShell PS > $xmlContent = [xml] $content PS > $xmlContent xml xml-stylesheet rss --- -------------- --- version="1.0" encoding... type="text/xsl" href="... rss PS > $xmlContent.rss version : 2.0 dc : http://purl.org/dc/elements/1.1/ slash : http://purl.org/rss/1.0/modules/slash/ wfw : http://wellformedweb.org/CommentAPI/ channel : channel PS > $xmlContent.rss.channel.item | select Title title ----- CMD.exe compatibility Time Stamping Log Files Microsoft Compute Cluster now has a PowerShell Provider and Cmdlets The Virtuous Cycle: .NET Developers using PowerShell (...) PowerShell also lets you work with Windows Management Instrumentation (WMI): PS > Get-WmiObject Win32_Bios SMBIOSBIOSVersion : ASUS A7N8X Deluxe ACPI BIOS Rev 1009 Manufacturer : Phoenix Technologies, LTD Name : Phoenix - AwardBIOS v6.00PG SerialNumber : xxxxxxxxxxx Version : Nvidia - 42302e31 Or, as Example T-6 shows, Active Directory Service Interfaces (ADSI). Example T-6. Working with Active Directory in PowerShell PS > [ADSI] "WinNT://./Administrator" | Format-List * UserFlags : {66113} MaxStorage : {-1} PasswordAge : {19550795} PasswordExpired : {0} LoginHours : {255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255} FullName : {} Description : {Built-in account for administering the compu ter/domain} A Guided Tour of Windows PowerShell | 13
  • 50.
    BadPasswordAttempts : {0} LastLogin: {5/21/2007 3:00:00 AM} HomeDirectory : {} LoginScript : {} Profile : {} HomeDirDrive : {} Parameters : {} PrimaryGroupID : {513} Name : {Administrator} MinPasswordLength : {0} MaxPasswordAge : {3710851} MinPasswordAge : {0} PasswordHistoryLength : {0} AutoUnlockInterval : {1800} LockoutObservationInterval : {1800} MaxBadPasswordsAllowed : {0} RasPermissions : {1} objectSid : {1 5 0 0 0 0 0 5 21 0 0 0 121 227 252 83 122 130 50 34 67 23 10 50 244 1 0 0} Or, as Example T-7 shows, even scripting traditional COM objects. Example T-7. Working with COM objects in PowerShell PS > $firewall = New-Object -com HNetCfg.FwMgr PS > $firewall.LocalPolicy.CurrentProfile Type : 1 FirewallEnabled : True ExceptionsNotAllowed : False NotificationsDisabled : False UnicastResponsesToMulticastBroadcastDisabled : False RemoteAdminSettings : System.__ComObject IcmpSettings : System.__ComObject GloballyOpenPorts : {Media Center Extender Serv ice, Remote Media Center Ex perience, Adam Test Instanc e, QWAVE...} Services : {File and Printer Sharing, UPnP Framework, Remote Desk top} AuthorizedApplications : {Remote Assistance, Windows Messenger, Media Center, T rillian...} Namespace Navigation Through Providers Another avenue PowerShell offers for working with the system is providers. PowerShell providers let you navigate and manage data stores using the same techniques you al- ready use to work with the filesystem, as illustrated in Example T-8. 14 | A Guided Tour of Windows PowerShell
  • 51.
    Example T-8. Navigatingthe filesystem PS > Set-Location c: PS > Get-ChildItem Directory: C: Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 11/2/2006 4:36 AM $WINDOWS.~BT d---- 5/8/2007 8:37 PM Blurpark d---- 11/29/2006 2:47 PM Boot d---- 11/28/2006 2:10 PM DECCHECK d---- 10/7/2006 4:30 PM Documents and Settings d---- 5/21/2007 6:02 PM F&SC-demo d---- 4/2/2007 7:21 PM Inetpub d---- 5/20/2007 4:59 PM Program Files d---- 5/21/2007 11:47 PM temp d---- 5/21/2007 8:55 PM Windows -a--- 1/7/2006 10:37 PM 0 autoexec.bat -ar-s 11/29/2006 1:39 PM 8192 BOOTSECT.BAK -a--- 1/7/2006 10:37 PM 0 config.sys -a--- 5/1/2007 8:43 PM 33057 RUU.log -a--- 4/2/2007 7:46 PM 2487 secedit.INTEG.RAW This also works on the registry, as shown in Example T-9. Example T-9. Navigating the registry PS > Set-Location HKCU:SoftwareMicrosoftWindows PS > Get-ChildItem Hive: HKEY_CURRENT_USERSoftwareMicrosoftWindows SKC VC Name Property --- -- ---- -------- 30 1 CurrentVersion {ISC} 3 1 Shell {BagMRU Size} 4 2 ShellNoRoam {(default), BagMRU Size} PS > Set-Location CurrentVersionRun PS > Get-ItemProperty . (...) FolderShare : "C:Program FilesFolderShareFolderShare.exe" / background TaskSwitchXP : d:leetoolsTaskSwitchXP.exe ctfmon.exe : C:WINDOWSsystem32ctfmon.exe Ditto : C:Program FilesDittoDitto.exe (...) A Guided Tour of Windows PowerShell | 15
  • 52.
    Or even themachine’s certificate store, as Example T-10 illustrates. Example T-10. Navigating the certificate store PS > Set-Location cert:CurrentUserRoot PS > Get-ChildItem Directory: Microsoft.PowerShell.SecurityCertificate::CurrentUserRoot Thumbprint Subject ---------- ------- CDD4EEAE6000AC7F40C3802C171E30148030C072 CN=Microsoft Root Certificate... BE36A4562FB2EE05DBB3D32323ADF445084ED656 CN=Thawte Timestamping CA, OU... A43489159A520F0D93D032CCAF37E7FE20A8B419 CN=Microsoft Root Authority, ... 9FE47B4D05D46E8066BAB1D1BFC9E48F1DBE6B26 CN=PowerShell Local Certifica... 7F88CD7223F3C813818C994614A89C99FA3B5247 CN=Microsoft Authenticode(tm)... 245C97DF7514E7CF2DF8BE72AE957B9E04741E85 OU=Copyright (c) 1997 Microso... (...) Much, Much More As exciting as this guided tour was, it barely scratches the surface of how you can use PowerShell to improve your productivity and systems management skills. For more information about getting started in PowerShell, see Chapter 1. 16 | A Guided Tour of Windows PowerShell
  • 53.
    PART II Fundamentals Chapter 1,The Windows PowerShell Interactive Shell Chapter 2, Pipelines Chapter 3, Variables and Objects Chapter 4, Looping and Flow Control Chapter 5, Strings and Unstructured Text Chapter 6, Calculations and Math Chapter 7, Lists, Arrays, and Hashtables Chapter 8, Utility Tasks
  • 55.
    Other documents randomlyhave different content
  • 56.
    Sure of allfriends the blackest we can find Are those ingrates who stab our peace of mind.” A not uncommon and much more agreeable verse sets forth the duties of man towards woman in so far as matrimony is concerned: — “Adam alone in Paradise did grieve And thought Eden a desert without Eve, Until God pitying his lonesome state Crown’d all his wishes with a lovely mate. Then why should men think mean, or slight her, That could not live in Paradise without her.” Samplers bearing the foregoing verse are usually decorated with a picture of our first parents and the Tree of Knowledge, supported by a demon and angel. The parent or teacher sometimes spoke through the sampler, as thus, in Lucia York’s, dated 1725:— “Oh child most dear Incline thy ear And hearken to God’s voice.” Or again:— “Return the kindness that you do receive As far as your ability gives leave.” Mary Lounds. “Humility I’d recommend Good nature, too, with ease, Be generous, good, and kind to all, You’ll never fail to please.” Susanna Hayes.
  • 57.
    Samplers Expatiating uponVirtue or Vice, Wealth or Poverty, Happiness or Misery Amongst these may be noted:— “Happy is he, the only man, Who out of choice does all he can Who business loves and others better makes By prudent industry and pains he takes. God’s blessing here he’ll have and man’s esteem, And when he dies his works will follow him.” Of those dealing with wealth or poverty none, perhaps, is more incisive than this:— “The world’s a city full of crooked streets, And Death’s the market-place where all men meet; If life was merchandise that men could buy The rich would always live, the poor alone would die.” An American sampler has the following from Burns’s “Grace before Meat”:— “Some men have meat who cannot eat And some have none who need it. But we have meat and we can eat, And so the Lord be thanked.”
  • 58.
    Larger Image Plate VIII.—Samplerby Mary Postle. Dated 1747. Mrs C. J. Longman. An early specimen of a bordered Sampler, dated 1747, the rows being relegated to a small space in the centre, where they are altogether an insignificant feature in comparison with the border. Some of the ornament to which we have been accustomed in the rows survives, as for instance the pinks, but a new one is introduced, namely, the strawberry. Here are also the Noah’s Ark animals, trees, etc., which henceforward
  • 59.
    become common objectsand soon transform the face of the Sampler. The border itself is in evident imitation of the worsted flower work with which curtains, quilts, and other articles were freely adorned in the early eighteenth century. Inscriptions having an Interest owing to their Quaintness The following dates from 1740, and has as appendix the line, “God prosper the war”:— “The sick man fasts because he cannot eat The poor man fasts because he hath no meat The miser fasts to increase his store The glutton fasts because he can eat no more The hypocrite fasts because he’d be condemned The just man fasts cause he hath offended.” An American version of this ends with:— “Praise God from whom all blessings flow We have meat enow.” That self-conceit was not always considered a failing, is evident from the following verses:— “This needlework of mine may tell That when a child I learned well And by my elders I was taught Not to spend my time for nought,” which is concentrated and intensified in one of Frances Johnson, worked in 1797:—
  • 60.
    “In reading thisif any faults you see Mend them yourself and find no fault in me.” In a much humbler strain is this from an old sampler in Mrs Longman’s collection:— “When I was young I little thought That wit must be so dearly bought But now experience tells me how If I must thrive, then I must bowe And bend unto another will, That I might learn both arte & skill.” Owing to the portrayal of an insect, which was not infrequently met with in days gone by, upon the face of the sampler which bears the following lines, it has been suggested that they were presumably written by that creature:— “Dear Debby I love you sincerely My heart retains a grateful sense of your past kindness When will the hours of our Separation be at an end? Preserve in your bosom the remembrance of your affectionate Deborah Jane Berkin.” The following, coming about the date when the abolition of the slave trade was imminent, may have reference to it:— “THERE’S mercy in each ray of light, that mortal eye e’er saw, There’s mercy in each breath of air, that mortal lips can draw, There’s mercy both for bird, and beast, in God’s indulgent plan, There’s mercy for each creeping thing—But man has none for man.”
  • 61.
    Elizabeth Jane Gates Aged12 years, 1829. Riddle samplers, such as that of Ann Witty, do not often occur:— “I had both Money and a Friend by both I set great store I lent my to my and took his word therefor I asked my of my and nought but words I got I lost my and my for sue him, I would not.” Here, too, is an “Acrostick,” the first letters of whose lines spell the name of the young lady who “ended” it “Anno Dom. 1749.” “A virgin that’s Industrious Merits Praise, Nature she Imitates in Various Ways, Now forms the Pink, now gives the Rose its blaze. Young Buds, she folds, in tender Leaves of green, Omits no shade to beautify her Scene, Upon the Canvas, see, the Letters rise, Neatly they shine with intermingled dies, Glide into Words, and strike us with Surprize.” E. W. As illustrations of tales the sampler of Sarah Young (Fig. 15) is an unusual example. It deals with Sir Richard Steele’s story of the loves of Inkle and Yarico. Inkle, represented as a strapping big sailor, was cast away in the Spanish Main, where he met and loved Yarico, an Indian girl, but showed his baseness by selling her for a slave when he reached Barbadoes in a vessel which rescued him. The story evidently had a considerable, if fleeting, popularity, for it was dramatised.
  • 62.
    The Design, Ornamentand Colouring of Samplers Whilst important clues to the age of a sampler may be gathered from its form and legend, its design and colouring are factors from which almost as much may be learnt. Design can be more easily learned from considering in detail the illustrations, which have been mainly chosen for their typifying one or other form of it, but certain general features are so usually present that they may be summarised here. No one with any knowledge of design can look through the specimens of samplers selected for this volume without noting, first, that it is, in the earlier specimens, appropriate to the subject, decorative in treatment, and lends itself to a variety of treatment with the needle. Secondly, that the decoration is not English in origin, but is usually derived from foreign sources. Indeed, if we are to believe an old writer of the Jacobean time, the designs were “Collected with much praise and industrie, From scorching Spaine and freezing Muscovie, From fertile France and pleasant Italie, From Poland, Sweden, Denmarke, Germanie, And some of these rare patternes have been set Beyond the boundes of faithlesse Mahomet, From spacious China and those Kingdomes East And from great Mexico, the Indies West. Thus are these workes farre fetch’t and dearly bought, And consequently good for ladyes thought.” Thirdly, that after maintaining a remarkable uniformity until the end of the seventeenth century, design falls away, and with rare exceptions continuously declines until it reaches a mediocrity to which the term can hardly be applied.
  • 63.
    Larger Image Fig. 15.—Samplerby Sarah Young. About 1750. Mrs Head. The same features are noticeable in the colouring. The samplers of the Caroline period are in the main marked by a softness and delicacy, with a preference for tender and harmonious shades of pinks, greens, and blues, but these quickly pass out of the schemes of colouring until their revival a few years ago through the influence of Japan and the perspicuity, of Sir Lazenby Liberty. This delicacy is not, as some suppose, due to time having softened the colours, for examination shows that fading has seldom taken place, in fact one of the most remarkable traits of the earlier samplers is the wonderful
  • 64.
    condition of theircolouring (see Mrs Longman’s sampler of 1656, Plate IV., as an example). Towards the end of the seventeenth century the adoption of a groundwork of roughish close-textured canvas of a canary hue also militated against this ensemble of the colour scheme, which is now and again too vivid, especially in the reds, a fact which may, in part, be due to their retaining their original tint with a persistency that has not endured with the other dyes. During the early Georgian era sampler workers seem to have passed through a stage of affection for deep reds, blues, and greens, with which they worked almost all their lettering. The same colours are met with in the large embroidered curtains of the time; it is probably due to the influence of the tapestries and the Chinese embroideries then so much in vogue. In the opening years of the eighteenth century a pride in lettering gave rise to a series of samplers of little interest or artistic value, consisting, as they did, of nothing else than long sentences, not readily readable, and worked in silks in colours of every imaginable hue used indiscriminately, even in a single word, without any thought bestowed on harmony or effect of colouring. Later on, towards the middle of the century, more sober schemes of colour set in, consisting in the abandonment of reds and the employment of little else than blues, greens, yellows, and blacks (see Plate IX.), which are attractive through their quietness and unity. Subsequently but little praise can be bestowed upon samplers so far as their design is concerned. Occasionally, as in that of Mr Ruskin’s ancestress (Plate X.), a result which is satisfactory, both in colour and design, is arrived at, but this is generally due to individual taste rather than to tuition or example. In this respect samplers only follow in the wake of all the other arts—furniture and silversmiths’ work, perhaps, excepted, as regards both of which the taste displayed was also individual rather than national.
  • 65.
    An evil whichcankered later sampler ornamentation was a desire for novelty and variety. The earliest samplers exhibit few signs of attempts at invention in design. A comparison of any number of them shows ideas repeated again and again with the slightest variation. The same floral motives are adapted in almost every instance, and one and all may well have been employed since the days when they arrived from the Far East, brought, it may be, by the Crusaders. But it is in no derogatory spirit that I call attention to this lack of originality. A craftsman is doing a worthier thing in assimilating designs which have shown their fitness by centuries of use, patterns which are examples of fine decorative ornament that really beautifies the object to which it is applied, than in inventing weak and imperfect originals. No architect is accused of plagiarism if he introduces the pointed arch, and the great designs of the past are free and out of copyright. The Greek fret, or the Persian rose, is as much the property of anyone as the daisy or the snowdrop, and it was far better to make sound decorative pieces of embroidery on the lines of these than to attempt, as was done later on, feeble originals, which have nothing ornamental or decorative in their composition. The workers of the East, when perfection was arrived at in a design, did not hesitate to reproduce it again and again for centuries.
  • 66.
    Larger Image Plate IX.—Samplerby E. Philips. Dated 1761. Author’s Collection. Were it not that this Sampler was produced by little Miss Philips at the tender age of seven, there would be a probability that it was unique through its containing a portrait of the producer. For in no other example have we so many evidences pointing to its being a record of actual facts. For instance, there is clearly shown a gentleman pointing to his wife (in a hooped costume), and
  • 67.
    having round himhis five girls of various ages, the youngest in the care of a nurse. In the upper left corner is his son in charge of a tutor, whilst on the right are two maid-servants, one being a woman of colour. This fashion for black servants is further emphasised by the negro boy with the dog. That these should be present in this family is not remarkable, for by the lower illustration it is evident that Mr Philips was a traveller who had crossed the seas in his ship to where alligators, black swans and other rare birds abounded. The work was executed in 1761, the second year of George the Third, whose monogram and crown are supported by two soldiers in the costume of the period. It has been most dexterously carried out by the young lady, and it is conceived in a delicate harmony of greens and blues which was not uncommon at that time. Size, 19 × 12½. An adaptation of this Sampler has been utilised as the drop scene to the play of “Peter Pan.” But the mistress of a ladies’ improving school would hardly like her pupils to copy time after time the same designs—designs which perhaps resembled those of a rival establishment. Such a one would be oblivious to the fact that an ornamentalist is born not made, that the best design is traditional, and that pupils would be far more worthily employed in perpetuating ornamentation which had been invented by races intuitively gifted for such a purpose, than in attempting feeble products of her own brain. So, too, results show
  • 68.
    that she was,as a rule, unaware that good design is better displayed in simplicity than in pretentiousness. As that authority on design, the late Lewis Day, wrote in his volume on Embroidery, “The combination of a good designer and worker in the same person is an ideal very occasionally to be met with, and any attempt to realise it generally fails.” Samplers show in increasing numbers as the end approaches that their designers were ignorant of most of the elementary rules of ornamentation in needlework, such, for instance, as that the pictorial is not a suitable subject for reproduction, nor the delineation of the human figure, nor that the floral and vegetable kingdom, whilst lending itself better than aught else, should be treated from the decorative, and not the realistic point of view. We will now pass on to consider generally the forms of decoration most usually met with. Sampler Design: the Human Figure Whilst embroideries in imitation of tapestries deal almost entirely with the portrayal of the human figure, samplers of the same period, and that the best, for the most part avoid it. This is somewhat remarkable, for the design of the Renaissance, which was universally practised at the time upon which we are dwelling, was almost entirely given up to weaving it into other forms, and the volumes which treat of embroidery show how frequently it occurs in foreign pieces of needlework. The omission is a curious one, but the reason for it is, apparently, not far to seek. If we examine the earlier pieces we shall see that practically one type of figure only presents itself. Save in exceptional pieces, such as Mrs Longman’s early piece (Plate IV.), where the figures are clearly copied from one of the small tapestry pieces so in vogue at that date (1656), or Mrs Millett’s piece
  • 69.
    (Fig. 16), thefigures which appear upon samplers are all cast in one mould, and in no way improve but rather mar the composition. This last-named drawn-work sampler is a specimen altogether apart for beauty of design and workmanship. Doubts have been expressed as to its English origin, but portions of the ornament, such as the acorn, and the Stuart S in the lowest row, are thoroughly English; besides, as we have seen, design in almost every one of the seventeenth-century samplers is infected with foreign motives. The uppermost panel is supposed to represent Abraham, Sarah, and the Angel. To the left is the tent, with the folds worked in relief, in a stitch so fine as to defy ordinary eyesight. Sarah, who holds up a hand in astonishment at the angel’s announcement, has her head- dress, collar, and skirt in relief, the latter being sewn with microscopic fleurs-de-lis. The winged angel to the left of Abraham has a skirt composed of tiny scallops, which may represent feathers. A rabbit browses in front of the tent. The centre of the second row is occupied by a veiled mermaid, her tail covered with scalloped scale in relief. She holds in either hand a cup and a mask. The lettering in the two flanking panels is “S.I.D. 1649 A.I.” The decorative motive of the outer panels is peapods in relief, some open and disclosing peas. Roses and tulips fill the larger square below, and these are followed by a row (reversed) of tulips and acorns. Four other rows complete the sampler, which only measures 18½ × 6¾. In order to give it a larger size the lowest row is not reproduced. I have seen another drawn-work sampler which antedates that just described by a year. It is of somewhat coarse texture but is good in design, and bears in a panel at the side initials and the date. The Victoria and Albert Museum has also two somewhat similar drawn-work samplers—one by Elizabeth Wood, dated 1666, which contains the Stuart S’s; the other (undated) has the arms of James I.
  • 70.
    Larger Image Fig. 16.—Drawn-WorkSampler by S. I. D. Dated 1649. Mrs C. F. Millett.
  • 71.
    Larger Image Fig. 17.—Samplerby Jean Porter. 1709-10. A type of figure prevalent in early samplers has puzzled collectors who possess specimens containing it. It wears a close-fitting costume and has arms extended, and has received the name of a “Boxer,” presumably from its attitude and costume. It and a companion are continuously depicted for nearly a century, finally disappearing about 1742, but maintaining their attitude with less variation than any other form of ornament, the only alteration being in the form of the trophy which they hold in one hand. It is this trophy, if we may use such a term, that negatives the idea of their being combatant figures, and it almost with certainty places them in
  • 72.
    the category ofthe Greek Erotes, the Roman Amores, or the Cupids of the Renaissance. It is difficult to give a name to the trophy in most of the samplers, and the worker was clearly often in doubt as to its structure. In some it resembles a small vase with a lid, in others a spray with branches or leaves on either side. In one of 1673 it takes the form of a four-petalled flower, and in one of 1679 that of an acorn, which is repeated in samplers of 1684, 1693, and 1694, this repetition being probably due to the acorn being a very favourite subject for design under the Stuarts. In a sampler of 1693 acorns are held in either hand. In one of 1742 (Fig. 18), the object held is a kind of candelabra. The little figures themselves preserve a singular uniformity of costume, which again points to their being the nude Erotes, clothed, to suit the times, in a tight-fitting jerkin and drawers. These are always of gayest colours. On occasions (as in a sampler dated 1693) they don a coat, and have long wigs, bringing them into line with the prevailing fashion. When these figures disappear their place is taken by those of our first parents in the Garden of Eden, the incongruity of which is well depicted in the sampler illustrated in Fig. 17. This piece of work, which took nearly a year to complete—it was begun on 14th May 1709, and finished on 6th April 1710—is unlike any other that I have seen of that period, for it antedates, by nearly half a century, the scenes from real life which afterwards became part and parcel of every sampler. Adam and Eve became quite common objects on samplers after 1760.[5] Mention need only be made here of the dressed figures which occur in samplers dated during the reign of George the Third. They are sometimes quaint (as in Plates IX. and XI.), but they hardly come into any scheme of decoration. The squareness of the stitch used in later samplers renders any imitation of painting such as was attempted altogether a failure.
  • 73.
    Larger Image Fig. 18.—Sampler.Name Illegible. Date 1742. Formerly in the Author’s Collection. Sampler Design: Animals Animals in any true decorative sense hardly came into sampler ornament. Whilst the tapestry pictures teem with them, so that one wanting in a lion or stag is a rarity, in samplers, probably, the difficulty of obtaining rounded forms with the stitch used in the large grained canvas was a deterrent. The lion only being found on the Fletwood sampler of 1654 (Fig. 44) and the stag, which in tapestry
  • 74.
    pictures usurps theplace of the unicorn, appears but rarely on samplers before the middle of the eighteenth century, when it came into fashion, and afterwards occurs with uninterrupted regularity so long as samplers were made. This neglect of animals is hardly to be deplored, for when they do occur they are little else than caricatures (see, for instance, those in Plate III.). Birds, which lend themselves to needlework, appear in the later samplers (Plate XI. and Fig. 18), but hardly as part of any decorative scheme. Sampler Design: Flowers With the practically insignificant exceptions which we have just noticed, the ornamentation of the sampler was confined to floral and geometrical motives, and whilst the latter were for the most part used in drawn-work samplers, the former constituted the stock whence the greater part of the decoration employed in the older examples was derived. Amongst the floral and vegetable kingdom the selection was a wide one, but a few favourites came in for recognition in almost every sampler, partly because of their decorative qualities, and partly from their being national badges. With few exceptions they were those which were to be met with in English seventeenth-century gardens, and undoubtedly, in some instances, may have been adapted by the makers from living specimens. Chief among the flowers was the rose, white and red, single and double, the emblem for centuries previously of two great parties in the State, a badge of the Tudor kings, a part of the insignia of the realm, and occupying a foremost place upon its coinage. In sampler ornamentation it is seldom used either in profile or in bud, but generally full face, and more often as a single than as a double flower. As a form of decoration it may have
  • 75.
    been derived fromforeign sources, but it clearly owed its popularity to the national significance that attached to it. The decorative value of the pink or carnation has been recognised from the earliest times, and a piece of Persian ornament is hardly complete without it. It is not surprising, therefore, that the old sampler workers utilised it to the full, and in fact it appears oftener than the rose in seventeenth-century specimens. Ten of the thirteen exhibits of that century at The Fine Art Society’s Exhibition in 1900 contained it as against seven where the rose was figured. It maintains this position throughout, and the most successful of the borders of bordered samplers are those where it is utilised. Specimens will be found in Plates III., IV., and VI. The decorative value of the honeysuckle was hardly appreciated, and it only appeared on samplers of the date of 1648 (Plate III.), 1662 (Plate V.), 1668, 1701, and 1711, in the Exhibition, and the undated one reproduced in Fig. 4.
  • 76.
    Larger Image Plate X.—Samplerby Catherine Tweedall. Dated 1775. Mrs Arthur Severn. The Sampler is noteworthy not only on account of its harmonious colour scheme, its symmetry of parts, and the excellence of its needlework, but as having been wrought by a young lady who afterwards became Mrs Ruskin, and the grandmother of John Ruskin. Her name, Cathrine Tweedall, is worked in the lower circle, and is illegible in the otherwise admirable reproduction, owing
  • 77.
    to its beingin a faded shade of the fairest pink. The verse was probably often read by her renowned grandson, and may perchance have spurred his determination to strive in the race in which he won so “high a reward.” Mrs Arthur Severn, to whom the Sampler belongs, notes that the Jean Ross whose name also appears upon it was the sister of the great Arctic explorer. The date of the Sampler is 1775. Sampler workers were very faithful to the strawberry, which, after appearing in almost every one of the seventeenth-century long samplers, was a favourite object for the later borders, and it may be seen almost unaltered in specimens separated in date by a century at least. We give in Fig. 31 a very usual version of it. (See also Plate XIII.)
  • 78.
    Larger Image Fig. 19.—Samplerby Mary Anderson. 1831. Lady Sherborne. Other fruits and flowers which now and again find a place are the fig, which will be seen in Plate III.; the pineapple, the thistle (Fig. 21), and the tulip in samplers dated 1662, 1694, 1760, and 1825 (Plate XIII.). Although the oak tree acquired political significance after the flight of Charles II., that fact can in no way account for such prominence being attached to its fruit and its foliage as, for instance, is the case in samplers dated 1644 and 1648 (Plate III.), where varieties of
  • 79.
    these are utilisedin a most decorative fashion in several of the rows of ornament, or in another of the following years (Fig. 16). But, curiously enough, after appearing in almost every seventeenth- century sampler, it disappeared entirely at the commencement of the eighteenth century. Sampler Design: Crowns, Coronets, Etc. The crown seems to have been suddenly seized upon by sampler makers as a form of decoration, and for half a century it was used with a tiresome reiteration. It had, of course, been largely used in Tudor decoration, and on the restoration of the monarchy it would be given prominence. But it probably was also in vogue because it lent itself to filling up spaces caused by alphabets not completing a line, and also because it allowed of variation through the coronets used by different ranks of nobility. We have seen in the sampler, Fig. 20, that the coronet of each order was used with a letter beneath, indicating duke, earl, etc. On occasions crowns were also used with some effect as a border. It is possible that the fashion for coronets was derived from foreign samplers, where this form of decoration was frequently used about the end of the seventeenth century, doubtless owing to the abundance of ennobled personages; they may well have come over with many other fancies which followed in the train of the House of Hanover. The earliest sampler in the Exhibition before referred to which bore a crown was one of 1693; but the coronet was there placed in conjunction with the initials M. D., and might be that of a titled lady who worked it. After that it appeared in one dated 1705 (where it was clearly a royal one connected with “Her Majesti Queen Anne”), and in samplers dated 1718, 1726, 1728 (1740, in which there were at least fifty varieties), and so on almost yearly up to 1767, after which it gradually disappeared, two only out of seventy subsequent samplers containing it. These were dated 1798 and 1804. In countries where
  • 80.
    almost every familybore a rank which warranted the use of a coronet, there would be a reason for their appearance as part of what would have to be embroidered on table linen, etc. Larger Image Fig. 20.—Sampler. Scottish (?). 18th Century. Formerly in the Author’s Collection. Note.—The bright colouring, coarse canvas, and ornate lettering of this piece suggest a Scottish origin. It dates from about 1730, and is one of the earliest of the bordered samplers, the border being
  • 81.
    at present analtogether insignificant addition. It is also one of the first specimens of decoration with crowns and coronets, the initials underneath standing for king, duke, marquis, earl, viscount, lord, count, and baron. Fig. 21.—Sampler by J. H. [Jane Heath]. a.d. 1725. Mr Ashby Sterry.
  • 82.
    Welcome to ourwebsite – the perfect destination for book lovers and knowledge seekers. We believe that every book holds a new world, offering opportunities for learning, discovery, and personal growth. That’s why we are dedicated to bringing you a diverse collection of books, ranging from classic literature and specialized publications to self-development guides and children's books. More than just a book-buying platform, we strive to be a bridge connecting you with timeless cultural and intellectual values. With an elegant, user-friendly interface and a smart search system, you can quickly find the books that best suit your interests. Additionally, our special promotions and home delivery services help you save time and fully enjoy the joy of reading. Join us on a journey of knowledge exploration, passion nurturing, and personal growth every day! ebookbell.com