Windows PowerShell in Action, Third Edition

Windows PowerShell in Action, Third Edition

Now pay Easier and Secure using Paypal

Read more

Bruce Payette & Richard Siddaway

Brief Table of Contents

Brief Table of Contents
Table of Contents
Praise for the Second Edition
Praise for the First Edition
About this Book
About the Cover Illustration
Chapter 1. Welcome to PowerShell
Chapter 2. Working with types
Chapter 3. Operators and expressions
Chapter 4. Advanced operators and variables
Chapter 5. Flow control in scripts
Chapter 6. PowerShell functions
Chapter 7. Advanced functions and scripts
Chapter 8. Using and authoring modules
Chapter 9. Module manifests and metadata
Chapter 10. Metaprogramming with scriptblocks and dynamic code
Chapter 11. PowerShell remoting
Chapter 12. PowerShell workflows
Chapter 13. PowerShell Jobs
Chapter 14. Errors and exceptions
Chapter 15. Debugging
Chapter 16. Working with providers, files, and CIM
Chapter 17. Working with .NET and events
Chapter 18. Desired State Configuration Chapter
19. Classes in PowerShell
Chapter 20. The PowerShell and runspace APIs
PowerShell 6.0 for Windows, Linux, and macOS
List of Figures
List of Tables
List of Listings

e-books shop
e-books shop
Purchase Now !
Just with Paypal

Book Details
 938 p
 File Size 
 11,096 KB
 File Type
 PDF format
 2018 by Manning Publications Co 

About the Author
BRUCE PAYETTE is one of the founding members of the Windows PowerShell team. He is codesigner of the PowerShell language along with Jim Truher and the principal author of the
language implementation. He joined Microsoft in 2001 working on Interix, the POSIX
subsystem for Windows. Shortly after that, he moved to help found the PowerShell project. Prior
to joining Microsoft, he worked at various companies including Softway (the creators of Interix)
and MKS (producers of the MKS Toolkit) building UNIX tools for Windows. He lives in
Bellevue, Washington, with his wife, many computers, and two extremely over-bonded
codependent cats.

RICHARD SIDDAWAY has been using PowerShell since the early beta versions of PowerShell
v1. He has introduced PowerShell to many organizations while producing automation-based
solutions to their problems. He has written, and co-authored, a number of PowerShell books for
Manning including PowerShell in Practice, PowerShell and WMI, and PowerShell in Depth. His
books on Hyper-V and Active Directory contain many practical PowerShell examples. An active
blogger and speaker, Richard has also received Microsoft’s PowerShell MVP award for 10 years.

About the title
By combining introductions, overviews, and how-to examples, the In Action books are designed
to help learning and remembering. According to research in cognitive science, the things people
remember are things they discover during self-motivated exploration.
Although no one at Manning is a cognitive scientist, we are convinced that for learning to
become permanent it must pass through stages of exploration, play, and, interestingly, retelling
of what is being learned. People understand and remember new things, which is to say they
master them, only after actively exploring them. Humans learn in action. An essential part of an
In Action book is that it is example-driven. It encourages the reader to try things out, to play with
new code, and explore new ideas.
There is another, more mundane, reason for the title of this book: Our readers are busy. They use
books to do a job or solve a problem. They need books that allow them to jump in and jump out
easily and learn just what they want just when they want it. They need books that aid them in
action. The books in this series are designed for such readers.

About this Book
Windows PowerShell is the next-generation scripting environment created by Microsoft. It’s
designed to provide a unified solution for Windows scripting and automation, able to access the
wide range of technologies such as .NET, COM, and WMI through a single tool. Since its release
in 2006, PowerShell has become the central component of any Windows management solution.
In addition, due to PowerShell’s comprehensive support for .NET, it has broad application
potential outside of the system administration space. PowerShell can be used for text processing,
general scripting, build management, creating test frameworks, and so on. With PowerShell v6
being available on Linux and macOS as well as Windows, the benefits of PowerShell now
extend cross-platform bringing a unified approach to system management.

The authors have extensive experience with PowerShell. Bruce was one of the principal creators
of PowerShell. Richard has been using PowerShell since it first became available to apply
automation techniques to many organizations. Using many examples, both small and large, this
book illustrates the features of the language and environment and shows how to compose those
features into solutions, quickly and effectively.

Note that, because of the broad scope of the PowerShell product, this book has a commensurately
broad focus. It was not designed as a cookbook of pre-constructed management examples, like
how to deal with Active Directory or how to script Exchange. Instead it provides information
about the core of the PowerShell runtime and how to use it to compose solutions the “PowerShell
Way.” After reading this book, the PowerShell user should be able to take any example written
in other languages like C# or Visual Basic and leverage those examples to build solutions in

Who should read this book?
This book is designed for anyone who wants to learn PowerShell and use it well. Rather than
simply being a book of recipes to read and apply, this book tries to give the reader a deep
knowledge about how PowerShell works and how to apply it. 
All users of PowerShell should read this book.

So, if you’re a Windows sysadmin, this book is for you. If you’re a developer and you need to
get things done in a hurry, if you’re interested in .NET, or just if you like to experiment with
computers, PowerShell is for you and this book is for you.

Table of Contents
Brief Table of Contents
Table of Contents
Praise for the Second Edition
Praise for the First Edition Preface
About this Book
About the Cover Illustration
Chapter 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. Formatting cmdlets
1.6.2. Outputter cmdlets
1.7. Summary
Chapter 2. Working with types
2.1. Type management in the wild, wild West
2.1.1. Types and classes
2.1.2. PowerShell: A type-promiscuous language
2.1.3. 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 hashtables
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
Chapter 3. Operators and expressions
3.1. Arithmetic operators
3.1.1. Addition operator
3.1.2. Multiplication operator
3.1.3. Subtraction, division, and the modulus operators
3.2. 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
Chapter 4. Advanced operators and variables
4.1. Operators for working with types
4.2. Unary operators
4.3. Grouping and subexpressions
4.3.1. Subexpressions $( ... )
4.3.2. Array subexpressions @( ... )
4.4. Array operators
4.4.1. Comma operator
4.4.2. 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. Dot operator
4.5.2. Static methods and the double-colon operator
4.5.3. Indirect method invocation 
4.6.  Format operator
4.7. Redirection and redirection operators
4.8. Working with variables
4.8.1. Creating variables
4.8.2. Variable name syntax
4.8.3. Working with variable cmdlets
4.8.4. Splatting a variable
4.9. Summary
Chapter 5. Flow control in scripts
5.1. Conditional statement
5.2. Looping statements
5.2.1. while loop
5.2.2. do-while loop
5.2.3. for loop 
5.2.4. foreach loop
5.3. Labels, break, and continue
5.4. 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. ForEach-Object cmdlet
5.5.2. Where-Object cmdlet
5.6. Statements as values
5.7. A word about performance
5.8. Summary
Chapter 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
Chapter 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
Chapter 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. Creating a binary module
8.5.2. Nesting binary modules in script modules
8.6. Summary
Chapter 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. Advanced module operations
9.6.1. The PSModuleInfo object
9.6.2. Using the PSModuleInfo methods
9.6.3. The defining module vs. the calling module
9.6.4. Setting module properties from inside a script module
9.6.5. Controlling when modules can be unloaded
9.6.6. Running an action when a module is removed
9.7. Publishing a module to a PowerShell Gallery
9.7.1. A module to publish
9.7.2. PSData Packaging elements
9.7.3. Publishing a module 
9.7.4. Publishing module updates
9.8. Summary
Chapter 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
Chapter 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.3.5. Copying files across a PowerShell remoting session
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
Chapter 12. PowerShell workflows
12.1. Workflow overview
12.1.1. Why use 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
Chapter 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.2.1. Checkpoints
13.2.2. Suspending workflows
13.2.3. Workflows and reboots
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
Chapter 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 whether 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
Chapter 15. Debugging
15.1. Script instrumentation
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
Chapter 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
Creating COM objects
Identifying and locating COM classes
Automating Windows with COM Using
Microsoft Word for spell checking Issues
with COM
16.4. Using CIM
16.4.1. The CIM cmdlets
16.4.2. CIM sessions
16.5. Summary
Chapter 17. Working with .NET and events
17.1. .NET and PowerShell
17.1.1. Using .NET from PowerShell
17.1.2. PowerShell and GUIs
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
Chapter 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
Chapter 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
The using assembly pattern
The using namespace pattern
The using module pattern
Using modules and namespaces
19.5. Writing class-based DSC resources
19.6. Summary
Chapter 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
PowerShell 6.0 for Windows, Linux, and macOS
The PowerShell open source project
.NET Core Installing
on Windows
PowerShell on Linux and macOS
Known issues
Using PowerShell v6 on Linux
PowerShell remoting and Linux
DSC and Linux
Installing DSC for Linux
Using DSC for Linux
List of Figures
List of Tables
List of Listings

e-books shop

Praise for the Second Edition
First he wrote the language, then he wrote the book.
Jeffrey Snover, Microsoft
Really understanding a scripting language means getting inside the heads of the designers
and developers. Windows PowerShell in Action makes that possible in one really
informative and entertaining book.
Jason Zions, Microsoft
Unleashes the power in PowerShell.
Sam Abraham, SISCO
Who better than the lead language designer to provide the definitive reference on the
PowerShell language!
Keith Hill, Agilent Technologies
If you like to learn by example, there is no better example of examples than Payette’s
incredible book.
Oisin Grehan, .NET Solution Architect & Microsoft PowerShell MVP

Praise for the First Edition
The book on PowerShell. It has all the secrets.
James Truher, PowerShell Program Manager, Microsoft
If all it had going for it was the authoritative pedigree of the writer, it might be worth it, but
it’s also well-written, well-organized, and thorough, which I think makes it invaluable as
both a learning tool and a reference.
...an encyclopedic tome of PowerShell scripting bringing the reader through the basics with
simple shell scripts through powerful and flexible scripts any Windows systems
administrator will find immediately useful.
[It gives you] inside information, excellent examples, and a colorful writing style.
Marc van Orsouw (MOW), PowerShell MVP