You are previewing Mastering Windows PowerShell Scripting.
O'Reilly logo
Mastering Windows PowerShell Scripting

Book Description

Master the art of automating and managing your Windows environment using PowerShell

In Detail

PowerShell is quickly becoming the de facto standard for scripting in Microsoft Windows environments. It enables the automation of otherwise complex tasks, providing interactivity between different products.

Beginning with PowerShell fundamentals, this book progresses by imparting the advanced skills required to master automation. You will learn how to deal with data and operations on various data types and structures, and see the techniques of data manipulation and parsing. Moving on, you will learn about the usage of regular expressions and comparison operators. Error handling techniques will enable you to identify and eliminate errors. This book also provides best practices for scripting and techniques to reduce the amount of code required to complete tasks.

By the end of this book, you will be able to create a variety of PowerShell scripts and successfully automate your environment and become the go-to person.

What You Will Learn

  • Utilize variables, hashes, and arrays to store data

  • Parse and manipulate different data types

  • Optimize code through the use of functions, switches, and looping structures

  • Create and implement regular expressions in PowerShell scripts

  • Leverage session-based remote management

  • Manage files, folders, and registries through the use of PowerShell

  • Discover the best practices to manage Microsoft systems

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. Mastering Windows PowerShell Scripting
      1. Table of Contents
      2. Mastering Windows PowerShell Scripting
      3. Credits
      4. Foreword
      5. About the Author
      6. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
          3. Instant updates on new Packt books
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      9. 1. Variables, Arrays, and Hashes
        1. Variables
          1. Objects stored in variables
        2. Arrays
          1. Single-dimension arrays
          2. Jagged arrays
          3. Updating array values
        3. Hashes
        4. Deciding the best container for your scripts
        5. Summary
      10. 2. Data Parsing and Manipulation
        1. String manipulation
          1. Replacing and splitting strings
          2. Counting and trimming strings
          3. The Trim method
          4. The Substring method
          5. The string true and false methods
        2. Number manipulation and parsing
          1. Formatting numbers
          2. Formatting bytes
        3. Date and time manipulation
        4. Forcing data types
        5. Piping variables
        6. Summary
      11. 3. Comparison Operators
        1. Comparison operator basics
        2. Equal and not equal comparison
        3. Greater than and less than comparison
        4. Contains, like, and match operators
        5. And / OR comparison operators
        6. Best practices for comparison operators
        7. Summary
      12. 4. Functions, Switches, and Loops Structures
        1. Functions
        2. Looping structures
        3. Switches
        4. Combining the use of functions, switches, and loops
        5. Best practices for functions, switches, and loops
          1. Best practices for functions
          2. Best practices for looping structures and switches
        6. Summary
      13. 5. Regular Expressions
        1. Getting started with regular expressions
        2. Regular expression grouping constructs and ranges
        3. Regular expression quantifiers
        4. Regular expression anchors
        5. Regular expressions examples
        6. Summary
      14. 6. Error and Exception Handling and Testing Code
        1. Error and exception handling – parameters
        2. Error and exception handling – Try/Catch
          1. Error and exception handling –Try/Catch with parameters
        3. Error and exception handling – legacy exception handling
        4. Methodologies for testing code
          1. Testing the –WhatIf argument
          2. Testing the frequency
          3. Hit testing containers
          4. Don't test in production
        5. Summary
      15. 7. Session-based Remote Management
        1. Utilizing CIM sessions
          1. Creating a session
          2. Creating a session with session options
          3. Using sessions for remote management
          4. Removing sessions
        2. Summary
      16. 8. Managing Files, Folders, and Registry Items
        1. Registry provider
        2. Creating files, folders, and registry items with PowerShell
        3. Adding named values to registry keys
        4. Verifying files, folders, and registry items
        5. Copying and moving files and folders
        6. Renaming files, folders, registry keys, and named values
        7. Deleting files, folders, registry keys, and named values
        8. Summary
      17. 9. File, Folder, and Registry Attributes, ACLs, and Properties
        1. Retrieving attributes and properties
        2. Viewing file and folder extended attributes
        3. Setting the mode and extended file and folder attributes
        4. Managing file, folder, and registry permissions
          1. Copying access control lists
          2. Adding and removing ACL rules
        5. Summary
      18. 10. Windows Management Instrumentation
        1. WMI structure
        2. Using WMI objects
        3. Searching for WMI classes
        4. Creating, modifying, and removing WMI property instances
          1. Creating property instances
          2. Modifying property instances
          3. Removing property instances
        5. Invoking WMI class methods
        6. Summary
      19. 11. XML Manipulation
        1. XML file structure
          1. Reading XML files
          2. Adding XML content
          3. Modifying XML content
          4. Removing XML content
        2. Summary
      20. 12. Managing Microsoft Systems with PowerShell
        1. Managing local users and groups
          1. Managing local users
          2. Managing local groups
          3. Querying for local users and groups
        2. Managing Windows services
        3. Managing Windows processes
        4. Installing Windows features and roles
        5. Summary
      21. 13. Automation of the Environment
        1. Invoking programs for automation
        2. Using desired state configuration
          1. Authoring phase
          2. Staging and remediation phase
        3. Detecting and restoring drifting configurations
        4. Summary
      22. 14. Script Creation Best Practices and Conclusion
        1. Best practices for script management
        2. # commenting headers
          1. Commenting code
        3. Best practices for script creation
          1. Script structure
          2. Other important best practices for script creation
          3. Controlling source files
        4. Best practices for software automation
        5. Summary
          1. Mastering Windows PowerShell Scripting – conclusion
          2. Staying connected with the author
      23. Index