How To Compare Files Using PowerShell

Intro

Hi Friends!!

Hope you all are doing well.

Today we will discuss comparing files using Powershell.

So for today’s session, the scenario is,

Let’s say you are going to perform a task/configuration change on a server, then you want to compare windows services status before & after the activity and take action on stopped service after you perform the change/activity.

At a high level the steps we will perform is as below:

  1. Login to server and export the current windows service status to a CSV file (pre.csv)
  2. Stop the BITS service using PowerShell
  3. Again export the windows service status to a CSV file (post.csv)
  4. Then compare both the files for difference
  5. Now if any service stopped in post.csv, start them using Powershell.

All the above steps will be done automatically using Powershell scripts.

Practical Steps

Now, let’s perform each above-mentioned steps one by one

Export the current windows service status to a csv file (pre.csv)

Login to server and export the current windows service status to a CSV file named pre.csv.

Get-Service | select Name,Status |Export-Csv -Path C:\temp\pre.csv -NoTypeInformation

Stop the BITS service using powershell

Now let’s say after you perform your task/activity the BITS service gets stopped. This is a test scenario hence to show our demo I am manually stopping a random service, real-world scenario may vary.

Stop-Service bits
Get-Service bits

Again export the windows service status to a csv file (post.csv)

Okay, so now let’s again export all windows service status to a CSV file named post.csv.

Get-Service | select Name,Status |Export-Csv -Path C:\temp\post.csv -NoTypeInformation

Compare both the files for difference

Now we have both the files to compare them using Powershell for any difference.

So let’s do it!

Using the following piece of code you can see the BITS service is reflecting as a difference, which means, BITS was Running in Pre.csv but stopped in the Post.csv file.

#Import files to compare - files contains windows services status before/after activity
$pre = Import-Csv C:\Temp\pre.csv
$post = Import-Csv C:\Temp\post.csv


$diff = Compare-Object -ReferenceObject $pre -DifferenceObject $post -Property Name,Status | Where-Object {$_.Status -eq "Stopped"}

Any service stopped in post.csv, start them using powershell

Okay so now it’s time to run the full code. 

After running the full Powershell code it will check if any service is stopped in post.csv and start it automatically.

Code is below:

#Import files to compare - files contains windows services status before/after activity
$pre = Import-Csv C:\Temp\pre.csv
$post = Import-Csv C:\Temp\post.csv


$diff = Compare-Object -ReferenceObject $pre -DifferenceObject $post -Property Name,Status | Where-Object {$_.Status -eq "Stopped"}

#Save the service which is stopped in temp folder as a csv file
$diff | Export-Csv C:\Temp\diff.csv -NoTypeInformation

# It will get the service which is stopped and start it
$import_svc = Import-Csv C:\Temp\diff.csv | select -ExpandProperty Name


foreach($service in $import_svc)

{
 
        Start-Service $service
        Write-Host "$($service) was stopped in post.csv file, now Started" -ForegroundColor Green

     

}

#clean up
Remove-Item C:\Temp\diff.csv
Write-Host "C:\Temp\diff.csv  Deleted" -ForegroundColor Yellow

Below is the output.

Let’s check whether bits service is started OR not

Awesome!!!

So we have compared two files to get the difference of service status and start it automatically.

You can use it in different scenarios as per your need. Please use it accordingly.

Thank you!! if you like it please do share.

Please visit my below Facebook page and like.

https://www.facebook.com/learninside2019