Windows PowerShell in Action, Third Edition
Bruce Payette and Richard Siddaway
  • September 2017
  • ISBN 9781633430297
  • 904 pages
  • printed in black & white

This comprehensive guide to PowerShell just gets better with every revision!

Wayne Boaz, Nike

GET MORE WITH MANNING

An eBook copy of the previous edition, Windows PowerShell in Action, Second Edition, is included at no additional cost. It will be automatically added to your Manning Bookshelf within 24 hours of purchase.


Windows PowerShell in Action, Third Edition is an end-to-end revision of the definitive guide to PowerShell. Written by language designer Bruce Payette and MVP Richard Siddaway, this rich book offers a crystal-clear introduction to the language along with its essential everyday use cases. Beyond the basics, you'll find detailed examples on deep topics like performance, module architecture, and parallel execution.

Table of Contents detailed table of contents

1. Welcome to PowerShell

1.1. What is PowerShell?

1.1.1. Shells, command lines, and scripting languages

1.2. PowerShell example code

1.2.1. Navigation and basic operations

1.2.2. Basic expressions and variables

1.2.3. Processing data

1.2.4. Flow-control statements

1.2.5. Scripts and functions

1.2.6. Remote administration

1.3. Core concepts

1.3.1. Command concepts and terminology

1.3.2. Commands and cmdlets

1.3.3. Command categories

1.3.4. Aliases and elastic syntax

1.4. Parsing the PowerShell language

1.4.1. How PowerShell parses

1.4.2. Quoting

1.4.3. Expression-mode and command-mode parsing

1.4.4. Statement termination

1.4.5. Comment syntax in PowerShell

1.5. How the pipeline works

1.5.1. Pipelines and streaming behavior

1.5.2. Parameters and parameter binding

1.6. Formatting and output

1.6.1. The formatting cmdlets

1.6.2. The outputter cmdlets

1.7. Summary

2. Working with types

2.1. Type management in the wild, wild West

2.1.1. What the Heck are Types and Classes?

2.1.2. PowerShell: a type-promiscuous language

2.1.3. The type system and type adaptation

2.1.4. Finding the Available Types

2.2. Basic types and literals

2.2.1. String literals

2.2.2. Numbers and numeric literals

2.3. Collections: dictionaries and hashtables

2.3.1. Creating and inspecting hashtables

2.3.2. Ordered hash tables

2.3.3. Modifying and manipulating hashtables

2.3.4. Hashtables as reference types

2.4. Collections: arrays and sequences

2.4.1. Collecting pipeline output as an array

2.4.2. Array indexing

2.4.3. Polymorphism in arrays

2.4.4. Arrays as reference types

2.4.5. Singleton Arrays and Empty Arrays

2.5. Type literals

2.5.1. Type name aliases

2.5.2. Generic type literals

2.5.3. Accessing static members with type literals

2.6. Type conversions

2.6.1. How type conversion works

2.6.2. PowerShell's type-conversion algorithm

2.6.3. Special type conversions in parameter binding

2.7. Summary

3. Operators and expressions

3.1. Arithmetic operators

3.1.1. The addition operator

3.1.2. The multiplication operator

3.1.3. Subtraction, division, and the modulus operator

3.2. The assignment operators

3.2.1. Multiple assignments

3.2.2. Multiple assignments with type qualifiers

3.2.3. Assignment operations as value expressions

3.3. Comparison operators

3.3.1. Scalar comparisons

3.3.2. Comparisons and case sensitivity

3.3.3. Using comparison operators with collections

3.4. Pattern matching and text manipulation

3.4.1. Wildcard patterns and the -like operator

3.4.2. Regular expressions

3.4.3. The -match operator

3.4.4. The -replace operator

3.4.5. The -join operator

3.4.6. The -split operator

3.5. Logical and bitwise operators

3.6. Where and ForEach methods

3.6.1. Where method

3.6.2. ForEach method

3.7. Summary

4. Advanced operators and variables

4.1. Operators for working with types

4.2. The unary operators

4.3. Grouping and subexpressions

4.3.1. Subexpressions $( …​ )

4.3.2. Array subexpressions @( …​ )

4.4. Array operators

4.4.1. The comma operator

4.4.2. The range operator

4.4.3. Array indexing and slicing

4.4.4. Using the range operator with arrays

4.4.5. Working with multidimensional arrays

4.5. Property and method operators

4.5.1. The dot operator

4.5.2. Static methods and the double-colon operator

4.5.3. Indirect method invocation

4.6. The format operator

4.7. Redirection and the redirection operators

4.8. Working with variables

4.8.1. Creating variables

4.8.2. Variable name syntax

4.8.3. Working with the variable cmdlets

4.8.4. Splatting a variable

4.9. Summary

5. Flow control in scripts

5.1. The conditional statement

5.2. Looping statements

5.2.1. The while loop

5.2.2. The do-while loop

5.2.3. The for loop

5.2.4. The foreach loop

5.3. Labels, break, and continue

5.4. The switch statement

5.4.1. Basic use of the switch statement

5.4.2. Using wildcard patterns with the switch statement

5.4.3. Using regular expressions with the switch statement

5.4.4. Processing files with the switch statement

5.4.5. Using the $switch loop enumerator in the switch statement

5.5. Flow control using cmdlets

5.5.1. The ForEach-Object cmdlet

5.5.2. The Where-Object cmdlet

5.6. Statements as values

5.7. A word about performance

5.8. Summary

6. PowerShell functions

6.1. Fundamentals of PowerShell functions

6.1.1. Passing arguments using $args

6.1.2. Example functions: ql and qs

6.2. Declaring formal parameters for a function

6.2.1. Mixing named and positional parameters

6.2.2. Adding type constraints to parameters

6.2.3. Handling variable numbers of arguments

6.2.4. Initializing function parameters with default values

6.2.5. Using switch parameters to define command switches

6.2.6. Switch parameters vs. Boolean parameters

6.3. Returning values from functions

6.3.1. Debugging problems in function output

6.3.2. The return statement

6.4. Using simple functions in a pipeline

6.4.1. Functions with begin, process, and end blocks

6.5. Managing function definitions in a session

6.6. Variable scoping in functions

6.6.1. Declaring variables

6.6.2. Using variable scope modifiers

6.7. Summary

7. Advanced functions and scripts

7.1. PowerShell scripts

7.1.1. Script execution policy

7.1.2. Passing arguments to scripts

7.1.3. Exiting scripts and the exit statement

7.1.4. Scopes and scripts

7.1.5. Managing your scripts

7.1.6. Running PowerShell scripts from other applications

7.2. Writing advanced functions and scripts

7.2.1. Specifying script and function attributes

7.2.2. The CmdletBinding attribute

7.2.3. The OutputType attribute

7.2.4. Specifying parameter attributes

7.2.5. Creating parameter aliases with the Alias attribute

7.2.6. Parameter validation attributes

7.3. Dynamic parameters and dynamicParam

7.3.1. Steps for adding a dynamic parameter

7.3.2. When should dynamic parameters be used?

7.4. Cmdlet default parameter values

7.4.1. Creating default values

7.4.2. Modifying default values

7.4.3. Using scriptblocks to determine default value

7.5. Documenting functions and scripts

7.5.1. Automatically generated help fields

7.5.2. Creating manual help content

7.5.3. Comment—based help

7.5.4. Tags used in documentation comments

7.6. Summary

8. Using and authoring modules

8.1. The role of a module system

8.1.1. Module roles in PowerShell

8.1.2. Module mashups: composing an application

8.2. Module basics

8.2.1. Module terminology

8.2.2. Modules are single—instance objects

8.3. Working with modules

8.3.1. Finding modules on the system

8.3.2. Loading a module

8.3.3. Removing a loaded module

8.4. Writing script modules

8.4.1. A quick review of scripts

8.4.2. Turning a script into a module

8.4.3. Controlling member visibility with Export—ModuleMember

8.4.4. Installing a module

8.4.5. How scopes work in script modules

8.4.6. Nested modules

8.5. Binary modules

8.5.1. Binary modules vs. snap—ins

8.5.2. Creating a binary module

8.5.3. Nesting binary modules in script modules

8.6. Summary

9. Module manifests and metadata

9.1. Module folder structure

9.2. Module manifest structure

9.3. Production manifest elements

9.3.1. Module identity

9.3.2. Runtime dependencies

9.4. Construction manifest elements

9.4.1. The loader manifest elements

9.4.2. Module component load order

9.5. Content manifest elements

9.6. Language restrictions in a manifest

9.7. Advanced module operations

9.7.1. The PSModuleInfo object

9.7.2. Using the PSModuleInfo methods

9.7.3. The defining module versus the calling module

9.7.4. Setting module properties from inside a script module

9.7.5. Controlling when modules can be unloaded

9.7.6. Running an action when a module is removed

9.8. Summary

10. Metaprogramming with scriptblocks and dynamic code

10.1. Scriptblock basics

10.1.1. Invoking commands

10.1.2. Getting CommandInfo objects

10.1.3. The scriptblock literal

10.1.4. Defining functions at runtime

10.2. Building and manipulating objects

10.2.1. Looking at members

10.2.2. Defining synthetic members

10.2.3. Using Add-Member to extend objects

10.2.4. Adding note properties with New-Object

10.3. Using the Select-Object cmdlet

10.4. Dynamic modules

10.4.1. Dynamic script modules

10.4.2. Closures in PowerShell

10.4.3. Creating custom objects from modules

10.5. Steppable pipelines

10.5.1. How steppable pipelines work

10.5.2. Creating a proxy command with steppable pipelines

10.6. A closer look at the type-system plumbing

10.6.1. Adding a property

10.6.2. Shadowing an existing property

10.7. Extending the PowerShell language

10.7.1. Little languages

10.7.2. Type extension

10.8. Building script code at runtime

10.8.1. The Invoke-Expression cmdlet

10.8.2. The ExecutionContext variable

10.8.3. The ExpandString() method

10.8.4. The InvokeScript() method

10.8.5. Mechanisms for creating scriptblocks

10.8.6. Creating functions using the function: drive

10.9. Compiling code with Add-Type

10.9.1. Defining a new .NET class: C#

10.9.2. Defining a new enum at runtime

10.9.3. Dynamic binary modules

10.10. Summary

11. PowerShell remoting

11.1. PowerShell remoting overview

11.1.1. Commands with built-in remoting

11.1.2. The PowerShell remoting subsystem

11.1.3. Enabling remoting

11.1.4. Additional setup steps for workgroup environments

11.1.5. Authenticating the connecting user

11.1.6. Enabling remoting in the enterprise

11.2. Applying PowerShell remoting

11.2.1. Basic remoting examples

11.2.2. Adding concurrency to the examples

11.2.3. Solving a real problem: multi-machine monitoring

11.3. PowerShell remoting sessions and persistent connections

11.3.1. Additional session attributes

11.3.2. Using the New-PSSession cmdlet

11.3.3. Interactive sessions

11.3.4. Managing PowerShell sessions

11.4. Implicit remoting

11.4.1. Using implicit remoting

11.4.2. How implicit remoting works

11.5. Considerations when running commands remotely

11.5.1. Remote session startup directory

11.5.2. Profiles and remoting

11.5.3. Issues running executables remotely

11.5.4. Using files and scripts

11.5.5. Using local variables in remote sessions

11.5.6. Reading and writing to the console

11.5.7. Remote output vs. local output

11.5.8. Processor architecture issues

11.6. Building custom remoting services

11.6.1. Working with custom configurations

11.6.2. Creating a custom configuration

11.6.3. Access controls and endpoints

11.6.4. Constraining a PowerShell session

11.7. Powershell Direct

11.8. Summary

12. PowerShell workflows

12.1. Workflow overview

12.1.1. Why workflows

12.1.2. Workflow Architecture

12.1.3. Your first workflow

12.1.4. Running a workflow

12.1.5. Cmdlets vs. Activities

12.1.6. Workflow restrictions

12.2. Workflow keywords

12.2.1. Parallel

12.2.2. Sequence

12.2.3. InlineScript

12.2.4. Foreach -parallel

12.3. Using workflows effectively

12.3.1. Workflow parameters

12.3.2. Variables in workflows

12.3.3. Nested workflows

12.4. Workflow cmdlets

12.4.1. Workflow execution options

12.4.2. Workflow sessions

12.4.3. Invoking as workflow

12.5. Summary

13. PowerShell Jobs

13.1. Background jobs in PowerShell

13.1.1. The job commands

13.1.2. Working with the job cmdlets

13.1.3. Working with multiple jobs

13.1.4. Starting jobs on remote computers

13.1.5. Running jobs in existing sessions

13.1.6. Job types

13.2. Workflows as jobs

13.3. Scheduled jobs

13.3.1. Creating scheduled jobs

13.3.2. Modifying a scheduled job

13.3.3. Managing scheduled jobs

13.4. Summary

14. Errors and exceptions

14.1. Error handling

14.1.1. ErrorRecords and the error stream

14.1.2. The $error variable and —ErrorVariable parameter

14.1.3. Determining if a command had an error

14.1.4. Controlling the actions taken on an error

14.2. Dealing with errors that terminate execution

14.2.1. The try/catch/finally statement

14.2.2. The throw statement

14.3. PowerShell and the event log

14.3.1. The EventLog cmdlets

14.3.2. Examining the PowerShell event log

14.3.3. Get-WinEvent

14.4. Summary

15. Debugging

15.1. Instrumenting your scripts

15.1.1. The Write cmdlets

15.1.2. Writing Events to the Event Log

15.1.3. Catching errors with strict mode

15.1.4. Static analysis of scripts

15.2. Capturing session output

15.2.1. Starting the transcript

15.2.2. What gets captured in the transcript

15.3. PowerShell script debugging features

15.3.1. The Set-PSDebug cmdlet

15.3.2. Nested prompts and the Suspend operation

15.4. Command line debugging

15.4.1. Working with breakpoint objects

15.4.2. Setting breakpoints on commands

15.4.3. Setting breakpoints on variable assignment

15.4.4. Debugger limitations and issues

15.5. Beyond scripts

15.5.1. Debugging PowerShell jobs

15.5.2. Debugging remote scripts

15.5.3. Debugging PowerShell runspaces

15.6. Summary

16. Working with providers, files and CIM

16.1. PowerShell providers

16.1.1. PowerShell core cmdlets

16.1.2. Working with PSDrives

16.1.3. Working with paths

16.1.4. The Registry provider

16.2. Files, text and XML

16.2.1. File processing

16.2.2. Unstructured text

16.2.3. XML structured text processing

16.2.4. Converting text output to objects

16.3. Accessing COM objects

16.4. Using CIM

16.4.1. The CIM cmdlets

16.4.2. CIM Sessions

16.5. Summary

17. Working with .NET and events

17.1. .NET and PowerShell

17.1.1. Using .NET from PowerShell

17.1.2. PowerShell and graphical user interfaces

17.2. Real time events

17.2.1. Foundations of event handling

17.2.2. Synchronous events

17.2.3. Asynchronous events

17.2.4. Working with asynchronous .NET events

17.2.5. Asynchronous event handling with scriptblocks

17.2.6. Automatic variables in the event handler

17.2.7. Dynamic modules and event handler state

17.2.8. Queued events and the Wait-Event cmdlet

17.2.9. Working with CIM events

17.2.10. Class-based CIM event registration

17.2.11. Engine events

17.2.12. Generating events in functions and scripts

17.2.13. Remoting and event forwarding

17.2.14. How eventing works

17.3. Summary

18. Desired State Configuration

18.1. DSC model and architecture

18.1.1. The need for configuration management

18.1.2. Desired State Configuration model

18.1.3. DSC architecture

18.2. Push mode to a single node

18.2.1. Create configuration

18.2.2. MOF file contents

18.2.3. Applying the configuration

18.2.4. Testing the configuration application

18.2.5. Viewing the current configuration

18.2.6. Removing a configuration

18.3. Pushing to multiple nodes

18.3.1. Parameterizing the computer name

18.3.2. Using configuration data

18.3.3. Configuration data and roles

18.3.4. Issues with push mode

18.4. DSC in pull mode

18.4.1. Pull server architecture

18.4.2. Creating a pull server

18.4.3. Publishing a MOF file

18.5. Configuring the Local Configuration Manager

18.5.1. LCM settings

18.5.2. Configuring LCM to use a pull server

18.6. Partial configurations

18.6.1. Partial configurations: yes or no

18.6.2. Pushing partial configurations

18.6.3. Pulling partial configurations

18.7. Summary

19. Classes in PowerShell

19.1. Writing Classes in PowerShell

19.1.1. Using Properties in a PowerShell class

19.1.2. Class Member Attributes

19.1.3. PowerShell enumerations

19.2. Methods in PowerShell Classes.

19.2.1. Method Basics

19.2.2. Static methods

19.2.3. Instance Methods

19.2.4. Method overloads

19.2.5. Hidden methods

19.2.6. Constructors in PowerShell classes

19.3. Extending existing classes

19.3.1. Creating a derived class

19.3.2. Overriding members on the base class

19.3.3. Extending .NET Classes

19.4. Classes, Modules, Using and Namespaces

19.5. Writing Class-based DSC resources

19.6. Summary

20. The PowerShell and Runspace APIs

20.1. PowerShell API Basics

20.1.1. Multi-command Pipelines

20.1.2. Building Pipelines Incrementally

20.1.3. Handling Execution Errors

20.1.4. Adding Scripts and Statements

20.2. Runspaces and the PowerShell API

20.2.1. Existing Runspaces and Isolated Execution

20.2.2. Creating Runspaces

20.2.3. Using Runspaces for Concurrency

20.3. Runspace Pools

20.4. Out-of-process Runspaces

20.5. Remote Runspaces

20.5.1. Sessions and Runspaces

20.5.2. Creating Remote Runspaces

20.6. Managing Runspaces

20.7. Summary

About the Technology

In 2006, Windows PowerShell reinvented the way administrators and developers interact with Windows. Today, PowerShell is required knowledge for Windows admins and devs. This powerful, dynamic language provides command-line control of the Windows OS and most Windows servers, such as Exchange and SCCM. And because it's a first-class .NET language, you can build amazing shell scripts and tools without reaching for VB or C#.

What's inside

  • The best end-to-end coverage of PowerShell available
  • PowerShell workflows
  • PowerShell classes
  • Writing modules and scripts
  • Desired State Configuration
  • Programming APIs and pipelines
  • Includes a detailed appendix on new PowerShell v6 features

About the reader

Written for intermediate-level developers and administrators.

About the authors

Bruce Payette is codesigner and principal author of the PowerShell language. Richard Siddaway is a longtime PowerShell MVP, author, speaker, and blogger.

Buy
combo $59.99 pBook + eBook + liveBook
eBook $47.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks

Excellent coverage of the new features in PowerShell. Recommended for all levels of users.

Lincoln Bovee, Proto Labs

Deep technical discussions of the inner workings of PowerShell. Many useful examples. Up to date!

Dr. Edgar Knapp, ISIS Papyrus Europe

If you're serious about PowerShell, you need to read this book. Seriously: Read this book!

Stephen Byrne, Dell