You are previewing Windows PowerShell 3.0 First Steps.
O'Reilly logo
Windows PowerShell 3.0 First Steps

Book Description

Get started with this powerful Windows administration tool

Automate Windows administration tasks with ease by learning the fundamentals of Windows PowerShell 3.0. Led by a Windows PowerShell expert, you’ll learn must-know concepts and techniques through easy-to-follow explanations, examples, and exercises. Once you complete this practical introduction, you can go deeper into the Windows PowerShell command line interface and scripting language with Windows PowerShell 3.0 Step by Step.

Discover how to:

  • Create effective Windows PowerShell commands with one line of code

  • Apply Windows PowerShell commands across several Windows platforms

  • Identify missing hotfixes and service packs with a single command

  • Sort, group, and filter data using the Windows PowerShell pipeline

  • Create users, groups, and organizational units in Active Directory

  • Add computers to a domain or workgroup with a single line of code

  • Run Windows PowerShell commands on multiple remote computers

  • Unleash the power of scripting with Windows Management Instrumentation (WMI)

  • Table of Contents

    1. Dedication
    2. Special Upgrade Offer
    3. Foreword
    4. Introduction
      1. System Requirements
        1. Hardware Requirements
        2. Software Requirements
      2. Acknowledgments
      3. Support & Feedback
        1.  
          1. Errata
          2. We Want to Hear from You
          3. Stay in Touch
    5. 1. Overview of Windows PowerShell 3.0
      1. Understanding Windows PowerShell
      2. Working with Windows PowerShell
        1. Security issues with Windows PowerShell
          1. Running as a non-elevated user
          2. Launching Windows PowerShell with administrator rights
      3. Using Windows PowerShell cmdlets
        1. The most common verb: <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Get</em></span>
          1. Finding process information
          2. Identifying installed Windows hotfixes
          3. Getting detailed service information
          4. Identifying installed network adapters
          5. Retrieving detected network connection profiles
          6. Getting the current culture settings
          7. Finding the current date and time
          8. Generating a random number
      4. Supplying options for cmdlets
        1. Using single parameters
          1. Finding specific types of hotfixes
          2. Finding specific processes
          3. Generating random numbers in a range
        2. Introduction to parameter sets
          1. Generating a certain number of random numbers
        3. Using command-line utilities
      5. Working with Help options
      6. Summary
    6. 2. Using Windows PowerShell cmdlets
      1. Understanding the basics of cmdlets
        1. Common Windows PowerShell parameters
          1. Using the Verbose parameter to provide additional information
          2. Using the ErrorAction parameter to hide errors
        2. Starting the Windows PowerShell transcript
        3. Stopping and reviewing the Windows PowerShell transcript
      2. Searching the Help topics
        1. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Get-Help</em></span> cmdlet cmdlet
          1. Paging the Help output
          2. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Get-Help</em></span> cmdlet to search for Windows PowerShell cmdlets cmdlet to search for Windows PowerShell cmdlets
        2. Using the About conceptual Help topics
      3. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Get-Command</em></span> to find cmdlets to find cmdlets
      4. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Get-Member</em></span> cmdlet cmdlet
        1. Exploring property members
      5. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Show-Command</em></span> cmdlet cmdlet
      6. Setting the Script Execution Policy
      7. Creating a basic Windows PowerShell profile
        1. Determining if a Windows PowerShell profile exists
        2. Creating a new Windows PowerShell profile
      8. Summary
    7. 3. Filtering, grouping, and sorting
      1. Introduction to the pipeline
      2. Sorting output from a cmdlet
      3. Grouping output after sorting
        1. Grouping information without element data
      4. Filtering output from one cmdlet
        1. Filtering by date
        2. Filtering to the left
      5. Filtering output from one cmdlet before sorting
      6. Summary
    8. 4. Formatting output
      1. Creating a table
        1. Choosing specific properties in a specific order
        2. Controlling the way the table displays
      2. Creating a list
        1. Choosing properties by name
        2. Choosing properties by wildcard
      3. Creating a wide display
        1. Using the -AutoSize parameter to configure the output
        2. Customizing the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Format-Wide</em></span> output output
      4. Creating an output grid
        1. Sorting output by using the column buttons
        2. Filtering output by using the filter box
      5. Summary
    9. 5. Storing output
      1. Storing data in text files
        1. Redirect and append
        2. Redirect and overwrite
        3. Controlling the text file
      2. Storing data in .csv files
        1. No type information
        2. Using type information
      3. Storing data in XML
        1. The problem with complex objects
        2. Using XML to store complex objects
      4. Summary
    10. 6. Leveraging Windows PowerShell providers
      1. Understanding Windows PowerShell providers
        1. Understanding the Alias provider
        2. Understanding the Certificate provider
          1. Searching for specific certificates
          2. Finding expiring certificates
        3. Understanding the Environment provider
        4. Understanding the File System provider
        5. Understanding the Function provider
        6. Understanding the Registry provider
          1. The two registry drives
          2. Retrieving registry values
          3. Creating new registry keys
          4. The short way to create a new registry key
          5. Setting the default value for the key
          6. Using <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>New-Item</em></span> to create and assign a value to create and assign a value
          7. Modifying the value of a registry property value
          8. Dealing with a missing registry property value
        7. Understanding the Variable provider
      2. Summary
    11. 7. Using Windows PowerShell remoting
      1. Using Windows PowerShell remoting
        1. Classic remoting
      2. Configuring Windows PowerShell remoting
        1. Running commands
          1. Running a single Windows PowerShell command
          2. Running a single command against multiple computers
        2. Creating a persisted connection
      3. Troubleshooting Windows PowerShell remoting
      4. Summary
    12. 8. Using WMI
      1. Understanding the WMI Model
        1. Working with objects and namespaces
        2. Listing WMI providers
        3. Working with WMI classes
      2. Querying WMI: The basics
        1. Tell me everything about everything
        2. Tell me selected things about everything
        3. Tell me everything about some things
        4. Tell me selected things about some things
      3. Summary
    13. 9. Using CIM
      1. Using CIM cmdlets to explore WMI classes
        1. Using the classname parameter
        2. Finding WMI class methods
        3. Filtering classes by qualifier
      2. Retrieving WMI instances
        1. Reducing returned properties and instances
        2. Cleaning up output from the command
      3. Working with associations
      4. Summary
    14. 10. Using the Windows PowerShell ISE
      1. Running the Windows PowerShell ISE
        1. Navigating the Windows PowerShell ISE
        2. Working with the Script pane
        3. Tab expansion and Intellisense
      2. Working with Windows PowerShell ISE snippets
        1. Using Windows PowerShell ISE snippets to create code
        2. Creating new Windows PowerShell ISE snippets
        3. Removing user-defined Windows PowerShell ISE snippets
      3. Summary
    15. 11. Using Windows PowerShell scripts
      1. Why write Windows PowerShell scripts?
      2. Scripting fundamentals
        1. Running Windows PowerShell scripts
        2. Enabling Windows PowerShell scripting support
        3. Transitioning from command line to script
        4. Running Windows PowerShell scripts
        5. Understanding variables and constants
          1. Using variables
      3. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>While</em></span> statement statement
        1. Constructing the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>While</em></span> statement statement
          1. Understanding expanding strings
          2. Understanding literal strings
        2. A practical example of using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>While</em></span> statement statement
        3. Using special features of Windows PowerShell
      4. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Do&#8230;While</em></span> statement statement
        1. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>range</em></span> operator operator
        2. Operating over an array
        3. Casting to ASCII values
      5. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Do&#8230;Until</em></span> statement statement
        1. Using the Windows PowerShell <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Do&#8230;Loop</em></span> statement statement
          1. The <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Do&#8230;While</em></span> and and <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Do&#8230;Until</em></span> statements always run once statements always run once
          2. The While statement is used to prevent unwanted execution
      6. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>For</em></span> statement statement
        1. Creating a <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>For&#8230;Loop</em></span>
        2. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>ForEach</em></span> statement statement
          1. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>ForEach</em></span> statement from the Windows PowerShell console statement from the Windows PowerShell console
        3. Exiting the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>ForEach</em></span> statement early statement early
          1. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Break</em></span> statement statement
          2. Using the Exit statement
      7. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>If</em></span> statement statement
        1. Using assignment and comparison operators
        2. Evaluating multiple conditions
      8. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Switch</em></span> statement statement
        1. Using the basic <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Switch</em></span> statement statement
          1. Defining the Default condition
          2. Understanding matching
          3. Evaluating an array
        2. Controlling matching behavior
      9. Summary
    16. 12. Working with functions
      1. Understanding functions
        1. Using a type constraint
      2. Using multiple input parameters
      3. Using functions to encapsulate business logic
      4. Using functions to provide ease of modification
      5. Summary
    17. 13. Debugging scripts
      1. Understanding debugging in Windows PowerShell
      2. Debugging the script
        1. Setting breakpoints
        2. Setting a breakpoint on a line number
        3. Setting a breakpoint on a variable
        4. Setting a breakpoint on a command
        5. Responding to breakpoints
        6. Listing breakpoints
        7. Enabling and disabling breakpoints
        8. Deleting breakpoints
      3. Summary
    18. 14. Handling errors
      1. Handling missing parameters
        1. Creating a default value for the parameter
          1. Detecting the missing value and assigning it in the script
          2. Assigning the value in the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Param</em></span> statement statement
        2. Making the parameter mandatory
      2. Limiting choices
        1. Using PromptForChoice to limit selections
        2. Using <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Test-Connection</em></span> to identify accessible computers to identify accessible computers
        3. Using the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>contains</em></span> operator to examine contents of an array operator to examine contents of an array
      3. Handling missing rights
        1. Attempting and failing
        2. Checking for rights and exiting gracefully
      4. Using <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Try/Catch/Finally</em></span>
      5. Summary
    19. A. Windows PowerShell FAQ
    20. B. Windows PowerShell 3.0 coding conventions
      1. General script construction
        1. Include functions in the script that uses the functions
        2. Use full cmdlet names and full parameter names
          1. Understanding the use of aliases
          2. If you must use an alias, use only canonical aliases in a script
          3. Always use the <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Description</em></span> property when creating an alias property when creating an alias
        3. Use <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="emphasis"><em>Get-Item</em></span> to convert path strings to rich types to convert path strings to rich types
      2. General script readability
      3. Formatting your code
        1. Working with functions
        2. Creating template files
        3. Writing your own functions
        4. Variables, constants, and naming
    21. Index
    22. About the Author
    23. Special Upgrade Offer
    24. Copyright