Cover image for Windows PowerShell Cookbook

Book description

This Cookbook by Windows PowerShell team developer Lee Holmes provides hundreds of tested scripts that you can use right away to get Microsoft's new tool working for you. More than 150 recipes, combined with a concise task-based introduction to the Windows PowerShell scripting language and environment, make it the perfect look-up guide when you encounter a thorny problem, or need a quick solution. The ideal companion to any tutorial or reference, this book meets the needs of system administrators at any level. Microsoft has revolutionized the world of system management and command-line shells with its release of Windows PowerShell, and Lee Holmes gives you practical tools and inside advice that will make you a more productive user and administrator. You will be able to solve everything from automating routine tasks, working with files, event logs and other forms of structured data, to managing the users and resources of complex Windows networks. Each recipe includes a focused piece of code plus discussion of how and why it works, so that you can apply the solution to similar tasks. You get an array of recipes covering PowerShell fundamentals, common tasks, and administrator tasks, including:

  • Pipelines, variables, objects, looping and flow control, strings and unstructured text, calculations and math

  • Simple files; structured files; Internet-enabled scripts; code reuse; lists, arrays and hashtables; user feedback; error management; environmental awareness; script signing; and more

  • Files and directories, registry manipulation, comparing data, event logs, process cmdlets, service cmdlets, Active Directory, enterprise computer management, and more

Those who administer Microsoft's Exchange 2007 and System Center Operations Manager (formerly MOM) will also benefit from this book, with separate chapters devoted to these servers. All Windows administrators will appreciate the appendices that include a PowerShell language quick reference, and a reference to the .NET, WMI and COM objects that PowerShell scripters will use often.

With working scripts, tutorials, and references all in one volume, Windows PowerShell Cookbook will turbocharge the productivity of any Windows administrator.

Table of Contents

  1. Windows PowerShell Cookbook
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Foreword
      1. Glue, Enablers, and a WSH
      2. That Lee Guy
    4. Preface
      1. Who This Book Is For
      2. How This Book Is Organized
        1. Part I: Tour
        2. Part II: Fundamentals
        3. Part III: Common Tasks
        4. Part IV: Administrator Tasks
        5. Part V: References
      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. Comments and Questions
      7. Acknowledgments
    5. 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
    6. II. Fundamentals
      1. 1. The Windows PowerShell Interactive Shell
        1. Introduction
        2. 1.1. Run Programs, Scripts, and Existing Tools
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 1.2. Run a PowerShell Command
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 1.3. Customize Your Shell, Profile, and Prompt
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 1.4. Find a Command to Accomplish a Task
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 1.5. Get Help on a Command
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 1.6. Program: Search Help for Text
          1. See Also
        8. 1.7. Invoke a PowerShell Script From Outside PowerShell
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        9. 1.8. Program: Retain Changes to Environment Variables Set by a Batch File
          1. See Also
        10. 1.9. Get the System Date and Time
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        11. 1.10. Determine the Status of the Last Command
          1. Problem
          2. Solution
          3. Discussion
        12. 1.11. Measure the Duration of a Command
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        13. 1.12. Customize the Shell to Improve Your Productivity
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        14. 1.13. Program: Learn Aliases for Common Commands
          1. See Also
        15. 1.14. Access and Manage Your Console History
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        16. 1.15. Store the Output of a Command into a File
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        17. 1.16. Add Information to the End of a File
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        18. 1.17. Record a Transcript of Your Shell Session
          1. Problem
          2. Solution
          3. Discussion
        19. 1.18. Display the Properties of an Item As a List
          1. Problem
          2. Solution
          3. Discussion
        20. 1.19. Display the Properties of an Item As a Table
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        21. 1.20. Manage the Error Output of Commands
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        22. 1.21. Configure Debug, Verbose, and Progress Output
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        23. 1.22. Extend Your Shell with Additional Snapins
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        24. 1.23. Use Console Files to Load and Save Sets of Snapins
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      2. 2. Pipelines
        1. 2.0. Introduction
        2. 2.1. Filter Items in a List or Command Output
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 2.2. Program: Simplify Most Where-Object Filters
          1. See Also
        4. 2.3. Program: Interactively Filter Lists of Objects
          1. See Also
        5. 2.4. Work with Each Item in a List or Command Output
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 2.5. Automate Data-Intensive Tasks
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      3. 3. Variables and Objects
        1. 3.0. Introduction
        2. 3.1. Store Information in Variables
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 3.2. Access Environment Variables
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 3.3. 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
        5. 3.4. 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
        6. 3.5. Create an Instance of a .NET Object
          1. Problem
          2. Solution
          3. Discussion
            1. Load types from another assembly
          4. See Also
        7. 3.6. Program: Create Instances of Generic Objects
        8. 3.7. Reduce Typing for Long Class Names
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        9. 3.8. Use a COM Object
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        10. 3.9. Learn About Types and Objects
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        11. 3.10. 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
        12. 3.11. Add Custom Methods and Properties to Objects
          1. Problem
          2. Solution
          3. Discussion
            1. Calculated properties
          4. See Also
        13. 3.12. 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
      4. 4. Looping and Flow Control
        1. 4.0. Introduction
        2. 4.1. Make Decisions with Comparison and Logical Operators
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 4.2. Adjust Script Flow Using Conditional Statements
          1. Problem
          2. Solution
          3. Discussion
        4. 4.3. Manage Large Conditional Statements with Switches
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 4.4. Repeat Operations with Loops
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 4.5. Add a Pause or Delay
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      5. 5. Strings and Unstructured Text
        1. 5.0. Introduction
        2. 5.1. Create a String
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 5.2. Create a Multiline or Formatted String
          1. Problem
          2. Solution
          3. Discussion
        4. 5.3. Place Special Characters in a String
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 5.4. Insert Dynamic Information in a String
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 5.5. Prevent a String from Including Dynamic Information
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 5.6. Place Formatted Information in a String
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        8. 5.7. Search a String for Text or a Pattern
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        9. 5.8. Replace Text in a String
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        10. 5.9. Convert a String to Upper/Lowercase
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        11. 5.10. Trim a String
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        12. 5.11. Format a Date for Output
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        13. 5.12. Program: Convert Text Streams to Objects
          1. See Also
        14. 5.13. Generate Large Reports and Text Streams
          1. Problem
          2. Solution
          3. Discussion
            1. Creating large text reports
      6. 6. Calculations and Math
        1. 6.0. Introduction
        2. 6.1. Perform Simple Arithmetic
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 6.2. Perform Complex Arithmetic
          1. Problem
          2. Solution
          3. Discussion
            1. Working with any root
            2. Working with degrees instead of radians
          4. See Also
        4. 6.3. Measure Statistical Properties of a List
          1. Problem
          2. Solution
          3. Discussion
        5. 6.4. Work with Numbers As Binary
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 6.5. Simplify Math with Administrative Constants
          1. Problem
          2. Solution
          3. Discussion
        7. 6.6. Convert Numbers Between Bases
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
    7. III. Common Tasks
      1. 7. Simple Files
        1. 7.0. Introduction
        2. 7.1. Get the Content of a File
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 7.2. Search a File for Text or a Pattern
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 7.3. Parse and Manage Text-Based Logfiles
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 7.4. Parse and Manage Binary Files
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 7.5. Create a Temporary File
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 7.6. 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
      2. 8. Structured Files
        1. 8.0. Introduction
        2. 8.1. Access Information in an XML File
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 8.2. Perform an XPath Query Against an XML File
          1. Problem
          2. Solution
          3. Discussion
        4. 8.3. Modify Data in an XML File
          1. Problem
          2. Solution
          3. Discussion
        5. 8.4. Easily Import and Export Your Structured Data
          1. Problem
          2. Solution
          3. Discussion
        6. 8.5. Store the Output of a Command in a CSV File
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 8.6. Import Structured Data from a CSV File
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        8. 8.7. Use Excel to Manage Command Output
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      3. 9. Internet-Enabled Scripts
        1. 9.0. Introduction
        2. 9.1. Download a File from the Internet
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 9.2. Download a Web Page from the Internet
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 9.3. Program: Get-PageUrls
          1. See Also
        5. 9.4. Program: Connect-WebService
          1. See Also
        6. 9.5. Export Command Output As a Web Page
          1. Problem
          2. Solution
          3. Discussion
        7. 9.6. Program: Send an Email
          1. See Also
        8. 9.7. Program: Interact with Internet Protocols
          1. See Also
      4. 10. Code Reuse
        1. 10.0. Introduction
        2. 10.1. Write a Script
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 10.2. Write a Function
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 10.3. Write a Script Block
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 10.4. Return Data from a Script, Function, or Script Block
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 10.5. Place Common Functions in a Library
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 10.6. Access Arguments of a Script, Function, or Script Block
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        8. 10.7. Access Pipeline Input
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        9. 10.8. Write Pipeline-Oriented Scripts with Cmdlet Keywords
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        10. 10.9. Write a Pipeline-Oriented Function
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      5. 11. Lists, Arrays, and Hashtables
        1. 11.0. Introduction
        2. 11.1. Create an Array or List of Items
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 11.2. Create a Jagged or Multidimensional Array
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 11.3. Access Elements of an Array
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 11.4. Visit Each Element of an Array
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 11.5. Sort an Array or List of Items
          1. Problem
          2. Solution
          3. Discussion
        7. 11.6. Determine Whether an Array Contains an Item
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        8. 11.7. Combine Two Arrays
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        9. 11.8. Find Items in an Array That Match a Value
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        10. 11.9. Remove Elements from an Array
          1. Problem
          2. Solution
          3. Discussion
        11. 11.10. Find Items in an Array Greater or Less Than a Value
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        12. 11.11. Use the ArrayList Class for Advanced Array Tasks
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        13. 11.12. Create a Hashtable or Associative Array
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        14. 11.13. Sort a Hashtable by Key or Value
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      6. 12. User Interaction
        1. 12.0. Introduction
        2. 12.1. Read a Line of User Input
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 12.2. Read a Key of User Input
          1. Problem
          2. Solution
          3. Discussion
        4. 12.3. Program: Display a Menu to the User
          1. See Also
        5. 12.4. Display Messages and Output to the User
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 12.5. Provide Progress Updates on Long-Running Tasks
          1. Problem
          2. Solution
          3. Discussion
        7. 12.6. 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. 12.7. Program: Invoke a Script Block with Alternate Culture Settings
          1. See Also
        9. 12.8. Access Features of the Host's User Interface
          1. Problem
          2. Solution
          3. Discussion
        10. 12.9. Program: Add a Graphical User Interface to Your Script
          1. See Also
      7. 13. Tracing and Error Management
        1. 13.0. Introduction
        2. 13.1. View the Errors Generated by a Command
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 13.2. Handle Warnings, Errors, and Terminating Errors
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 13.3. Output Warnings, Errors, and Terminating Errors
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 13.4. Debug a Script
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 13.5. Collect Detailed Traces of a Script or Command
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 13.6. Program: Analyze a Script's Performance Profile
          1. See Also
      8. 14. Environmental Awareness
        1. 14.0. Introduction
        2. 14.1. View and Modify Environment Variables
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 14.2. Access Information About Your Command's Invocation
          1. Problem
          2. Solution
          3. Discussion
            1. Scripts
            2. Functions
            3. Script blocks
        4. 14.3. Program: Investigate the InvocationInfo Variable
          1. See Also
        5. 14.4. Find Your Script's Name
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 14.5. Find Your Script's Location
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 14.6. Find the Location of Common System Paths
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        8. 14.7. Program: Search the Windows Start Menu
          1. See Also
        9. 14.8. Get the Current Location
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        10. 14.9. Safely Build File Paths Out of Their Components
          1. Problem
          2. Solution
          3. Discussion
        11. 14.10. Interact with PowerShell's Global Environment
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      9. 15. Extend the Reach of Windows PowerShell
        1. 15.0. Introduction
        2. 15.1. Access Windows Management Instrumentation Data
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 15.2. Program: Determine Properties Available to WMI Filters
          1. See Also
        4. 15.3. Program: Search for WMI Classes
          1. See Also
        5. 15.4. 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
        6. 15.5. 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
        7. 15.6. Automate Programs Using COM Scripting Interfaces
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        8. 15.7. Program: Query a SQL Data Source
          1. See Also
        9. 15.8. Access Windows Performance Counters
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        10. 15.9. Program: Invoke Native Windows API Calls
          1. See Also
        11. 15.10. Program: Add Inline C# to Your PowerShell Script
          1. See Also
        12. 15.11. Access a .NET SDK Library
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        13. 15.12. Create Your Own PowerShell Cmdlet
          1. Problem
          2. Discussion
            1. Step 1: Download the Windows SDK
            2. Step 2: Create a file to hold the cmdlet and snapin source code
            3. Step 3: Compile the snapin
            4. Step 4: Install and register the snapin
            5. Step 5: Add the snapin to your session
            6. Step 6: Use the snapin
        14. 15.13. Add PowerShell Scripting to Your Own Program
          1. Problem
          2. Discussion
            1. Step 1: Download the Windows SDK
            2. Step 2: Create a file to hold the hosting source code
            3. Step 3: Compile and run the example
          3. See Also
      10. 16. Security and Script Signing
        1. 16.0. Introduction
        2. 16.1. Enable Scripting Through an Execution Policy
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 16.2. Sign a PowerShell Script or Formatting File
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 16.3. Program: Create a Self-Signed Certificate
          1. Discussion
          2. See Also
        5. 16.4. Manage PowerShell Security in an Enterprise
          1. Problem
          2. Solution
          3. Discussion
            1. Apply PowerShell's Group Policy templates
            2. Deploy Microsoft Certificate services
            3. Apply software restriction policies
          4. See Also
        6. 16.5. Verify the Digital Signature of a PowerShell Script
          1. Problem
          2. Solution
          3. Discussion
        7. 16.6. Securely Handle Sensitive Information
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        8. 16.7. Securely Request Usernames and Passwords
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        9. 16.8. Program: Start a Process As Another User
          1. Discussion
          2. See Also
        10. 16.9. 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
        11. 16.10. Access User and Machine Certificates
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        12. 16.11. Program: Search the Certificate Store
          1. Discussion
          2. See Also
    8. IV. Administrator Tasks
      1. 17. Files and Directories
        1. 17.0. Introduction
        2. 17.1. Find All Files Modified Before a Certain Date
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 17.2. Clear or Remove a File
          1. Problem
          2. Solution
          3. Discussion
        4. 17.3. Manage and Change the Attributes of a File
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 17.4. Get the Files in a Directory
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 17.5. Find Files That Match a Pattern
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 17.6. Manage Files That Include Special Characters
          1. Problem
          2. Solution
          3. Discussion
        8. 17.7. Program: Get Disk Usage Information
          1. Discussion
          2. See Also
        9. 17.8. Determine the Current Location
          1. Problem
          2. Solution
          3. Discussion
        10. 17.9. Monitor a File for Changes
          1. Problem
          2. Solution
          3. Discussion
        11. 17.10. Program: Get the MD5 or SHA1 Hash of a File
          1. Discussion
          2. See Also
        12. 17.11. Create a Directory
          1. Problem
          2. Solution
          3. Discussion
        13. 17.12. Remove a File or Directory
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        14. 17.13. Rename a File or Directory
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        15. 17.14. Move a File or Directory
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        16. 17.15. Get the ACL of a File or Directory
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        17. 17.16. Set the ACL of a File or Directory
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        18. 17.17. Program: Add Extended File Properties to Files
          1. Discussion
          2. See Also
        19. 17.18. Program: Create a Filesystem Hard Link
          1. Discussion
          2. See Also
        20. 17.19. Program: Create a ZIP Archive
          1. Discussion
          2. See Also
      2. 18. The Windows Registry
        1. 18.0. Introduction
        2. 18.1. Navigate the Registry
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 18.2. View a Registry Key
          1. Problem
          2. Solution
          3. Discussion
        4. 18.3. Modify or Remove a Registry Key Value
          1. Problem
          2. Solution
          3. Discussion
        5. 18.4. Create a Registry Key Value
          1. Problem
          2. Solution
          3. Discussion
        6. 18.5. Remove a Registry Key
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 18.6. Add a Site to an Internet Explorer Security Zone
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        8. 18.7. Modify Internet Explorer Settings
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        9. 18.8. Program: Search the Windows Registry
          1. Discussion
          2. See Also
        10. 18.9. Get the ACL of a Registry Key
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        11. 18.10. Set the ACL of a Registry Key
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        12. 18.11. Work with the Registry of a Remote Computer
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        13. 18.12. Program: Get Registry Items from Remote Machines
          1. Discussion
          2. See Also
        14. 18.13. Program: Get Properties of Remote Registry Keys
          1. Discussion
          2. See Also
        15. 18.14. Program: Set Properties of Remote Registry Keys
          1. Discussion
          2. See Also
        16. 18.15. 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. 19. Comparing Data
        1. 19.0. Introduction
        2. 19.1. Compare the Output of Two Commands
          1. Problem
          2. Solution
          3. Discussion
        3. 19.2. Determine the Differences Between Two Files
          1. Problem
          2. Solution
          3. Discussion
        4. 19.3. Verify Integrity of File Sets
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      4. 20. Event Logs
        1. 20.0. Introduction
        2. 20.1. List All Event Logs
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 20.2. Get the Newest Entries from an Event Log
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 20.3. Find Event Log Entries with Specific Text
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 20.4. Retrieve a Specific Event Log Entry
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 20.5. Find Event Log Entries by Their Frequency
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 20.6. Back Up an Event Log
          1. Problem
          2. Solution
          3. Discussion
        8. 20.7. Create or Remove an Event Log
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        9. 20.8. Write to an Event Log
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        10. 20.9. Access Event Logs of a Remote Machine
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      5. 21. Processes
        1. 21.0. Introduction
        2. 21.1. List Currently Running Processes
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 21.2. Launch a Process
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 21.3. Stop a Process
          1. Problem
          2. Solution
          3. Discussion
        5. 21.4. Program: Invoke a PowerShell Expression on a Remote Machine
          1. See Also
      6. 22. System Services
        1. 22.0. Introduction
        2. 22.1. List All Running Services
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        3. 22.2. Manage a Running Service
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 22.3. Access Services on a Remote Machine
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      7. 23. Active Directory
        1. 23.0. Introduction
        2. 23.1. Test Active Directory Scripts on a Local Installation
          1. Problem
          2. Solution
          3. Discussion
            1. Install ADAM
            2. Create a test instance
          4. See Also
        3. 23.2. Create an Organizational Unit
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        4. 23.3. Get the Properties of an Organizational Unit
          1. Problem
          2. Solution
          3. Discussion
        5. 23.4. Modify Properties of an Organizational Unit
          1. Problem
          2. Solution
          3. Discussion
        6. 23.5. Get the Children of an Active Directory Container
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        7. 23.6. Create a User Account
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
          5. See Also
        8. 23.7. Search for a User Account
          1. Problem
          2. Solution
          3. Discussion
        9. 23.8. Get and List the Properties of a User Account
          1. Problem
          2. Solution
          3. Discussion
        10. 23.9. Modify Properties of a User Account
          1. Problem
          2. Solution
          3. Discussion
        11. 23.10. Create a Security or Distribution Group
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        12. 23.11. Search for a Security or Distribution Group
          1. Problem
          2. Solution
          3. Discussion
        13. 23.12. Get the Properties of a Group
          1. Problem
          2. Solution
          3. Discussion
        14. 23.13. Find the Owner of a Group
          1. Problem
          2. Solution
          3. Discussion
        15. 23.14. Modify Properties of a Security or Distribution Group
          1. Problem
          2. Solution
          3. Discussion
        16. 23.15. Add a User to a Security or Distribution Group
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        17. 23.16. Remove a User from a Security or Distribution Group
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        18. 23.17. List a User's Group Membership
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        19. 23.18. List the Members of a Group
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        20. 23.19. List the Users in an Organizational Unit
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        21. 23.20. Search for a Computer Account
          1. Problem
          2. Solution
          3. Discussion
        22. 23.21. Get and List the Properties of a Computer Account
          1. Problem
          2. Solution
          3. Discussion
      8. 24. Enterprise Computer Management
        1. 24.0. Introduction
        2. 24.1. Program: List Logon or Logoff Scripts for a User
          1. See Also
        3. 24.2. Program: List Startup or Shutdown Scripts for a Machine
          1. See Also
        4. 24.3. Enable or Disable the Windows Firewall
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        5. 24.4. Open or Close Ports in the Windows Firewall
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        6. 24.5. Program: List All Installed Software
          1. See Also
        7. 24.6. Uninstall an Application
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        8. 24.7. Manage Scheduled Tasks on a Computer
          1. Problem
          2. Solution
          3. Discussion
        9. 24.8. Retrieve Printer Information
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        10. 24.9. Retrieve Printer Queue Statistics
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        11. 24.10. Manage Printers and Print Queues
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        12. 24.11. Determine Whether a Hotfix Is Installed
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        13. 24.12. Program: Summarize System Information
          1. See Also
        14. 24.13. 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
        15. 24.14. Assign a Static IP Address
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
        16. 24.15. 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
        17. 24.16. List Network Adapter Properties
          1. Problem
          2. Solution
          3. Discussion
          4. See Also
      9. 25. Manage an Exchange 2007 Server
        1. 25.0. Introduction
        2. 25.1. Experiment with Exchange Management Shell
          1. Problem
          2. Solution
          3. Discussion
        3. 25.2. Automate Wizard-Guided Tasks
          1. Problem
          2. Solution
          3. Discussion
        4. 25.3. Manage Exchange Users
          1. Problem
          2. Solution
          3. Discussion
        5. 25.4. Manage Mailboxes
          1. Problem
          2. Solution
          3. Discussion
        6. 25.5. Manage Distribution Groups
          1. Problem
          2. Solution
          3. Discussion
        7. 25.6. Manage Transport Rules
          1. Problem
          2. Solution
          3. Discussion
        8. 25.7. Manage Outlook Web Access
          1. Problem
          2. Solution
          3. Discussion
      10. 26. Manage an Operations Manager 2007 Server
        1. 26.0. Introduction
        2. 26.1. Experiment with the Command Shell
          1. Problem
          2. Solution
          3. Discussion
        3. 26.2. Manage Operations Manager Agents
          1. Problem
          2. Solution
          3. Discussion
        4. 26.3. Schedule a Maintenance Window
          1. Problem
          2. Solution
          3. Discussion
        5. 26.4. Get, Install, and Uninstall Management Packs
          1. Problem
          2. Solution
          3. Discussion
        6. 26.5. Enable or Disable Rules
          1. Problem
          2. Solution
          3. Discussion
        7. 26.6. List and Start Tasks
          1. Problem
          2. Solution
          3. Discussion
        8. 26.7. Manage Alerts
          1. Problem
          2. Solution
          3. Discussion
    9. V. References
      1. A. PowerShell Language and Environment
        1. A.1. Commands and Expressions
        2. A.2. Comments
        3. A.3. Variables
        4. A.4. Booleans
        5. A.5. Strings
          1. Literal and Expanding Strings
          2. Here Strings
          3. Escape Sequences
        6. A.6. Numbers
          1. Simple Assignment
          2. Administrative Numeric Constants
          3. Hexadecimal and Other Number Bases
        7. A.7. Arrays and Lists
          1. Array Definitions
          2. Array Access
          3. Array Slicing
        8. A.8. Hashtables (Associative Arrays)
          1. Hashtable Definitions
          2. Hashtable Access
        9. A.9. XML
        10. A.10. Simple Operators
          1. Arithmetic Operators
          2. Logical Operators
          3. Binary Operators
          4. Other Operators
        11. A.11. Comparison Operators
        12. A.12. Conditional Statements
          1. if, elseif, and else Statements
          2. switch Statements
        13. A.13. 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
        14. A.14. Working with the .NET Framework
          1. Static Methods
          2. Instance Methods
          3. Static Properties
          4. Instance Properties
          5. Learning About Types
            1. The Get-Member Cmdlet
            2. .NET framework documentation
          6. Type Shortcuts
          7. Creating Instances of Types
          8. Interacting with COM Objects
          9. Extending Types
            1. The Add-Member cmdlet
            2. Custom type extension files
        15. A.15. Writing Scripts, Reusing Functionality
          1. Writing Scripts
          2. Running Scripts
            1. Invoking
            2. Dot-sourcing
          3. Providing Input to Scripts
            1. Argument array
            2. Formal parameters
            3. Pipeline input
            4. Cmdlet keywords in scripts
            5. $MyInvocation automatic variable
          4. Retrieving Output from Scripts
            1. Pipeline output
            2. Return statement
            3. Exit statement
          5. Functions
          6. Script Blocks
        16. A.16. Managing Errors
          1. Nonterminating Errors
          2. Terminating Errors
        17. A.17. Formatting Output
          1. Formatting Output
            1. Custom formatting files
        18. A.18. Capturing Output
        19. A.19. Tracing and Debugging
          1. The Set-PsDebug Cmdlet
          2. The Trace-Command Cmdlet
          3. Verbose Cmdlet Output
        20. A.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
      2. B. Regular Expression Reference
      3. C. PowerShell Automatic Variables
      4. D. Standard PowerShell Verbs
      5. E. Selected .NET Classes and Their Uses
      6. F. WMI Reference
      7. G. Selected COM Objects and Their Uses
      8. H. .NET String Formatting
        1. H.1. String Formatting Syntax
        2. H.2. Standard Numeric Format Strings
        3. H.3. Custom Numeric Format Strings
      9. I. .NET DateTime Formatting
        1. I.1. Custom DateTime Format Strings
    10. Index
    11. About the Author
    12. Colophon
    13. SPECIAL OFFER: Upgrade this ebook with O’Reilly