15 Working with many objects, one at a time

 

The whole point of PowerShell is to automate administration, and that often means you’ll want to perform some tasks with multiple targets. You might want to start several VMs, push to several blob storages, modify permissions of several users, and so on. In this chapter, you’ll learn two distinct techniques for accomplishing these and other multiple-target tasks: batch cmdlets and object enumeration. The concepts and techniques here are the same regardless of the OS you’re using.

Note

This is an extremely difficult chapter and will probably frustrate you. Please be patient—with yourself and us—and trust that we do explain everything by the end.

15.1 The preferred way: “Batch” cmdlets

As you’ve learned in several previous chapters, many PowerShell cmdlets can accept batches, or collections, of objects to work with. In chapter 6, for example, you learned that objects can be piped from one cmdlet to another, like this (please don’t run this on any system, unless you really want to have a bad day):

Get-Service | Stop-Service

15.2 The CIM way: Invoking methods

15.3 The backup plan: Enumerating objects

15.3.1 Making the cmdlets work for you

15.4 Let’s speed things up

15.5 Common points of confusion

15.5.1 Which way is the right way?

15.5.2 Diminishing returns of Parallel ForEach

15.5.3 Method documentation

15.5.4 ForEach-Object confusion

15.6 Lab

15.7 Lab answers