You are previewing Windows PowerShell™ 3.0 Step by Step.
O'Reilly logo
Windows PowerShell™ 3.0 Step by Step

Book Description

Your hands-on, step-by-step guide to automating Windows administration with Windows PowerShell 3.0

Teach yourself the fundamentals of Windows PowerShell 3.0 command line interface and scripting language—one step at a time. Written by a leading scripting expert, this practical tutorial delivers learn-by-doing exercises, timesaving tips, and hands-on sample scripts for performing administrative tasks on both local and remote Windows systems.

Discover how to:

  • Use built-in cmdlets to execute commands

  • Write scripts to handle recurring tasks

  • Use providers to access information beyond the shell environment

  • Configure network components with Windows Management Instrumentation

  • Manage users, groups, and computers with Active Directory services

  • Execute scripts to administer and troubleshoot Microsoft Exchange Server 2010

  • Table of Contents

    1. Windows PowerShell™ 3.0 Step by Step
    2. Dedication
    3. Foreword
    4. Introduction
      1. Who should read this book
        1. Assumptions
      2. Who should not read this book
      3. Organization of this book
        1. Finding your best starting point in this book
      4. Conventions and features in this book
      5. System requirements
      6. Code samples
        1. Installing the code samples
      7. Acknowledgments
      8. Errata and book support
      9. We want to hear from you
      10. Stay in touch
    5. 1. Overview of Windows PowerShell 3.0
      1. Understanding Windows PowerShell
        1. Using cmdlets
        2. Installing Windows PowerShell
        3. Deploying Windows PowerShell to down-level operating systems
      2. Using command-line utilities
      3. Security issues with Windows PowerShell
        1. Controlling execution of PowerShell cmdlets
        2. Confirming actions
        3. Suspending confirmation of cmdlets
      4. Working with Windows PowerShell
        1. Accessing Windows PowerShell
        2. Configuring the Windows PowerShell console
      5. Supplying options for cmdlets
      6. Working with the help options
      7. Exploring commands: step-by-step exercises
      8. Chapter 1 quick reference
    6. 2. Using Windows PowerShell Cmdlets
      1. Understanding the basics of cmdlets
      2. Using the Get-ChildItem cmdlet
        1. Obtaining a directory listing
        2. Formatting a directory listing using the Format-List cmdlet
        3. Using the Format-Wide cmdlet
        4. Formatting a directory listing using Format-Table
      3. Formatting output with Out-GridView
      4. Leveraging the power of Get-Command
        1. Searching for cmdlets using wildcard characters
      5. Using the Get-Member cmdlet
        1. Using the Get-Member cmdlet to examine properties and methods
      6. Using the New-Object cmdlet
        1. Creating and Using the wshShell Object
      7. Using the Show-Command cmdlet
      8. Windows PowerShell cmdlet naming helps you learn
        1. Windows PowerShell verb grouping
        2. Windows PowerShell verb distribution
      9. Creating a Windows PowerShell profile
        1. Finding all aliases for a particular object
      10. Working with cmdlets: step-by-step exercises
      11. Chapter 2 quick reference
    7. 3. Understanding and Using PowerShell Providers
      1. Understanding 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 filesystem provider
        5. Understanding the function provider
      2. Using the registry provider to manage the Windows registry
        1. The two registry drives
          1. Retrieving registry values
          2. Creating new registry keys
          3. The short way to create a new registry key
          4. Setting the default value for the key
          5. Using New-Item to create and assign a value
          6. Modifying the value of a registry property value
          7. The short way to change a registry property value
          8. Dealing with a missing registry property
      3. Understanding the variable provider
      4. Exploring PowerShell providers: step-by-step exercises
      5. Chapter 3 quick reference
    8. 4. Using PowerShell Remoting and Jobs
      1. Understanding Windows PowerShell remoting
        1. Classic remoting
        2. WinRM
          1. Creating a remote Windows PowerShell session
          2. Running a single Windows PowerShell command
      2. Using Windows PowerShell jobs
      3. Using Windows PowerShell remoting: step-by-step exercises
      4. Chapter 4 quick reference
    9. 5. Using 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. Use of variables
        6. Use of constants
      3. Using the While statement
        1. Constructing the While statement in PowerShell
          1. Understanding expanding strings
          2. Understanding literal strings
        2. A practical example of using the While statement
        3. Using special features of Windows PowerShell
      4. Using the Do...While statement
        1. Using the range operator
        2. Operating over an array
        3. Casting to ASCII values
      5. Using the Do...Until statement
        1. Comparing the PowerShell Do...Until statement with VBScript
        2. Using the Windows PowerShell Do statement
          1. The Do...While and Do...Until statements always run once
          2. The While statement is used to prevent unwanted execution
      6. The For statement
        1. Using the For statement
        2. Using the Foreach statement
          1. Using the Foreach statement from the Windows PowerShell console
        3. Exiting the Foreach statement early
          1. Using the Break statement
          2. Using the Exit statement
      7. The If statement
        1. Using assignment and comparison operators
        2. Evaluating multiple conditions
      8. The Switch statement
        1. Using the Switch statement
          1. Defining the default condition
          2. Understanding matching with the Switch statement
          3. Evaluating an array
        2. Controlling matching behavior
      9. Creating multiple folders: step-by-step exercises
      10. Chapter 5 quick reference
    10. 6. Working with Functions
      1. Understanding functions
      2. Using functions to provide ease of code reuse
      3. Including functions in the Windows PowerShell environment
        1. Using dot-sourcing
        2. Using dot-sourced functions
      4. Adding help for functions
        1. Using a here-string object for help
        2. Using two input parameters
        3. Using a type constraint in a function
      5. Using more than two input parameters
      6. Use of functions to encapsulate business logic
      7. Use of functions to provide ease of modification
      8. Understanding filters
      9. Creating a function: step-by-step exercises
      10. Chapter 6 quick reference
    11. 7. Creating Advanced Functions and Modules
      1. The [cmdletbinding] attribute
        1. Easy verbose messages
        2. Automatic parameter checks
        3. Adding support for the -whatif parameter
        4. Adding support for the -confirm parameter
        5. Specifying the default parameter set
      2. The parameter attribute
        1. The mandatory parameter property
        2. The position parameter property
        3. The ParameterSetName parameter property
        4. The ValueFromPipeline property
        5. The HelpMessage property
      3. Understanding modules
      4. Locating and loading modules
        1. Listing available modules
        2. Loading modules
      5. Installing modules
        1. Creating a per-user Modules folder
        2. Working with the $modulePath variable
        3. Creating a module drive
        4. Checking for module dependencies
        5. Using a module from a share
      6. Creating a module
      7. Creating an advanced function: step-by-step exercises
      8. Chapter 7 quick reference
    12. 8. 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. Using the Commands add-on: step-by-step exercises
      4. Chapter 8 quick reference
    13. 9. Working with Windows PowerShell Profiles
      1. Six Different PowerShell profiles
        1. Understanding the six different Windows PowerShell profiles
        2. Examining the $profile variable
          1. Unraveling the different profiles
          2. Using the $profile variable to refer to more than the current host
        3. Determining whether a specific profile exists
        4. Creating a new profile
      2. Design considerations for profiles
        1. Using one or more profiles
        2. Using the All Users, All Hosts profile
        3. Using your own file
      3. Grouping similar functionality into a module
        1. Where to store the profile module
      4. Creating a profile: step-by-step exercises
      5. Chapter 9 quick reference
    14. 10. Using WMI
      1. Understanding the WMI model
      2. Working with objects and namespaces
      3. Listing WMI providers
      4. Working with WMI classes
      5. Querying WMI
      6. Obtaining service information: step-by-step exercises
      7. Chapter 10 quick reference
    15. 11. Querying WMI
      1. Alternate ways to connect to WMI
        1. Selective data from all instances
        2. Selecting multiple properties
        3. Choosing specific instances
        4. Utilizing an operator
        5. Where is the where?
        6. Shortening the syntax
          1. Using the -property parameter
          2. Using the -filter parameter
      2. Working with software: step-by-step exercises
      3. Chapter 11 quick reference
    16. 12. Remoting WMI
      1. Using WMI against remote systems
        1. Supplying alternate credentials for the remote connection
          1. Storing the credentials for a remote connection
        2. Using Windows PowerShell remoting to run WMI
        3. Using CIM classes to query WMI classes
          1. Using CIM to query remote WMI data
      2. Working with remote results
        1. Reducing data via Windows PowerShell parameters
      3. Running WMI jobs
      4. Using Windows PowerShell remoting and WMI: Step-by-step exercises
      5. Chapter 12 quick reference
    17. 13. Calling WMI Methods on WMI Classes
      1. Using WMI cmdlets to execute instance methods
        1. Using the terminate method directly
        2. Using the Invoke-WmiMethod cmdlet
        3. Using the [wmi] type accelerator
      2. Using WMI to work with static methods
      3. Executing instance methods: step-by-step exercises
      4. Chapter 13 quick reference
    18. 14. Using the CIM Cmdlets
      1. Using the 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. Retrieving WMI instances: step-by-step exercises
      5. Chapter 14 quick reference
    19. 15. Working with Active Directory
      1. Creating objects in Active Directory
        1. Creating an OU
        2. ADSI providers
        3. LDAP names
          1. Binding
      2. Creating users
        1. What is user account control?
        2. Working with users
          1. General user information
          2. Creating the address page
          3. Deleting users
      3. Creating multiple organizational units: step-by-step exercises
      4. Chapter 15 quick reference
    20. 16. Working with the AD DS Module
      1. Understanding the Active Directory module
        1. Installing the Active Directory module
        2. Getting started with the Active Directory module
      2. Using the Active Directory module
        1. Finding the FSMO role holders
        2. Discovering Active Directory
        3. Renaming Active Directory sites
        4. Managing users
        5. Creating a user
        6. Finding and unlocking Active Directory user accounts
        7. Finding disabled users
        8. Finding unused user accounts
      3. Updating Active Directory objects: step-by-step exercises
      4. Chapter 16 quick reference
    21. 17. Deploying Active Directory with Windows Server 2012
      1. Using the Active Directory module to deploy a new forest
      2. Adding a new domain controller to an existing domain
      3. Adding a read-only domain controller
      4. Domain controller prerequisites: step-by-step exercises
      5. Chapter 17 quick reference
    22. 18. Debugging Scripts
      1. Understanding debugging in Windows PowerShell
        1. Understanding three different types of errors
          1. Working with syntax errors
          2. Working with run-time errors
          3. Working with logic errors
      2. Using the Set-PSDebug cmdlet
        1. Tracing the script
          1. Working with trace level 1
          2. Working with trace level 2
        2. Stepping through the script
      3. Enabling strict mode
        1. Using Set-PSDebug -Strict
        2. Using the Set-StrictMode cmdlet
      4. 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
      5. Debugging a function: step-by-step exercises
      6. Chapter 18 quick reference
    23. 19. Handling Errors
      1. Handling missing parameters
        1. Creating a default value for a parameter
          1. Detecting a missing value and assigning it in the script
          2. Assigning a value in the param statement
        2. Making the parameter mandatory
      2. Limiting choices
        1. Using PromptForChoice to limit selections
        2. Using Test-Connection to identify computer connectivity
        3. Using the -contains operator to examine contents of an array
        4. Using the -contains operator to test for properties
      3. Handling missing rights
        1. Attempt and fail
        2. Checking for rights and exiting gracefully
      4. Handling missing WMI providers
      5. Incorrect data types
      6. Out-of-bounds errors
        1. Using a boundary-checking function
        2. Placing limits on the parameter
      7. Using Try...Catch...Finally
        1. Catching multiple errors
      8. Using PromptForChoice to limit selections: Step-by-step exercises
      9. Chapter 19 quick reference
    24. 20. Managing Exchange Server
      1. Exploring the Exchange 2010 cmdlets
      2. Working with remote Exchange servers
      3. Configuring recipient settings
        1. Creating the user and the mailbox
        2. Reporting user settings
      4. Managing storage settings
        1. Examining the mailbox database
        2. Managing the mailbox database
      5. Managing Exchange logging
      6. Managing auditing
      7. Parsing the audit XML file
      8. Creating user accounts: step-by-step exercises
      9. Chapter 20 quick reference
    25. A. Windows PowerShell Core Cmdlets
    26. B. Windows PowerShell Module Coverage
    27. C. Windows PowerShell Cmdlet Naming
    28. D. Windows PowerShell FAQ
    29. E. Useful WMI Classes
    30. F. Basic Troubleshooting Tips
    31. G. General PowerShell Scripting Guidelines
      1. General script construction
        1. Include functions in the script that uses the function
        2. Use full cmdlet names and full parameter names
          1. Understanding the use of aliases
          2. If you must use an alias, only use canonical aliases in a script
          3. Always use the description property when creating an alias
        3. Use Get-Item to convert path strings to rich types
      2. General script readability
      3. Formatting your code
        1. Working with functions
        2. Creating template files
        3. Functions
        4. Variables, constants, and naming
    32. H. About the Author
    33. Index
    34. About the Author
    35. Copyright