In this article, I will show you how to compare the configuration of Windows services with the help of the Compare-Object cmdlet. You will also learn how to format the output for Excel and in HTML.

Damien Van Robaeys

Damien Van Robaeys is a system and desktop engineer who focuses on PowerShell and the Microsoft Deployment Toolkit. You can follow him on Twitter and on his blog.

Displaying differences between services on the console ^

First, we have to collect all services available on the computer. You can use three methods for this purpose:

  • Get-WMIObject: available for all PowerShell versions
  • Get-CimInstance: available since PowerShell 3
  • Get-Service: available for PowerShell versions

Because you can’t read the service start mode with Get-Service, we will use Get-CimInstance for the examples in this post.

Now let’s change some service values to see what PowerShell can do to highlight differences.

We will stop the services below to compare the state property:

Adobe Acrobat Update service before change

Adobe Acrobat Update service before change

Adobe Acrobat Update service after change

Adobe Acrobat Update service after change

Print Spooler before change

Print Spooler before change

Print Spooler after change

Print Spooler after change

The command below collects the new state of the services:

Now we can use Compare-Object to compare the contents of the two variables. The first variable is called the reference set and the second variable is the difference set. The IncludeEqual parameter ensures that values that appear in both sets will be displayed.

Comparing two variables in PowerShell

Comparing two variables in PowerShell

The SideIndicator (==) tells us that the name and the caption properties have the same value in both sets.

In the next example, we omit the IncludeEqual parameter and only list services that have different values with regard to the name, caption, and state properties.

Displaying the differences between two variables

Displaying the differences between two variables

The AdobeARMservice and the Spooler service appear twice in the list because PowerShell displays the reference and the difference set with regard to their differences. For instance, the state of AdobeARMservice is set to “Stopped” in the reference set and is set to “Running” in the difference set. The SideIndicator => tells you that the state of AdobeARMservice is set to “Stopped” only in the reference set and the SideIndicator <= implies that the state of AdobeARMservice is set to “Running” only in the difference set.

You can pipe the comparison result to Out-Gridview to display an interactive table:

Displaying the comparison results in an interactive table

Displaying the comparison results in an interactive table

In the next example, we compare the services with regard to the startmode property. For this purpose, we first have to change the start mode of the Adobe Acrobat Update Service and the Print Spooler service from Automatic to Disabled.

Next, we collect the new values in the variable $Services_3 and then compare the values with $Services_1:

Comparing the start mode of services

Comparing the start modes of services

We can also only display the values of the properties in the difference set using the Where filter:

Displaying property values in the difference set

Displaying property values in the difference set

If you want to store the comparison in an Excel table, you can pipe the output to Export-CSV:

This method also allows you to compare services between two computers. All you have to do is to first export the comparison results to a CSV and then import the results again to PowerShell variables:

To compare both files, use the same compare command as previously:

For better readability, you can change the column titles in the table. The example below uses the Group-Object and New-Object cmdlets for this purpose:

Using different column titles in the comparison table

Using different column titles in the comparison table

The method below can be used to compare multiple properties. It compares each name from the first CSV file with the second CSV file. If a name appears in both files, it will compare the startmode value for each service.

Comparing multiple properties

Comparing multiple properties

Displaying the comparison results in Excel and HTML ^

The next sample script starts an Excel process and adds a workbook and a worksheet. It then loads the content of the CSV file and applies a TableStyle.

Displaying the comparison result in Excel

Displaying the comparison result in Excel

The next example displays the results in HTML.

Displaying the comparison result in HTML

Displaying the comparison result in HTML

It’s not very pretty, so now let’s give it more style by adding some CSS. In the next example, I have created a CSS file and applied it with the CSSUri parameter.

Displaying the comparison result in HTML with CSS

Displaying the comparison result in HTML with CSS

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

1+
Share
0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account