Combining Results

by Sep 4, 2014

All PowerShell Versions

Let's assume you want to identify suspicious service states like services that are stopped although their start mode is set to "Automatic", or identify services with ExitCodes that you know are bad.

Here is some example code that illustrates how you can query these scenarios and combine the results in one variable.

Sort-Object makes sure you do not have duplicates in your list before all the collected results are output to just one grid view window.

$list = @()

$list += Get-WmiObject -Class Win32_Service -Filter 'State="Stopped" and StartMode="Auto" and ExitCode!=0' | 
  Select-Object -Property Name, DisplayName, ExitCode, Description, PathName, DesktopInteract 

$list += Get-WmiObject -Class Win32_Service -Filter 'ExitCode!=0 and ExitCode!=1077' | 
  Select-Object -Property Name, DisplayName, ExitCode, Description, PathName, DesktopInteract 


$list |
  # remove identical (-Unique)
  Sort-Object -Unique -Property Name | 
  Out-GridView

 

Twitter This Tip! ReTweet this Tip!