Geek School: PowerShell Collections - A Deep Dive

PowerShell Task Optimization: Selecting the Optimal Approach
PowerShell, built upon the .Net Framework and integrating technologies such as WMI and CIM, frequently offers multiple pathways to achieve identical outcomes. This article will explore strategies for discerning the most effective method for completing your PowerShell tasks.
The flexibility inherent in PowerShell’s design allows administrators to leverage different techniques. Understanding these options is crucial for writing efficient and maintainable scripts.
Review of Previous Articles
To fully grasp the concepts discussed here, it is recommended to review the preceding articles in this series:
- Automating Windows with PowerShell: An introduction to the fundamentals of PowerShell automation.
- Cmdlet Utilization in PowerShell: A deep dive into the use of cmdlets for task execution.
- Working with Objects in PowerShell: Understanding how to manipulate and utilize objects within PowerShell.
- Formatting, Filtering, and Comparison in PowerShell: Techniques for refining and analyzing data.
- PowerShell Remoting: Utilizing remote management capabilities.
- Retrieving Computer Information with PowerShell: Methods for gathering system details.
These articles provide a foundational understanding of PowerShell concepts. They will enhance your ability to select the best approach for your scripting needs.
Further installments of this series will be published throughout the week. Continue to follow along for more advanced techniques and best practices.
Leveraging Batch Cmdlets in PowerShell
Previously, when discussing the PowerShell pipeline, we demonstrated how cmdlet outputs can be channeled as inputs to subsequent cmdlets, as illustrated by this example:
Get-Process -Name notepad | Stop-Process
This command effectively terminates all processes named “notepad”. You might be curious about how PowerShell manages to halt every instance of notepad with a single instruction. The explanation resides within the documentation for the Stop-Process cmdlet.
help Stop-Process –Full
Examining the syntax section, specifically the final line of code, reveals that the InputObject parameter is designed to accept an object of type Process[]. The notation of a type followed by square brackets signifies that the parameter can accommodate one or more instances of that type.
In this instance, it accepts multiple process objects. More precisely, the InputObject parameter accepts a process array. Whenever a cmdlet offers support for batch operations in this way, it should be utilized. This represents the preferred approach.
Leveraging WMI
While not necessarily Microsoft’s most refined technology, WMI remains a significant method for interacting with collections of objects. Obtaining a list of currently running processes, for example, can be achieved through the Win32_Process class using the following command:
Get-WmiObject –Class Win32_Process
Because the WMI query yields a unique object type, identifying a method to halt a process requires further investigation. Piping the output to Get-Member will reveal available methods.
Get-WmiObject –Class Win32_Process | Get-Member
The results indicate that the Terminate method is the closest available option for stopping a process. Executing a method on a WMI object is accomplished by piping it to Invoke-WmiMethod and specifying the method’s name.
Get-WmiObject -Class Win32_Process -Filter "name='notepad.exe'" | Invoke-WmiMethod -Name Terminate
This command successfully terminated the specified process. A ReturnValue of 0 from a WMI operation signifies successful execution of the command.
Enumeration as a Method of Process Control
Should the previously discussed techniques prove insufficient, a viable approach involves iterating through a collection of objects and applying an action to each one individually. Initially, identifying the appropriate method for terminating a single process is crucial.
Get-Process | Get-Member -MemberType Method
This command allows you to discover the available methods associated with process objects.

As demonstrated, the Kill method is suitable for this purpose. It can then be combined with ForEach-Object to apply the termination action to multiple processes.
Get-Process -Name notepad | ForEach-Object -Process {$_.Kill()}
This pipeline retrieves all processes named "notepad" and then iterates through each one.

Within the ForEach-Object cmdlet, each process object is represented by the variable $_. This allows access to the object's properties and methods, such as Kill(). It's important to note that enumerating a collection is generally less efficient than the alternative methods previously outlined.
Therefore, this technique should be reserved for scenarios where other approaches are not feasible, due to its comparatively slower execution speed.
Terminating Processes with PowerShell
Several methods exist within PowerShell for ending processes. These techniques offer varying levels of control and are useful for system administration and automation tasks.
Method 1: Utilizing Stop-Process
One approach involves the Stop-Process cmdlet. This is a straightforward way to terminate a process by its name.
Get-Process -Name notepad | Stop-Process
This command retrieves the process named "notepad" and then pipes it to Stop-Process, effectively ending the application.
Method 2: Employing WMI
Alternatively, the Windows Management Instrumentation (WMI) can be leveraged. This method uses the Get-WmiObject and Invoke-WmiMethod cmdlets.
Get-WmiObject -Class Win32_Process -Filter "name='notepad.exe'" | Invoke-WmiMethod -Name Terminate
Here, WMI queries for the "notepad.exe" process and then invokes the "Terminate" method to stop it.
Method 3: Using the Kill() Method
A third option utilizes the Kill() method directly on the process object. This is achieved through ForEach-Object.
Get-Process -Name notepad | ForEach-Object -Process {$_.Kill()}
This command obtains the "notepad" process and then iterates through each instance, calling the Kill() method to terminate it.
These are just a few examples of how to terminate processes using PowerShell. Further exploration of these cmdlets will reveal additional functionalities and options for process management.