Chapter 16. 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 reboot several computers, reconfigure several services, modify several mailboxes, and so on. In this chapter, you’ll learn three distinct techniques for accomplishing these and other multiple-target tasks: batch cmdlets, WMI (and other object) methods, and object enumeration. Also, you should know that most of the examples in this chapter won’t work on Linux or macOS; they’re (at least currently) Windows-only. But the concepts and techniques here are the same regardless of the OS you’re using.

16.1. Automation for mass management

We know this isn’t a book about VBScript, but we want to use a VBScript example to briefly illustrate the way that multiple-target administration—what Don likes to call mass management—has been approached in the past. Consider this example (you don’t need to type this in and run it—we’re going to discuss the approach, not the results):

For Each varService in colServices         

This kind of approach is common not only in VBScript but also throughout the world of programming. The following steps illustrate what it does:

16.2. The preferred way: “batch” cmdlets

16.3. The CIM/WMI way: invoking methods

16.4. The backup plan: enumerating objects

16.5. Common points of confusion

16.6. Lab

16.7. Lab answers