You are previewing Windows PowerShell™ 2 For Dummies®.
O'Reilly logo
Windows PowerShell™ 2 For Dummies®

Book Description

Prepare for the future of Microsoft automation with this no-nonsense guide

Windows PowerShell 2 is the scripting language that enables automation within the Windows operating system. Packed with powerful new features, this latest version is complex, and Windows PowerShell 2 For Dummies is the perfect guide to help system administrators get up to speed.

Written by a Microsoft MVP with direct access to the program managers and developers, this book covers every new feature of Windows PowerShell 2 in a friendly, easy-to-follow format.

  • Windows PowerShell 2 is the updated scripting language that enables system administrators to automate Windows operating systems

  • System administrators with limited scripting experience will find this book helps them learn the fundamentals of Windows PowerShell 2 quickly and easily

  • Translates the jargon and complex syntax of Windows PowerShell 2

  • Covers script debugging improvements, the ability to invoke commands remotely, and the new user interface

  • Uses real-world applications to clarify the theory, fundamentals, and techniques of the scripting language

  • Written by a Microsoft MVP with direct access to the developers of Windows PowerShell 2

  • Windows PowerShell 2 For Dummies makes this tool easily accessible to administrators of every experience level.

    Table of Contents

    1. Copyright
    2. About the Author
    3. Author's Acknowledgments
    4. Publisher's Acknowledgments
    5. Introduction
      1. About This Book
      2. Conventions Used in This Book
      3. What You're Not to Read
      4. Foolish Assumptions
      5. How This Book Is Organized
        1. Part I: Getting a Bird's-Eye View of PowerShell 2
        2. Part II: PowerShell's Basic Structure and Syntax
        3. Part III: Complex Data Description and Sharing
        4. Part IV: Controlling Where and How You Operate PowerShell
        5. Part V: Real-World Windows Administration Using PowerShell
        6. Part VI: Configuring and Reporting Via PowerShell
        7. Part VII: The Part of Tens
        8. Icons Used in This Book
        9. What's on the Web Site
        10. Where to Go from Here
    6. I. Getting a Bird's-Eye View of PowerShell 2
      1. 1. The Windows PowerShell Rap Sheet
        1. 1.1. Addressing the Need for a Powerful, Windows-Focused Scripting Language
          1. 1.1.1. Watching Monad morph into PowerShell
          2. 1.1.2. A little bit on Windows PowerShell 1.0
        2. 1.2. Windows PowerShell 2, the Next Evolution
        3. 1.3. Installing Windows PowerShell 2
        4. 1.4. Firing up the Windows PowerShell Command Shell
        5. 1.5. Going GUI: The Windows PowerShell Integrated Shell Environment (ISE)
      2. 2. Customizing and Shortcutting the Environment
        1. 2.1. Personalizing the Look and Feel of the Command Shell
          1. 2.1.1. Adding color to your world
          2. 2.1.2. Getting size-specific with your windows
          3. 2.1.3. A window by any other name. . .
        2. 2.2. Changing Your PowerShell Profile
        3. 2.3. Making the Windows PowerShell ISE Work for You
          1. 2.3.1. Customizing the ISE
          2. 2.3.2. Adding your own functions to the ISE menu
        4. 2.4. Creating Aliases
        5. 2.5. Deleting Aliases
        6. 2.6. Accessing the Alias Drive
        7. 2.7. Creating Persistent Aliases
        8. 2.8. Getting to Know Tab Expansion
      3. 3. A Pinch of Shell, a Pound of Power
        1. 3.1. Getting a Taste of Windows PowerShell
        2. 3.2. Creating Your First Script
        3. 3.3. Breaking Down Your First Script
        4. 3.4. Sneaking a Peek at Complex Scripts
        5. 3.5. Examining the Nuts and Bolts of the Complist Script
    7. II. PowerShell's Basic Structure and Syntax
      1. 4. Shelling Out Commands and Scripts
        1. 4.1. Cmdlets: The Little Commands That Could!
          1. 4.1.1. Putting Cmdlets under a microscope
          2. 4.1.2. Checking out existing Cmdlets
          3. 4.1.3. Making Cmdlets understand you
        2. 4.2. One Shell to Rule Them All
          1. 4.2.1. Windows Shell scripts
          2. 4.2.2. Windows Scripting Host
      2. 5. When Dollars Turn into Variables
        1. 5.1. Discovering Variables: They Vary Very Much
        2. 5.2. Getting to Know Data Types
          1. 5.2.1. Dealing with data types
          2. 5.2.2. Explicitly defining the data type
          3. 5.2.3. Casting values
        3. 5.3. Constant and Read-Only Variables
        4. 5.4. Understanding Automatic Variables
        5. 5.5. Working with Objects through Variables
      3. 6. A Bit of Logic to Save the Day
        1. 6.1. A Logic Primer
        2. 6.2. Branching Using If/Else
        3. 6.3. Using the Switch Statement
        4. 6.4. Doing It Over and Over and Over Again with Loops
          1. 6.4.1. Looping with For
          2. 6.4.2. Using Foreach to loop through collections
          3. 6.4.3. Looping for a While
          4. 6.4.4. Running a loop at least once with Do While
          5. 6.4.5. Taking a look at Do Until
          6. 6.4.6. Avoiding loop pitfalls
      4. 7. Working on a Pipeline
        1. 7.1. Using Pipelines to Streamline Your Commands
        2. 7.2. Stringing Commands Together
        3. 7.3. Getting the Right Output
    8. III. Complex Data Description and Sharing
      1. 8. Working with Windows Management Instrumentation
        1. 8.1. Getting Familiar with Windows Management Instrumentation
          1. 8.1.1. Examining the WMI architecture
          2. 8.1.2. Poking around in WMI namespaces
          3. 8.1.3. Securing WMI
        2. 8.2. Making Windows PowerShell Interact with WMI
        3. 8.3. Using SQL Syntax in WMI to Get WQL
        4. 8.4. Harnessing the Power of WMI
          1. 8.4.1. Querying service status
          2. 8.4.2. Looking for event log entries
        5. 8.5. Changing WMI Authentication Levels
        6. 8.6. Pretending to Be Someone Else Using Impersonation
        7. 8.7. Using the New WMI Cmdlets
          1. 8.7.1. Making things happen with Invoke-WMIMethod
          2. 8.7.2. Deleting objects using Remove-WmiObject
          3. 8.7.3. Setting WMI properties using Set-WmiInstance
      2. 9. Bringing Strings into the Limelight
        1. 9.1. Taking Your First Look at Strings
          1. 9.1.1. Differentiating between empty and null strings
          2. 9.1.2. Creating literal strings
          3. 9.1.3. Simplifying using Here-Strings
        2. 9.2. Performing String Surgery
          1. 9.2.1. Combining strings
          2. 9.2.2. Combining strings with nonstrings
          3. 9.2.3. Splitting strings
          4. 9.2.4. Snipping off a piece of a string
          5. 9.2.5. Performing string substitutions
        3. 9.3. Working with String Positions
        4. 9.4. Changing the Case of Strings
        5. 9.5. Using Regular Expressions
          1. 9.5.1. Creating the simplest RegEx using literal characters
          2. 9.5.2. Performing more dynamic searches using character sets
          3. 9.5.3. Using modifiers to define optional or repeating sequences
          4. 9.5.4. Using anchors to maintain position
          5. 9.5.5. Coming up with alternatives
          6. 9.5.6. Making use of RegEx in Windows PowerShell
      3. 10. I'll Take Numbers for $100, Please
        1. 10.1. Putting Numeric Data Types under a Microscope
          1. 10.1.1. Having a look at integral data types
          2. 10.1.2. Getting precise using nonintegral data types
        2. 10.2. Doing Some Calculations
          1. 10.2.1. Adding things up
          2. 10.2.2. Reducing values with subtraction
          3. 10.2.3. Expanding through multiplication
          4. 10.2.4. Reducing through division
        3. 10.3. Rounding Off Values
        4. 10.4. Creating Random Numbers
        5. 10.5. Converting Numbers
        6. 10.6. Watching Out for Overflow
      4. 11. Grouping Data Using Arrays and Hash Tables
        1. 11.1. Taking an In-Depth Look at Arrays
        2. 11.2. Creating and Using Arrays
          1. 11.2.1. Accessing array elements
          2. 11.2.2. Looping through arrays
        3. 11.3. Growing Arrays Dynamically
        4. 11.4. Creating Multidimensional Arrays
        5. 11.5. Finding Other Uses for Arrays
        6. 11.6. Working with Hash Tables: The Array's Useful Cousin
          1. 11.6.1. Creating and using hash tables
          2. 11.6.2. Modifying hash tables
          3. 11.6.3. Looping through hash tables
      5. 12. Readin' and Writin' Files
        1. 12.1. Having Some Fun with the File System
          1. 12.1.1. Moving around the file system
          2. 12.1.2. Managing directories
            1. 12.1.2.1. Creating directories
            2. 12.1.2.2. Deleting directories
            3. 12.1.2.3. Copying directories
            4. 12.1.2.4. Moving directories
            5. 12.1.2.5. Renaming directories
          3. 12.1.3. Manipulating files in the file system
        2. 12.2. Reading Text Files
        3. 12.3. Writing Files
        4. 12.4. Working with XML
          1. 12.4.1. Reading and writing XML files
          2. 12.4.2. Saving objects in XML files
        5. 12.5. Working with HTML
      6. 13. Going On a Date with PowerShell
        1. 13.1. Going On Your First Date
          1. 13.1.1. Getting the date and time in a specific format
          2. 13.1.2. Creating your own dates
        2. 13.2. Using Date Math (It's Not Just for Nerds)
          1. 13.2.1. Calculating time differences
          2. 13.2.2. Looking into the future
          3. 13.2.3. Checking whether it's daylight saving time
        3. 13.3. Dealing with Time Zones
          1. 13.3.1. Standardizing with Coordinated Universal Time
          2. 13.3.2. Using the TimeZone class
    9. IV. Controlling Where and How You Operate PowerShell
      1. 14. Using Functions to Divide and Conquer
        1. 14.1. Reusing Code Using Functions
          1. 14.1.1. Creating your first function
          2. 14.1.2. Defining parameters
            1. 14.1.2.1. Defining more than one parameter
            2. 14.1.2.2. Working with default parameters
          3. 14.1.3. Returning values
        2. 14.2. Using Scope
          1. 14.2.1. Understanding scope rules
          2. 14.2.2. Watching out for name overlap
          3. 14.2.3. Defining functions in Global scope
        3. 14.3. Creating Your Own Cmdlets — Advanced Functions!
          1. 14.3.1. Understanding the structure of Advanced Functions
          2. 14.3.2. Defining attributes
          3. 14.3.3. Defining parameters
          4. 14.3.4. Using methods
          5. 14.3.5. Running Advanced Functions
          6. 14.3.6. Finding uses for Advanced Functions
      2. 15. PowerShell Ninjas: Running Jobs Remotely or in the Background
        1. 15.1. Using Background Jobs
          1. 15.1.1. Enabling WinRM
          2. 15.1.2. Starting a new job
          3. 15.1.3. Getting results
          4. 15.1.4. Waiting for a job
          5. 15.1.5. Terminating a job
          6. 15.1.6. Bringing a job to a grinding halt
        2. 15.2. Running Commands Remotely
          1. 15.2.1. Using Windows PowerShell everywhere
          2. 15.2.2. Getting what you need for remote commands
          3. 15.2.3. Speaking PowerShell with a different computer
          4. 15.2.4. Invoking commands remotely
          5. 15.2.5. Creating a persistent connection
          6. 15.2.6. Running remote background jobs
          7. 15.2.7. Understanding policies, profiles, and precedence
      3. 16. Making Your Script Speak Different Languages
        1. 16.1. Seeing the Importance of Internationalizing Scripts
        2. 16.2. Giving Your Scripts Different Tongues
          1. 16.2.1. Using new internationalization features
          2. 16.2.2. Understanding cultures
          3. 16.2.3. Putting it all together
        3. 16.3. Sharing Scripts with Others
      4. 17. Smashing Those Bugs
        1. 17.1. Finding Out Where the Bugs Come From
          1. 17.1.1. Guarding against unexpected input
          2. 17.1.2. Watching out for incorrect logic
          3. 17.1.3. Expecting the unexpected: System errors
        2. 17.2. Understanding the Debugging Process
        3. 17.3. Working On Your Defense
        4. 17.4. Working with Debugging Tools
          1. 17.4.1. Working with breakpoints
          2. 17.4.2. Setting fancier breakpoints
          3. 17.4.3. Issuing debugger commands
          4. 17.4.4. Listing all breakpoints
          5. 17.4.5. Disabling and enabling breakpoints
          6. 17.4.6. Removing breakpoints
    10. V. Real-World Windows Administration Using PowerShell
      1. 18. Mission Control: All Systems Go
        1. 18.1. Monitoring Drive Space
        2. 18.2. Converting to Windows Management Infrastructure from System.IO.DriveInfo
        3. 18.3. Managing Windows Services
          1. 18.3.1. Controlling services
          2. 18.3.2. Configuring services
        4. 18.4. Checking Your Event Logs
        5. 18.5. Querying EventLogs Using WMI
      2. 19. Taming the Windows Registry
        1. 19.1. Following the Registry Tree
        2. 19.2. Connecting to the Windows Registry
          1. 19.2.1. Navigating the registry by using the PowerShell drives
          2. 19.2.2. Using Microsoft.Win32.RegistryKey to access the registry
        3. 19.3. Reading Keys and Values
        4. 19.4. Writing Keys and Values
          1. 19.4.1. Writing keys and values using the PSDrive
          2. 19.4.2. Writing registry values using Microsoft.Win32.RegistryKey
        5. 19.5. Renaming and Deleting Registry Keys and Values
          1. 19.5.1. Renaming and deleting registry keys and Values using PSDrive
          2. 19.5.2. Using Microsoft.Win32.RegistryKey to delete registry keys and values
      3. 20. Reaching Out to Active Directory
        1. 20.1. A Really Brief Active Directory Primer
        2. 20.2. Connecting to Active Directory
        3. 20.3. Querying for Objects and Attributes
          1. 20.3.1. Creating your LDAP filter
            1. 20.3.1.1. Defining a simple filter
            2. 20.3.1.2. Using wildcards
            3. 20.3.1.3. Negating the filter
            4. 20.3.1.4. Combining filter terms
            5. 20.3.1.5. Looking up userAccountControl properties
          2. 20.3.2. Dynamically obtaining a user's distinguishedName
        4. 20.4. Modifying Object Attributes
        5. 20.5. Updating Group Membership
        6. 20.6. Getting to the Raw ADSI Object Using psbase
      4. 21. PowerShell Lockdown
        1. 21.1. PowerShell Security Features
          1. 21.1.1. Getting rid of the current directory loophole
          2. 21.1.2. Stopping the double-click blues
          3. 21.1.3. Protecting through ExecutionPolicy
        2. 21.2. Generating a Code-Signing Certificate
          1. 21.2.1. Creating a self-signed certificate
          2. 21.2.2. Requesting a certificate from your Enterprise CA
        3. 21.3. Browsing the Certificate Store
        4. 21.4. Signing Your Scripts
        5. 21.5. Managing the Windows Firewall
          1. 21.5.1. Defining globally open ports
          2. 21.5.2. Listing firewall services
          3. 21.5.3. Allowing applications to get through
      5. 22. Converting Your Old Scripts: Out with the Old, In with the New
        1. 22.1. Converting a Windows Shell Script to Windows PowerShell
          1. 22.1.1. Echoing to the screen
          2. 22.1.2. Using conditional statements
          3. 22.1.3. Migrating that FOR command
            1. 22.1.3.1. Performing some action for each line in a text file
            2. 22.1.3.2. Using a FOR loop to split text
        2. 22.2. Converting a Windows Scripting Host Script to Windows PowerShell
          1. 22.2.1. Comparing the basics
          2. 22.2.2. Working with COM objects
          3. 22.2.3. Understanding the difference between CreateObject and GetObject
          4. 22.2.4. Handling I/O
          5. 22.2.5. Working with ActiveX Data Objects (ADO)
          6. 22.2.6. Leveraging ADO.NET to your advantage
    11. VI. Configuring and Reporting Via PowerShell
      1. 23. Controlling Your Network Configuration
        1. 23.1. Managing Your Network Settings
          1. 23.1.1. Familiarizing yourself with Win32_NetworkAdapterConfiguration
          2. 23.1.2. Retrieving your TCP/IP settings
          3. 23.1.3. Manipulating your TCP/IP settings
            1. 23.1.3.1. Troubleshooting connectivity
            2. 23.1.3.2. Changing TCP/IP settings
        2. 23.2. Managing Your Windows Firewall
          1. 23.2.1. Getting to know the Windows Firewall COMmander
          2. 23.2.2. Enabling and disabling the Windows Firewall
          3. 23.2.3. Making yourself visible
          4. 23.2.4. Getting a list of all authorized applications
          5. 23.2.5. Getting a list of all globally open ports
          6. 23.2.6. Using the big reset button
      2. 24. Managing Your Hardware
        1. 24.1. Polling Your Hardware
          1. 24.1.1. Finding out what hardware you have
            1. 24.1.1.1. Creating a hardware inventory script
            2. 24.1.1.2. Detecting whether the computer is a laptop
            3. 24.1.1.3. Finding any connected USB disk drives and memory sticks
          2. 24.1.2. Checking hardware state
        2. 24.2. Controlling Your Printers
          1. 24.2.1. Connecting to a shared network printer
          2. 24.2.2. Disconnecting a shared network printer
          3. 24.2.3. Setting the default printer
          4. 24.2.4. Checking up on printer state
          5. 24.2.5. Keeping an eye on the printer queue
      3. 25. Making Reporting Easy
        1. 25.1. Using Built-In Reporting Cmdlets
        2. 25.2. Generating Reports
          1. 25.2.1. Customizing tabular output with Format-Table
          2. 25.2.2. Setting column width in Format-Table
          3. 25.2.3. Using an interactive data table
          4. 25.2.4. Preparing data for other reporting tools
          5. 25.2.5. Making your data table ready
        3. 25.3. Making Reports Pretty
        4. 25.4. Formatting Using Cascading Style Sheets
        5. 25.5. Using Third-Party Reporting Tools
    12. VII. The Part of Tens
      1. 26. The Ten Most Important Cmdlets
        1. 26.1. Getting Help with Get-Help
        2. 26.2. Getting to Know Your Objects with Get-Member
        3. 26.3. Navigating with Set-Location
        4. 26.4. Reading Text Files with Get-Content
        5. 26.5. Writing to a File with Out-File
        6. 26.6. Leveraging WMI with Get-WMIObject
        7. 26.7. Creating New Objects with New-Object
        8. 26.8. Getting Picky with Select-Object
        9. 26.9. Going Through Collections with Foreach-Object
        10. 26.10. Controlling the Pipeline with Where-Object
      2. 27. Ten Common PowerShell Mistakes
        1. 27.1. Forgetting to Change the Execution Policy
        2. 27.2. Using Commas to Separate Parameters When Calling a Function
        3. 27.3. Defining Functions After You Use Them
        4. 27.4. Treating Pipeline Data as Strings
        5. 27.5. Forgetting to Cast Variables as a String
        6. 27.6. Using Incorrect Comparison Operators
        7. 27.7. Trying to Do Too Much in One Pipeline
        8. 27.8. Forgetting About Variable Scope
        9. 27.9. Not Using the Debugger
        10. 27.10. Not Using .NET Classes When Available
    13. BC1. Handling Exceptions
      1. BC.1. Handling Errors the Old-Fashioned Way
      2. BC.2. Understanding Exceptions
      3. BC.3. Trapping Exceptions
      4. BC.4. Throwing Exceptions