Cover image for Windows PowerShell Cookbook, 3rd Edition

Book description

This introduction to the PowerShell language and scripting environment provides more than 400 task-oriented recipes to help you solve the most complex and pressing problems, and includes more than 100 tried-and-tested scripts that intermediate to advanced system administrators can copy and use immediately. Updated for PowerShell on Windows 8 and Windows Server 2012.

Table of Contents

  1. Windows PowerShell Cookbook
  2. Foreword
  3. Preface
    1. Who This Book Is For
    2. How This Book Is Organized
      1. Part I
      2. Part II
      3. Part III
      4. Part IV
      5. Part V
    3. What You Need to Use This Book
    4. Conventions Used in This Book
    5. Code Examples
      1. Obtaining Code Examples
      2. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  4. I. Tour
    1. A Guided Tour of Windows PowerShell
      1. Introduction
      2. An Interactive Shell
      3. Structured Commands (Cmdlets)
      4. Deep Integration of Objects
      5. Administrators as First-Class Users
      6. Composable Commands
      7. Techniques to Protect You from Yourself
      8. Common Discovery Commands
      9. Ubiquitous Scripting
      10. Ad Hoc Development
      11. Bridging Technologies
      12. Namespace Navigation Through Providers
      13. Much, Much More
  5. II. Fundamentals
    1. 1. The Windows PowerShell Interactive Shell
      1. Introduction
      2. Run Programs, Scripts, and Existing Tools
        1. Problem
        2. Solution
        3. Discussion
          1. Specifying the command name
          2. Specifying command arguments
        4. See Also
      3. Run a PowerShell Command
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Resolve Errors Calling Native Executables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Supply Default Values for Parameters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Invoke a Long-Running or Background Command
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Program: Monitor a Command for Changes
        1. See Also
      8. Notify Yourself of Job Completion
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Customize Your Shell, Profile, and Prompt
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Customize PowerShell’s User Input Behavior
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Customize PowerShell’s Command Resolution Behavior
        1. Problem
        2. Solution
        3. Discussion
      12. Find a Command to Accomplish a Task
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Get Help on a Command
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Update System Help Content
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Program: Search Help for Text
        1. See Also
      16. Launch PowerShell at a Specific Location
        1. Problem
        2. Solution
        3. Discussion
      17. Invoke a PowerShell Command or Script from Outside PowerShell
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Understand and Customize PowerShell’s Tab Completion
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Program: Learn Aliases for Common Commands
        1. See Also
      20. Program: Learn Aliases for Common Parameters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Access and Manage Your Console History
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Program: Create Scripts from Your Session History
        1. See Also
      23. Invoke a Command from Your Session History
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. Program: Search Formatted Output for a Pattern
        1. See Also
      25. Interactively View and Process Command Output
        1. Problem
        2. Solution
        3. Discussion
          1. Processing output
        4. See Also
      26. Program: Interactively View and Explore Objects
        1. See Also
      27. Store the Output of a Command into a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      28. Add Information to the End of a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      29. Record a Transcript of Your Shell Session
        1. Problem
        2. Solution
        3. Discussion
      30. Extend Your Shell with Additional Commands
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      31. Use Commands from Customized Shells
        1. Problem
        2. Solution
        3. Discussion
          1. Detecting loaded snapins
          2. Detecting loaded modules
        4. See Also
      32. Save State Between Sessions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    2. 2. Pipelines
      1. Introduction
      2. Filter Items in a List or Command Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Group and Pivot Data by Name
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Program: Simplify Most Where-Object Filters
        1. See Also
      5. Program: Interactively Filter Lists of Objects
        1. See Also
      6. Work with Each Item in a List or Command Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Automate Data-Intensive Tasks
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Program: Simplify Most Foreach-Object Pipelines
        1. See Also
      9. Intercept Stages of the Pipeline
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Automatically Capture Pipeline Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Capture and Redirect Binary Process Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    3. 3. Variables and Objects
      1. Introduction
      2. Display the Properties of an Item as a List
        1. Problem
        2. Solution
        3. Discussion
      3. Display the Properties of an Item as a Table
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Store Information in Variables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Access Environment Variables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Program: Retain Changes to Environment Variables Set by a Batch File
        1. See Also
      7. Control Access and Scope of Variables and Other Items
        1. Problem
        2. Solution
        3. Discussion
          1. Variables
          2. Functions
          3. Aliases and drives
        4. See Also
      8. Program: Create a Dynamic Variable
      9. Work with .NET Objects
        1. Problem
        2. Solution
        3. Discussion
          1. Static methods
          2. Instance methods
          3. Static properties
          4. Instance properties
        4. See Also
      10. Create an Instance of a .NET Object
        1. Problem
        2. Solution
        3. Discussion
          1. Load types from another assembly
        4. See Also
      11. Create Instances of Generic Objects
      12. Reduce Typing for Long Class Names
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Use a COM Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Learn About Types and Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Get Detailed Documentation About Types and Objects
        1. Problem
        2. Solution
        3. Discussion
          1. Public constructors
          2. Public fields/public properties
          3. Public methods
        4. See Also
      16. Add Custom Methods and Properties to Objects
        1. Problem
        2. Solution
        3. Discussion
          1. Calculated properties
        4. See Also
      17. Create and Initialize Custom Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Add Custom Methods and Properties to Types
        1. Problem
        2. Solution
        3. Discussion
          1. Getting started
          2. Add a ScriptProperty
          3. Add an AliasProperty
          4. Add a ScriptMethod
          5. Add other extension points
      19. Define Custom Formatting for a Type
        1. Problem
        2. Solution
        3. Discussion
    4. 4. Looping and Flow Control
      1. Introduction
      2. Make Decisions with Comparison and Logical Operators
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Adjust Script Flow Using Conditional Statements
        1. Problem
        2. Solution
        3. Discussion
      4. Manage Large Conditional Statements with Switches
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Repeat Operations with Loops
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Add a Pause or Delay
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    5. 5. Strings and Unstructured Text
      1. Introduction
      2. Create a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Create a Multiline or Formatted String
        1. Problem
        2. Solution
        3. Discussion
      4. Place Special Characters in a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Insert Dynamic Information in a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Prevent a String from Including Dynamic Information
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Place Formatted Information in a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Search a String for Text or a Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Replace Text in a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Split a String on Text or a Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Combine Strings into a Larger String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Convert a String to Uppercase or Lowercase
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Trim a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Format a Date for Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Program: Convert Text Streams to Objects
        1. See Also
      16. Generate Large Reports and Text Streams
        1. Problem
        2. Solution
        3. Discussion
          1. Creating large text reports
      17. Generate Source Code and Other Repetitive Text
        1. Problem
        2. Solution
        3. Discussion
    6. 6. Calculations and Math
      1. Introduction
      2. Perform Simple Arithmetic
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Perform Complex Arithmetic
        1. Problem
        2. Solution
        3. Discussion
          1. Working with any root
          2. Working with degrees instead of radians
          3. Working with large numbers
          4. Working with imaginary and complex numbers
        4. See Also
      4. Measure Statistical Properties of a List
        1. Problem
        2. Solution
        3. Discussion
      5. Work with Numbers as Binary
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Simplify Math with Administrative Constants
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Convert Numbers Between Bases
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    7. 7. Lists, Arrays, and Hashtables
      1. Introduction
      2. Create an Array or List of Items
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Create a Jagged or Multidimensional Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Access Elements of an Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Visit Each Element of an Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Sort an Array or List of Items
        1. Problem
        2. Solution
        3. Discussion
      7. Determine Whether an Array Contains an Item
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Combine Two Arrays
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Find Items in an Array That Match a Value
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Compare Two Lists
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Remove Elements from an Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Find Items in an Array Greater or Less Than a Value
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Use the ArrayList Class for Advanced Array Tasks
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Create a Hashtable or Associative Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Sort a Hashtable by Key or Value
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    8. 8. Utility Tasks
      1. Introduction
      2. Get the System Date and Time
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Measure the Duration of a Command
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Read and Write from the Windows Clipboard
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Generate a Random Number or Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Program: Search the Windows Start Menu
        1. See Also
      7. Program: Show Colorized Script Content
        1. Discussion
        2. See Also
  6. III. Common Tasks
    1. 9. Simple Files
      1. Introduction
      2. Get the Content of a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Search a File for Text or a Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Parse and Manage Text-Based Logfiles
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Parse and Manage Binary Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Create a Temporary File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Search and Replace Text in a File
        1. Problem
        2. Solution
        3. Discussion
          1. Work with files encoded in Unicode or another (OEM) code page
          2. Replace text using a pattern instead of plain text
          3. Replace text that spans multiple lines
          4. Replace text in large files
        4. See Also
      8. Program: Get the Encoding of a File
        1. See Also
      9. Program: View the Hexadecimal Representation of Content
        1. See Also
    2. 10. Structured Files
      1. Introduction
      2. Access Information in an XML File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Perform an XPath Query Against XML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Convert Objects to XML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Modify Data in an XML File
        1. Problem
        2. Solution
        3. Discussion
      6. Easily Import and Export Your Structured Data
        1. Problem
        2. Solution
        3. Discussion
      7. Store the Output of a Command in a CSV or Delimited File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Import CSV and Delimited Data from a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Manage JSON Data Streams
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Use Excel to Manage Command Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Parse and Interpret PowerShell Scripts
        1. Problem
        2. Solution
        3. Discussion
          1. Tokenization
          2. AST generation
        4. See Also
    3. 11. Code Reuse
      1. Introduction
      2. Write a Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Write a Function
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Find a Verb Appropriate for a Command Name
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Write a Script Block
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Return Data from a Script, Function, or Script Block
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Package Common Commands in a Module
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Write Commands That Maintain State
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Selectively Export Commands from a Module
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Diagnose and Interact with Internal Module State
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Handle Cleanup Tasks When a Module Is Removed
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Access Arguments of a Script, Function, or Script Block
        1. Problem
        2. Solution
        3. Discussion
          1. Supporting PowerShell’s common parameters
          2. Using the $args array
        4. See Also
      13. Add Validation to Parameters
        1. Problem
        2. Solution
        3. Discussion
          1. Defining parameter behavior
          2. Defining parameter validation
        4. See Also
      14. Accept Script Block Parameters with Local Variables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Dynamically Compose Command Parameters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Provide -WhatIf, -Confirm, and Other Cmdlet Features
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Add Help to Scripts or Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Add Custom Tags to a Function or Script Block
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Access Pipeline Input
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Write Pipeline-Oriented Scripts with Cmdlet Keywords
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Write a Pipeline-Oriented Function
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Organize Scripts for Improved Readability
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. Invoke Dynamically Named Commands
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. Program: Enhance or Extend an Existing Cmdlet
        1. See Also
    4. 12. Internet-Enabled Scripts
      1. Introduction
      2. Download a File from an FTP or Internet Site
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Upload a File to an FTP Site
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Download a Web Page from the Internet
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Parse and Analyze a Web Page from the Internet
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Script a Web Application Session
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Program: Get-PageUrls
        1. See Also
      8. Interact with REST-Based Web APIs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Connect to a Web Service
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Export Command Output as a Web Page
        1. Problem
        2. Solution
        3. Discussion
      11. Send an Email
        1. Problem
        2. Solution
        3. Discussion
      12. Program: Monitor Website Uptimes
        1. See Also
      13. Program: Interact with Internet Protocols
        1. See Also
    5. 13. User Interaction
      1. Introduction
      2. Read a Line of User Input
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Read a Key of User Input
        1. Problem
        2. Solution
        3. Discussion
      4. Program: Display a Menu to the User
        1. See Also
      5. Display Messages and Output to the User
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Provide Progress Updates on Long-Running Tasks
        1. Problem
        2. Solution
        3. Discussion
      7. Write Culture-Aware Scripts
        1. Problem
        2. Solution
        3. Discussion
          1. Date, time, and number formats
          2. Complexity of user input and file content
          3. Capitalization rules
          4. Sorting rules
          5. Other guidelines
        4. See Also
      8. Support Other Languages in Script Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Program: Invoke a Script Block with Alternate Culture Settings
        1. See Also
      10. Access Features of the Host’s User Interface
        1. Problem
        2. Solution
        3. Discussion
      11. Program: Add a Graphical User Interface to Your Script
        1. See Also
      12. Interact with MTA Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    6. 14. Debugging
      1. Introduction
      2. Prevent Common Scripting Errors
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Trace Script Execution
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Set a Script Breakpoint
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Debug a Script When It Encounters an Error
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Create a Conditional Breakpoint
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Investigate System State While Debugging
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Program: Watch an Expression for Changes
        1. See Also
      9. Program: Get Script Code Coverage
        1. See Also
    7. 15. Tracing and Error Management
      1. Introduction
      2. Determine the Status of the Last Command
        1. Problem
        2. Solution
        3. Discussion
      3. View the Errors Generated by a Command
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Manage the Error Output of Commands
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Program: Resolve an Error
        1. See Also
      6. Configure Debug, Verbose, and Progress Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Handle Warnings, Errors, and Terminating Errors
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Output Warnings, Errors, and Terminating Errors
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Program: Analyze a Script’s Performance Profile
        1. See Also
    8. 16. Environmental Awareness
      1. Introduction
      2. View and Modify Environment Variables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Modify the User or System Path
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Access Information About Your Command’s Invocation
        1. Problem
        2. Solution
        3. Discussion
          1. Scripts
          2. Functions
          3. Script blocks
      5. Program: Investigate the InvocationInfo Variable
        1. See Also
      6. Find Your Script’s Name
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Find Your Script’s Location
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Find the Location of Common System Paths
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Get the Current Location
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Safely Build File Paths Out of Their Components
        1. Problem
        2. Solution
        3. Discussion
      11. Interact with PowerShell’s Global Environment
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Determine PowerShell Version Information
        1. Problem
        2. Solution
        3. Discussion
      13. Test for Administrative Privileges
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    9. 17. Extend the Reach of Windows PowerShell
      1. Introduction
      2. Automate Programs Using COM Scripting Interfaces
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Program: Query a SQL Data Source
        1. See Also
      4. Access Windows Performance Counters
        1. Problem
        2. Solution
        3. Discussion
      5. Access Windows API Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Program: Invoke Simple Windows API Calls
        1. See Also
      7. Define or Extend a .NET Class
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Add Inline C# to Your PowerShell Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Access a .NET SDK Library
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Create Your Own PowerShell Cmdlet
        1. Problem
        2. Solution
        3. Discussion
          1. Step 1: Download the PowerShell SDK
          2. Step 2: Create a file to hold the cmdlet source code
          3. Step 3: Compile the DLL
          4. Step 4: Load the module
          5. Step 5: Use the module
        4. See Also
      11. Add PowerShell Scripting to Your Own Program
        1. Problem
        2. Solution
        3. Discussion
          1. Step 1: Download the PowerShell SDK
          2. Step 2: Create a file to hold the hosting source code
          3. Step 3: Compile and run the example
        4. See Also
    10. 18. Security and Script Signing
      1. Introduction
      2. Enable Scripting Through an Execution Policy
        1. Problem
        2. Solution
        3. Discussion
          1. Execution policies are not user restrictions
        4. See Also
      3. Disable Warnings for UNC Paths
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Sign a PowerShell Script, Module, or Formatting File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Program: Create a Self-Signed Certificate
        1. Discussion
        2. See Also
      6. Manage PowerShell Security in an Enterprise
        1. Problem
        2. Solution
        3. Discussion
          1. Apply PowerShell’s Group Policy templates
          2. Deploy Microsoft Certificate Services
        4. See Also
      7. Block Scripts by Publisher, Path, or Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Verify the Digital Signature of a PowerShell Script
        1. Problem
        2. Solution
        3. Discussion
      9. Securely Handle Sensitive Information
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Securely Request Usernames and Passwords
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Program: Start a Process as Another User
        1. See Also
      12. Program: Run a Temporarily Elevated Command
        1. See Also
      13. Securely Store Credentials on Disk
        1. Problem
        2. Solution
          1. Save the credential’s password to disk
          2. Recreate the credential from the password stored on disk
        3. Discussion
        4. See Also
      14. Access User and Machine Certificates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Program: Search the Certificate Store
        1. See Also
      16. Add and Remove Certificates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Manage Security Descriptors in SDDL Form
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    11. 19. Integrated Scripting Environment
      1. Introduction
      2. Debug a Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Customize Text and User Interface Colors
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Connect to a Remote Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Extend ISE Functionality Through Its Object Model
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Quickly Insert Script Snippets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Add an Item to the Tools Menu
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
  7. IV. Administrator Tasks
    1. 20. Files and Directories
      1. Introduction
      2. Determine the Current Location
        1. Problem
        2. Solution
        3. Discussion
      3. Get the Files in a Directory
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Find All Files Modified Before a Certain Date
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Clear the Content of a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Manage and Change the Attributes of a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Find Files That Match a Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Manage Files That Include Special Characters
        1. Problem
        2. Solution
        3. Discussion
      9. Program: Get Disk Usage Information
        1. See Also
      10. Monitor a File for Changes
        1. Problem
        2. Solution
        3. Discussion
      11. Get the Version of a DLL or Executable
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Program: Get the MD5 or SHA1 Hash of a File
        1. See Also
      13. Create a Directory
        1. Problem
        2. Solution
        3. Discussion
      14. Remove a File or Directory
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Rename a File or Directory
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Move a File or Directory
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Create and Map PowerShell Drives
        1. Problem
        2. Solution
        3. Discussion
      18. Access Long File and Directory Names
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Unblock a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Interact with Alternate Data Streams
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Program: Move or Remove a Locked File
        1. See Also
      22. Get the ACL of a File or Directory
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. Set the ACL of a File or Directory
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. Program: Add Extended File Properties to Files
        1. See Also
      25. Program: Create a Filesystem Hard Link
        1. See Also
      26. Program: Create a ZIP Archive
        1. See Also
    2. 21. The Windows Registry
      1. Introduction
      2. Navigate the Registry
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. View a Registry Key
        1. Problem
        2. Solution
        3. Discussion
      4. Modify or Remove a Registry Key Value
        1. Problem
        2. Solution
        3. Discussion
      5. Create a Registry Key Value
        1. Problem
        2. Solution
        3. Discussion
      6. Remove a Registry Key
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Safely Combine Related Registry Modifications
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Add a Site to an Internet Explorer Security Zone
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Modify Internet Explorer Settings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Program: Search the Windows Registry
        1. See Also
      11. Get the ACL of a Registry Key
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Set the ACL of a Registry Key
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Work with the Registry of a Remote Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Program: Get Registry Items from Remote Machines
        1. See Also
      15. Program: Get Properties of Remote Registry Keys
        1. See Also
      16. Program: Set Properties of Remote Registry Keys
        1. See Also
      17. Discover Registry Settings for Programs
        1. Problem
        2. Solution
        3. Discussion
          1. Launch and configure Process Monitor
          2. Prepare to manually set the configuration option
          3. Tell Process Monitor to begin capturing information
          4. Manually set the configuration option
          5. Tell Process Monitor to stop capturing information
          6. Review the capture logs for registry modification
          7. Automate these registry writes
        4. See Also
    3. 22. Comparing Data
      1. Introduction
      2. Compare the Output of Two Commands
        1. Problem
        2. Solution
        3. Discussion
      3. Determine the Differences Between Two Files
        1. Problem
        2. Solution
        3. Discussion
      4. Verify Integrity of File Sets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    4. 23. Event Logs
      1. Introduction
      2. List All Event Logs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Get the Newest Entries from an Event Log
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Find Event Log Entries with Specific Text
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Retrieve and Filter Event Log Entries
        1. Problem
        2. Solution
        3. Discussion
          1. Efficiently processing simple queries
          2. Automating GUI-generated searches
          3. Performing complex event analysis and correlation
        4. See Also
      6. Find Event Log Entries by Their Frequency
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Back Up an Event Log
        1. Problem
        2. Solution
        3. Discussion
      8. Create or Remove an Event Log
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Write to an Event Log
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Run a PowerShell Script for Windows Event Log Entries
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Clear or Maintain an Event Log
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Access Event Logs of a Remote Machine
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    5. 24. Processes
      1. Introduction
      2. List Currently Running Processes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Launch the Application Associated with a Document
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Launch a Process
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Stop a Process
        1. Problem
        2. Solution
        3. Discussion
      6. Get the Owner of a Process
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Get the Parent Process of a Process
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Debug a Process
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    6. 25. System Services
      1. Introduction
      2. List All Running Services
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Manage a Running Service
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Configure a Service
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    7. 26. Active Directory
      1. Introduction
      2. Test Active Directory Scripts on a Local Installation
        1. Problem
        2. Solution
        3. Discussion
          1. Verify prerequisites
          2. Install ADAM
          3. Create a test instance
        4. See Also
      3. Create an Organizational Unit
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Get the Properties of an Organizational Unit
        1. Problem
        2. Solution
        3. Discussion
      5. Modify Properties of an Organizational Unit
        1. Problem
        2. Solution
        3. Discussion
      6. Delete an Organizational Unit
        1. Problem
        2. Solution
        3. Discussion
      7. Get the Children of an Active Directory Container
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Create a User Account
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Program: Import Users in Bulk to Active Directory
        1. See Also
      10. Search for a User Account
        1. Problem
        2. Solution
        3. Discussion
      11. Get and List the Properties of a User Account
        1. Problem
        2. Solution
        3. Discussion
      12. Modify Properties of a User Account
        1. Problem
        2. Solution
        3. Discussion
      13. Change a User Password
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Create a Security or Distribution Group
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Search for a Security or Distribution Group
        1. Problem
        2. Solution
        3. Discussion
      16. Get the Properties of a Group
        1. Problem
        2. Solution
        3. Discussion
      17. Find the Owner of a Group
        1. Problem
        2. Solution
        3. Discussion
      18. Modify Properties of a Security or Distribution Group
        1. Problem
        2. Solution
        3. Discussion
      19. Add a User to a Security or Distribution Group
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Remove a User from a Security or Distribution Group
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. List a User’s Group Membership
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. List the Members of a Group
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. List the Users in an Organizational Unit
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. Search for a Computer Account
        1. Problem
        2. Solution
        3. Discussion
      25. Get and List the Properties of a Computer Account
        1. Problem
        2. Solution
        3. Discussion
    8. 27. Enterprise Computer Management
      1. Introduction
      2. Join a Computer to a Domain or Workgroup
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Remove a Computer from a Domain
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Rename a Computer
        1. Problem
        2. Solution
        3. Discussion
      5. Program: List Logon or Logoff Scripts for a User
        1. See Also
      6. Program: List Startup or Shutdown Scripts for a Machine
        1. See Also
      7. Deploy PowerShell-Based Logon Scripts
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Enable or Disable the Windows Firewall
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Open or Close Ports in the Windows Firewall
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Program: List All Installed Software
        1. See Also
      11. Uninstall an Application
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Manage Computer Restore Points
        1. Problem
        2. Solution
        3. Discussion
      13. Reboot or Shut Down a Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Determine Whether a Hotfix Is Installed
        1. Problem
        2. Solution
        3. Discussion
      15. Manage Scheduled Tasks on a Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Retrieve Printer Information
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Retrieve Printer Queue Statistics
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Manage Printers and Print Queues
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Program: Summarize System Information
        1. See Also
      20. Renew a DHCP Lease
        1. Problem
        2. Solution
        3. Discussion
          1. Use the Win32_NetworkAdapterConfiguration WMI class
          2. Run ipconfig on the remote computer
        4. See Also
      21. Assign a Static IP Address
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. List All IP Addresses for a Computer
        1. Problem
        2. Solution
        3. Discussion
          1. Use the Win32_NetworkAdapterConfiguration WMI class
          2. Run ipconfig on the remote computer
        4. See Also
      23. List Network Adapter Properties
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    9. 28. Windows Management Instrumentation
      1. Introduction
        1. The Shift to CIM
      2. Access Windows Management Instrumentation and CIM Data
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Modify the Properties of a WMI or CIM Instance
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Invoke a Method on a WMI Instance or Class
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Program: Determine Properties Available to WMI and CIM Filters
        1. See Also
      6. Program: Search for WMI Classes
        1. See Also
      7. Use .NET to Perform Advanced WMI Tasks
        1. Problem
        2. Solution
          1. Advanced instance features
          2. Advanced class features
          3. Advanced query feature
        3. Discussion
        4. See Also
      8. Improve the Performance of Large-Scale WMI Operations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Convert a VBScript WMI Script to PowerShell
        1. Problem
        2. Solution
        3. Discussion
          1. Retrieving data
          2. Calling methods on an instance
          3. Calling methods on a class
        4. See Also
    10. 29. Remoting
      1. Introduction
      2. Find Commands That Support Their Own Remoting
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Enable PowerShell Remoting on a Computer
        1. Problem
        2. Solution
        3. Discussion
          1. Enable remoting on a single local machine
          2. Enable remoting on a remote machine
          3. Enable remoting in an enterprise
        4. See Also
      4. Interactively Manage a Remote Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Invoke a Command on a Remote Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Disconnect and Reconnect PowerShell Sessions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Program: Remotely Enable PowerShell Remoting
        1. See Also
      8. Program: Invoke a PowerShell Expression on a Remote Machine
        1. See Also
      9. Test Connectivity Between Two Computers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Limit Networking Scripts to Hosts That Respond
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Enable Remote Desktop on a Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Configure User Permissions for Remoting
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Enable Remoting to Workgroup Computers
        1. Problem
        2. Solution
        3. Discussion
      14. Implicitly Invoke Commands from a Remote Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Create Sessions with Full Network Access
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Pass Variables to Remote Sessions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Configure Advanced Remoting Quotas and Options
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Invoke a Command on Many Computers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Run a Local Script on a Remote Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Program: Transfer a File to a Remote Computer
        1. See Also
      21. Determine Whether a Script Is Running on a Remote Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Create a Task-Specific Remoting Endpoint
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    11. 30. Workflows
      1. Introduction
      2. Write a Workflow
        1. Problem
        2. Solution
        3. Discussion
          1. Writing a basic workflow
          2. Major conceptual differences between scripts and workflows
          3. Major syntactic differences between scripts and workflows
        4. See Also
      3. Run a Workflow
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Suspend and Resume a Workflow
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Invoke Islands of Traditional PowerShell Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Invoke Workflow Actions in Parallel
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Customize an Activity’s Connection Parameters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Write a Workflow That Requires Human Intervention
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Add Raw XAML to a Workflow
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Reference Custom Activities in a Workflow
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Debug or Troubleshoot a Workflow
        1. Problem
        2. Solution
        3. Discussion
          1. Authoring-time debugging
          2. Runtime debugging
        4. See Also
      12. Use PowerShell Activities from a Traditional Windows Workflow Application
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    12. 31. Transactions
      1. Introduction
      2. Safely Experiment with Transactions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Change Error Recovery Behavior in Transactions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    13. 32. Event Handling
      1. Introduction
      2. Respond to Automatically Generated Events
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Create and Respond to Custom Events
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Create a Temporary Event Subscription
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Forward Events from a Remote Computer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Investigate Internal Event Action State
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Use a Script Block as a .NET Delegate or Event Handler
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
  8. V. References
    1. A. PowerShell Language and Environment
      1. Commands and Expressions
      2. Comments
      3. Help Comments
      4. Variables
      5. Booleans
      6. Strings
        1. Literal and Expanding Strings
        2. Here Strings
        3. Escape Sequences
      7. Numbers
        1. Simple Assignment
        2. Administrative Numeric Constants
        3. Hexadecimal and Other Number Bases
        4. Large Numbers
        5. Imaginary and Complex Numbers
      8. Arrays and Lists
        1. Array Definitions
        2. Array Access
        3. Array Slicing
      9. Hashtables (Associative Arrays)
        1. Hashtable Definitions
        2. Hashtable Access
      10. XML
      11. Simple Operators
        1. Arithmetic Operators
        2. Logical Operators
        3. Binary Operators
        4. Other Operators
      12. Comparison Operators
      13. Conditional Statements
        1. if, elseif, and else Statements
        2. switch Statements
      14. Looping Statements
        1. for Statement
        2. foreach Statement
        3. while Statement
        4. do … while Statement/do … until Statement
        5. Flow Control Statements
          1. break
          2. continue
        6. Workflow-Specific Statements
          1. InlineScript
          2. Parallel/Sequence
          3. Foreach -Parallel
      15. Working with the .NET Framework
        1. Static Methods
        2. Instance Methods
        3. Explicitly Implemented Interface Methods
        4. Static Properties
        5. Instance Properties
        6. Learning About Types
          1. The Get-Member cmdlet
          2. .NET Framework documentation
        7. Type Shortcuts
        8. Creating Instances of Types
        9. Interacting with COM Objects
        10. Extending Types
          1. The Add-Member cmdlet
          2. Custom type extension files
      16. Writing Scripts, Reusing Functionality
        1. Writing Commands
          1. Writing scripts
          2. Writing functions
          3. Writing script blocks
        2. Running Commands
          1. Invoking
          2. Dot-sourcing
          3. Parameters
        3. Providing Input to Commands
          1. Argument array
          2. Formal parameters
          3. Command behavior customizations
          4. Parameter attribute customizations
          5. Parameter validation attributes
          6. Pipeline input
          7. Cmdlet keywords in commands
          8. $MyInvocation automatic variable
        4. Retrieving Output from Commands
          1. Pipeline output
          2. Return statement
          3. Exit statement
        5. Help Documentation
      17. Managing Errors
        1. Nonterminating Errors
        2. Terminating Errors
      18. Formatting Output
        1. Custom Formatting Files
      19. Capturing Output
      20. Common Customization Points
        1. Console Settings
          1. Adjust your window size
          2. Make text selection easier
          3. Use hotkeys to operate the shell more efficiently
        2. Profiles
        3. Prompts
        4. Tab Completion
        5. User Input
        6. Command Resolution
    2. B. Regular Expression Reference
    3. C. XPath Quick Reference
    4. D. .NET String Formatting
      1. String Formatting Syntax
      2. Standard Numeric Format Strings
      3. Custom Numeric Format Strings
    5. E. .NET DateTime Formatting
      1. Custom DateTime Format Strings
    6. F. Selected .NET Classes and Their Uses
    7. G. WMI Reference
    8. H. Selected COM Objects and Their Uses
    9. I. Selected Events and Their Uses
    10. J. Standard PowerShell Verbs
  9. Index
  10. About the Author
  11. Colophon
  12. Copyright