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

Learn Windows PowerShell in a Month of Lunches, Third Edition

Book Description

Learn Windows PowerShell in a Month of Lunches, Third Edition is an innovative tutorial designed for busy IT professionals. Just set aside one hour a day - lunchtime would be perfect - for a month, and you'll be automating Windows tasks faster than you ever thought possible. This updated edition covers PowerShell features that run on Windows 7, Windows Server 2008 R2 and later, PowerShell v3 and later, and includes v5 features like PowerShellGet.

Table of Contents

  1. Learn Windows PowerShell in a Month of Lunches, Third Edition
    1. Don Jones and Jeffery Hicks
  2. Copyright
  3. Brief Table of Contents
  4. Table of Contents
  5. Preface
  6. Acknowledgments
  7. About this Book
    1. Author Online
  8. About the Authors
  9. Chapter 1. Before you begin
    1. 1.1. Why you can’t afford to ignore PowerShell
      1. 1.1.1. Life without PowerShell
      2. 1.1.2. Life with PowerShell
    2. 1.2. And now, it’s just “PowerShell”
    3. 1.3. Is this book for you?
    4. 1.4. How to use this book
      1. 1.4.1. The main chapters
      2. 1.4.2. Hands-on labs
      3. 1.4.3. Code samples
      4. 1.4.4. Supplementary materials
      5. 1.4.5. Further exploration
      6. 1.4.6. Above and beyond
    5. 1.5. Setting up your lab environment
    6. 1.6. Installing Windows PowerShell
    7. 1.7. Contacting us
    8. 1.8. Being immediately effective with PowerShell
  10. Chapter 2. Meet PowerShell
    1. 2.1. Choose your weapon
      1. 2.1.1. The console window
      2. 2.1.2. The Integrated Scripting Environment
    2. 2.2. It’s typing class all over again
    3. 2.3. Common points of confusion
    4. 2.4. What version is this?
    5. 2.5. Lab
  11. Chapter 3. Using the help system
    1. 3.1. The help system: how you discover commands
    2. 3.2. Updatable help
    3. 3.3. Asking for help
    4. 3.4. Using help to find commands
    5. 3.5. Interpreting the help
      1. 3.5.1. Parameter sets and common parameters
      2. 3.5.2. Optional and mandatory parameters
      3. 3.5.3. Positional parameters
      4. 3.5.4. Parameter values
      5. 3.5.5. Finding command examples
    6. 3.6. Accessing “about” topics
    7. 3.7. Accessing online help
    8. 3.8. Lab
    9. 3.9. Lab answers
  12. Chapter 4. Running commands
    1. 4.1. Not scripting, but running commands
    2. 4.2. The anatomy of a command
    3. 4.3. The cmdlet naming convention
    4. 4.4. Aliases: nicknames for commands
    5. 4.5. Taking shortcuts
      1. 4.5.1. Truncating parameter names
      2. 4.5.2. Using parameter name aliases
      3. 4.5.3. Using positional parameters
    6. 4.6. Cheating a bit: Show-Command
    7. 4.7. Support for external commands
    8. 4.8. Dealing with errors
    9. 4.9. Common points of confusion
      1. 4.9.1. Typing cmdlet names
      2. 4.9.2. Typing parameters
    10. 4.10. Lab
    11. 4.11 Lab Answers
  13. Chapter 5. Working with providers
    1. 5.1. What are providers?
    2. 5.2. Understanding how the filesystem is organized
    3. 5.3. Understanding how the filesystem is like other data stores
    4. 5.4. Navigating the filesystem
    5. 5.5. Using wildcards and literal paths
    6. 5.6. Working with other providers
    7. 5.7. Lab
    8. 5.8. Further exploration
    9. 5.9. Lab answers
  14. Chapter 6. The pipeline: connecting commands
    1. 6.1. Connecting one command to another: less work for you
    2. 6.2. Exporting to a CSV or an XML file
      1. 6.2.1. Exporting to CSV
      2. 6.2.2. Exporting to XML
      3. 6.2.3. Comparing files
    3. 6.3. Piping to a file or a printer
    4. 6.4. Converting to HTML
    5. 6.5. Using cmdlets that modify the system: killing processes and stopping services
    6. 6.6. Common points of confusion
    7. 6.7. Lab
    8. 6.8. Lab answers
  15. Chapter 7. Adding commands
    1. 7.1. How one shell can do everything
    2. 7.2. About product-specific “management shells”
    3. 7.3. Extensions: finding and adding snap-ins
    4. 7.4. Extensions: finding and adding modules
    5. 7.5. Command conflicts and removing extensions
    6. 7.6. On non-Windows operating systems
    7. 7.7. Playing with a new module
    8. 7.8. Profile scripts: preloading extensions when the shell starts
    9. 7.9. Getting modules from the internet
    10. 7.10. Common points of confusion
    11. 7.11. Lab
    12. 7.12. Lab answers
  16. Chapter 8. Objects: data by another name
    1. 8.1. What are objects?
    2. 8.2. Understanding why PowerShell uses objects
    3. 8.3. Discovering objects: Get-Member
    4. 8.4. Using object attributes, or properties
    5. 8.5. Using object actions, or methods
    6. 8.6. Sorting objects
    7. 8.7. Selecting the properties you want
    8. 8.8. Objects until the end
    9. 8.9. Common points of confusion
    10. 8.10. Lab
    11. 8.11. Lab answers
  17. Chapter 9. The pipeline, deeper
    1. 9.1. The pipeline: enabling power with less typing
    2. 9.2. How PowerShell passes data down the pipeline
    3. 9.3. Plan A: pipeline input ByValue
    4. 9.4. Plan B: pipeline input ByPropertyName
    5. 9.5. When things don’t line up: custom properties
    6. 9.6. Parenthetical commands
    7. 9.7. Extracting the value from a single property
    8. 9.8. Lab
    9. 9.9. Further exploration
    10. 9.10. Lab answers
  18. Chapter 10. Formatting—and why it’s done on the right
    1. 10.1. Formatting: making what you see prettier
    2. 10.2. Working with the default formatting
    3. 10.3. Formatting tables
    4. 10.4. Formatting lists
    5. 10.5. Formatting wide lists
    6. 10.6. Creating custom columns and list entries
    7. 10.7. Going out: to a file, a printer, or the host
    8. 10.8. Another out: GridViews
    9. 10.9. Common points of confusion
      1. 10.9.1. Always format right
      2. 10.9.2. One type of object at a time, please
    10. 10.10. Lab
    11. 10.11. Further exploration
    12. 10.12. Lab answers
  19. Chapter 11. Filtering and comparisons
    1. 11.1. Making the shell give you just what you need
    2. 11.2. Filtering left
    3. 11.3. Using comparison operators
    4. 11.4. Filtering objects out of the pipeline
    5. 11.5. Using the iterative command-line model
    6. 11.6. Common points of confusion
      1. 11.6.1. Filter left, please
      2. 11.6.2. When $_ is allowed
    7. 11.7. Lab
    8. 11.8. Further exploration
    9. 11.9. Lab answers
  20. Chapter 12. A practical interlude
    1. 12.1. Defining the task
    2. 12.2. Finding the commands
    3. 12.3. Learning to use the commands
    4. 12.4. Tips for teaching yourself
    5. 12.5. Lab
    6. 12.6. Lab answer
  21. Chapter 13. Remote control: one-to-one, and one-to-many
    1. 13.1. The idea behind remote PowerShell
    2. 13.2. WinRM overview
    3. 13.3. Using Enter-PSSession and Exit-PSSession for one-to-one remoting
    4. 13.4. Using Invoke-Command for one-to-many remoting
    5. 13.5. Differences between remote and local commands
      1. 13.5.1. Invoke-Command vs. -computerName
      2. 13.5.2. Local vs. remote processing
      3. 13.5.3. Deserialized objects
    6. 13.6. But wait, there’s more
    7. 13.7. Remote options
    8. 13.8. Common points of confusion
    9. 13.9. Lab
    10. 13.10. Further exploration
    11. 13.11. Lab answers
  22. Chapter 14. Using Windows Management Instrumentation and CIM
    1. 14.1. WMI essentials
    2. 14.2. The bad news about WMI
    3. 14.3. Exploring WMI
    4. 14.4. Choose your weapon: WMI or CIM
    5. 14.5. Using Get-WmiObject
    6. 14.6. Using Get-CimInstance
    7. 14.7. WMI documentation
    8. 14.8. Common points of confusion
    9. 14.9. Lab
    10. 14.10. Further exploration
    11. 14.11. Lab answers
  23. Chapter 15. Multitasking with background jobs
    1. 15.1. Making PowerShell do multiple things at the same time
    2. 15.2. Synchronous vs. asynchronous
    3. 15.3. Creating a local job
    4. 15.4. WMI, as a job
    5. 15.5. Remoting, as a job
    6. 15.6. Getting job results
    7. 15.7. Working with child jobs
    8. 15.8. Commands for managing jobs
    9. 15.9. Scheduled jobs
    10. 15.10. Common points of confusion
    11. 15.11. Lab
    12. 15.12. Lab answers
  24. Chapter 16. Working with many objects, one at a time
    1. 16.1. Automation for mass management
    2. 16.2. The preferred way: “batch” cmdlets
    3. 16.3. The CIM/WMI way: invoking methods
    4. 16.4. The backup plan: enumerating objects
    5. 16.5. Common points of confusion
      1. 16.5.1. Which way is the right way?
      2. 16.5.2. WMI methods vs. cmdlets
      3. 16.5.3. Method documentation
      4. 16.5.4. ForEach-Object confusion
    6. 16.6. Lab
    7. 16.7. Lab answers
  25. Chapter 17. Security alert!
    1. 17.1. Keeping the shell secure
    2. 17.2. Windows PowerShell security goals
    3. 17.3. Execution policy and code signing
      1. 17.3.1. Execution policy settings
      2. 17.3.2. Digital code signing
    4. 17.4. Other security measures
    5. 17.5. Other security holes?
    6. 17.6. Security recommendations
    7. 17.7. Lab
  26. Chapter 18. Variables: a place to store your stuff
    1. 18.1. Introduction to variables
    2. 18.2. Storing values in variables
    3. 18.3. Using variables: fun tricks with quotes
    4. 18.4. Storing many objects in a variable
      1. 18.4.1. Working with single objects in a variable
      2. 18.4.2. Working with multiple objects in a variable
      3. 18.4.3. Other ways to work with multiple objects
      4. 18.4.4. Unrolling properties and methods in PowerShell v3
    5. 18.5. More tricks with double quotes
    6. 18.6. Declaring a variable’s type
    7. 18.7. Commands for working with variables
    8. 18.8. Variable best practices
    9. 18.9. Common points of confusion
    10. 18.10. Lab
    11. 18.11. Further exploration
    12. 18.12. Lab answers
  27. Chapter 19. Input and output
    1. 19.1. Prompting for, and displaying, information
    2. 19.2. Read-Host
    3. 19.3. Write-Host
    4. 19.4. Write-Output
    5. 19.5. Other ways to write
    6. 19.6. Lab
    7. 19.7. Further exploration
    8. 19.8. Lab answers
  28. Chapter 20. Sessions: remote control with less work
    1. 20.1. Making PowerShell remoting a bit easier
    2. 20.2. Creating and using reusable sessions
    3. 20.3. Using sessions with Enter-PSSession
    4. 20.4. Using sessions with Invoke-Command
    5. 20.5. Implicit remoting: importing a session
    6. 20.6. Using disconnected sessions
    7. 20.7. Lab
    8. 20.8. Further exploration
    9. 20.9. Lab answers
  29. Chapter 21. You call this scripting?
    1. 21.1. Not programming, more like batch files
    2. 21.2. Making commands repeatable
    3. 21.3. Parameterizing commands
    4. 21.4. Creating a parameterized script
    5. 21.5. Documenting your script
    6. 21.6. One script, one pipeline
    7. 21.7. A quick look at scope
    8. 21.8. Lab
    9. 21.9. Lab answer
  30. Chapter 22. Improving your parameterized script
    1. 22.1. Starting point
    2. 22.2. Getting PowerShell to do the hard work
    3. 22.3. Making parameters mandatory
    4. 22.4. Adding parameter aliases
    5. 22.5. Validating parameter input
    6. 22.6. Adding the warm and fuzzies with verbose output
    7. 22.7. Lab
    8. 22.8. Lab answer
  31. Chapter 23. Advanced remoting configuration
    1. 23.1. Using other endpoints
    2. 23.2. Creating custom endpoints
      1. 23.2.1. Creating the session configuration
      2. 23.2.2. Registering the session
    3. 23.3. Enabling multihop remoting
    4. 23.4. Digging deeper into remoting authentication
      1. 23.4.1. Defaults for mutual authentication
      2. 23.4.2. Mutual authentication via SSL
      3. 23.4.3. Mutual authentication via TrustedHosts
    5. 23.5. Lab
    6. 23.6. Lab answer
  32. Chapter 24. Using regular expressions to parse text files
    1. 24.1. The purpose of regular expressions
    2. 24.2. A regex syntax primer
    3. 24.3. Using regex with -Match
    4. 24.4. Using regex with Select-String
    5. 24.5. Lab
    6. 24.6. Further exploration
    7. 24.7. Lab answers
  33. Chapter 25. Additional random tips, tricks, and techniques
    1. 25.1. Profiles, prompts, and colors: customizing the shell
      1. 25.1.1. PowerShell profiles
      2. 25.1.2. Customizing the prompt
      3. 25.1.3. Tweaking colors
    2. 25.2. Operators: -as, -is, -replace, -join, -split, -in, -contains
      1. 25.2.1. -as and -is
      2. 25.2.2. -replace
      3. 25.2.3. -join and -split
      4. 25.2.4. -contains and -in
    3. 25.3. String manipulation
    4. 25.4. Date manipulation
    5. 25.5. Dealing with WMI dates
    6. 25.6. Setting default parameter values
    7. 25.7. Playing with script blocks
    8. 25.8. More tips, tricks, and techniques
  34. Chapter 26. Using someone else’s script
    1. 26.1. The script
    2. 26.2. It’s a line-by-line examination
    3. 26.3. Lab
    4. 26.4. Lab answer
  35. Chapter 27. Never the end
    1. 27.1. Ideas for further exploration
    2. 27.2. “Now that I’ve read the book, where do I start?”
    3. 27.3. Other resources you’ll grow to love
  36. Chapter 28. PowerShell cheat sheet
    1. 28.1. Punctuation
    2. 28.2. Help file
    3. 28.3. Operators
    4. 28.4. Custom property and column syntax
    5. 28.5. Pipeline parameter input
    6. 28.6. When to use $_
  37. Appendix. Review labs
    1. Review lab 1: chapters 1–8
      1. Hints:
      2. Task 1
      3. Task 2
      4. Task 3
      5. Task 4
      6. Task 5
      7. Task 6
      8. Task 7
      9. Task 8
      10. Task 9
      11. Task 10
      12. Task 11
      13. Task 12
      14. Task 13
      15. Task 14
      16. Task 15
      17. Task 16
      18. Task 17
      19. Task 18
    2. Review lab 2: chapters 1–14
      1. Hints:
      2. Task 1
      3. Task 2
      4. Task 3
      5. Task 4
      6. Task 5
      7. Task 6
      8. Task 7
      9. Task 8
      10. Task 9
      11. Task 10
      12. Task 11
      13. Task 12
      14. Task 13
      15. Task 14
      16. Task 15
      17. Task 16
      18. Task 17
    3. Review lab 3: chapters 1–19
      1. Task 1
      2. Task 2
      3. Task 3
      4. Task 4
      5. Task 5
    4. Answers
      1. Review Lab 1
      2. Review Lab 2
      3. Review Lab 3
  38. List of Figures
  39. List of Tables
  40. List of Listings