O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Windows PowerShell Step by Step, Third Edition

Book Description

Your hands-on guide to Windows PowerShell scripting fundamentals
Expand your expertise--and teach yourself the fundamentals of Windows PowerShell scripting, including features available in Windows PowerShell 5. If you are an IT professional, power user, or consultant, you’ll get the guidance, exercises, and code you need to master core techniques for automating Windows setup, deployment, and management.
Discover how to:

  • Run cmdlets and command-line utilities

  • Administer Windows-based servers and desktops with built-in cmdlets

  • Use providers to access external information

  • Write and run scripts from the Windows ISE

  • Create functions that are easy to maintain

  • Build standardized environments with profiles

  • Automate Windows systems with WMI, CIM cmdlets, and remoting

  • Automate Active Directory Domain Services (AD DS)

  • Debug scripts and handle errors

  • Run commands that survive interruptions

  • Use Desired State Configuration (DSC) to manage software services and their environments

  • Get powerful new modules from PowerShell Gallery

    About You
    This book is for:

  • IT professionals and power users who want to get productive with Windows PowerShell, including new features in Windows PowerShell 5

  • Windows system administrators who want to be more efficient and productive

  • Anyone pursuing Windows PowerShell certifications

  • No experience with Windows PowerShell or other scripting technologies necessary

    Table of Contents

    1. Cover
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents at a glance
    6. Contents
    7. Introduction
      1. Who should read this book
        1. Assumptions
      2. This book might not be for you if...
      3. Organization of this book
        1. Finding your best starting point in this book
      4. System requirements
      5. Downloads: Scripts
        1. Installing the scripts
        2. Using the scripts
      6. Acknowledgments
      7. Errata, updates, & book support
      8. Free ebooks from Microsoft Press
      9. We want to hear from you
      10. Stay in touch
    8. Chapter 1. Overview of Windows PowerShell 5.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 Windows 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
    9. Chapter 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 by using the Format-List cmdlet
        3. Using the Format-Wide cmdlet
        4. Formatting a directory listing by using Format-Table
      3. Formatting output with Out-GridView
      4. Taking advantage of the power of Get-Command
        1. Searching for cmdlets by 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
      10. Working with cmdlets: Step-by-step exercises
      11. Chapter 2 quick reference
    10. Chapter 3. Understanding and using Windows PowerShell providers
      1. Understanding Windows PowerShell providers
        1. Understanding the alias provider
        2. Understanding the certificate provider
        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
        2. The short way to create a new registry key
        3. Dealing with a missing registry property
      3. Understanding the variable provider
      4. Exploring Windows PowerShell providers: Step-by-step exercises
      5. Chapter 3 quick reference
    11. Chapter 4. Using Windows PowerShell remoting and jobs
      1. Understanding Windows PowerShell remoting
        1. Classic remoting
        2. WinRM
      2. Using Windows PowerShell jobs
      3. Using Windows PowerShell remoting and jobs: Step-by-step exercises
      4. Chapter 4 quick reference
    12. Chapter 5. Using Windows PowerShell scripts
      1. Why write Windows PowerShell scripts?
      2. The fundamentals of scripting
        1. Running Windows PowerShell scripts
        2. Turning on Windows PowerShell scripting support
        3. Transitioning from command line to script
        4. Manually running Windows PowerShell scripts
        5. Understanding variables and constants
      3. Using the While statement
        1. Constructing the While statement in Windows PowerShell
        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 Windows PowerShell Do...Until statement with VBScript
        2. Using the Windows PowerShell Do statement
      6. The For statement
        1. Using the For statement
        2. Using the Foreach statement
        3. Exiting the Foreach statement early
      7. Using the If statement
        1. Using assignment and comparison operators
        2. Evaluating multiple conditions
      8. The Switch statement
        1. Using the Switch statement
        2. Controlling matching behavior
      9. Creating multiple folders: Step-by-step exercises
      10. Chapter 5 quick reference
    13. Chapter 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
      5. Using two input parameters
      6. Using a type constraint in a function
      7. Using more than two input parameters
      8. Using functions to encapsulate business logic
      9. Using functions to provide ease of modification
      10. Understanding filters
      11. Creating a function: Step-by-step exercises
      12. Chapter 6 quick reference
    14. Chapter 7. Creating advanced functions and modules
      1. The [cmdletbinding] attribute
        1. Easy verbose messages
        2. Automatic parameter checks
        3. Adding support for the -WhatIf switch parameter
        4. Adding support for the -Confirm switch 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
        1. Locating and loading modules
        2. Installing modules
        3. Creating a module
      4. Creating an advanced function and installing a module: Step-by-step exercises
      5. Chapter 7 quick reference
    15. Chapter 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. Using 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 and snippets: Step-by-step exercises
      4. Chapter 8 quick reference
    16. Chapter 9. Working with Windows PowerShell profiles
      1. Six different Windows PowerShell profiles
        1. Understanding the six Windows PowerShell profiles
        2. Examining the $profile variable
        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 and adding functionality to a profile: Step-by-step exercises
      5. Chapter 9 quick reference
    17. Chapter 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
    18. Chapter 11. Querying WMI
      1. Alternate ways to connect to WMI
        1. Returning selective data from all instances
        2. Selecting multiple properties
        3. Choosing specific instances
        4. Using an operator
        5. Shortening the syntax
      2. Working with software: Step-by-step exercises
      3. Chapter 11 quick reference
    19. Chapter 12. Remoting WMI
      1. Using WMI against remote systems
        1. Supplying alternate credentials for the remote connection
        2. Using Windows PowerShell remoting to run WMI
        3. Using CIM classes to query WMI classes
      2. Working with remote results
        1. Reducing data via Windows PowerShell parameters
        2. Reducing data via WQL query
      3. Running WMI jobs
      4. Using Windows PowerShell remoting and WMI: Step-by-step exercises
      5. Chapter 12 quick reference
    20. Chapter 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 cmdlets to work with static methods
      3. Executing instance methods: Step-by-step exercises
      4. Chapter 13 quick reference
    21. Chapter 14. Using the CIM cmdlets
      1. Using the CIM cmdlets to explore WMI classes
        1. Using the Get-CimClass cmdlet and 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
    22. Chapter 15. Working with Active Directory
      1. Creating objects in Active Directory
        1. Creating an OU
        2. ADSI providers
        3. LDAP names
      2. Creating users
        1. What is user account control?
        2. Working with users
      3. Creating multiple OUs: Step-by-step exercises
      4. Chapter 15 quick reference
    23. Chapter 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
    24. Chapter 17. Deploying Active Directory by using Windows PowerShell
      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. Installing domain controller prerequisites and adding to a forest: Step-by-step exercises
      5. Chapter 17 quick reference
    25. Chapter 18. Debugging scripts
      1. Understanding debugging in Windows PowerShell
        1. Understanding the three different types of errors
      2. Using the Set-PSDebug cmdlet
        1. Tracing the script
        2. Stepping through the script
        3. Enabling strict mode
      3. 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
      4. Debugging a function: Step-by-step exercises
      5. Chapter 18 quick reference
    26. Chapter 19. Handling errors
      1. Handling missing parameters
        1. Creating a default value for a parameter
        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 the contents of an array
        4. Using the -contains operator to test for properties
      3. Handling missing rights
        1. Using an attempt-and-fail approach
        2. Checking for rights and exiting gracefully
      4. Handling missing WMI providers
      5. Handling incorrect data types
      6. Handling 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 and using Try...Catch...Finally: Step-by-step exercises
      9. Chapter 19 quick reference
    27. Chapter 20. Using the Windows PowerShell workflow
      1. Why use workflows?
        1. Workflow requirements
        2. A simple workflow
      2. Parallel PowerShell
      3. Workflow activities
        1. Windows PowerShell cmdlets as activities
        2. Disallowed core cmdlets
        3. Non-automatic cmdlet activities
        4. Parallel activities
      4. Checkpointing Windows PowerShell workflow
        1. Understanding checkpoints
        2. Placing checkpoints
        3. Adding checkpoints
      5. Adding a sequence activity to a workflow
      6. Creating a workflow and adding checkpoints: Step-by-step exercises
      7. Chapter 20 quick reference
    28. Chapter 21. Managing Windows PowerShell DSC
      1. Understanding Desired State Configuration
        1. The DSC process
        2. Configuration parameters
        3. Setting dependencies
      2. Controlling configuration drift
      3. Modifying environment variables
      4. Creating a DSC configuration and adding a dependency: Step-by-step exercises
      5. Chapter 21 quick reference
    29. Chapter 22. Using the PowerShell Gallery
      1. Exploring the PowerShell Gallery
      2. Configuring and using PowerShell Get
      3. Installing a module from the PowerShell Gallery
        1. Configuring trusted installation locations
        2. Uninstalling a module
      4. Searching for and installing modules from the PowerShell Gallery: Step-by-step exercises
      5. Chapter 22 quick reference
    30. Appendix A. Windows PowerShell scripting best practices
      1. General script construction
        1. Include functions in the scripts that use them
        2. Use full cmdlet names and full parameter names
        3. Use Get-Item to convert path strings to rich types
      2. General script readability
      3. Format your code
        1. Work with functions
        2. Create template files
        3. Format functions
        4. Variables, constants, and naming
    31. Appendix B. Regular expressions quick reference
    32. Index
    33. About the author
    34. Survey
    35. Code Snippets