Powershell script to compare files.

Hi...I am looking for a script to compare 2 different .CSV files to check if there is any name entry to pop-up a message with the entry name. 

Also we have to do this regularly, hence I would like to have a pop-up message to enter the 1st file name and then enter the 2nd file name. Then would like to compare the values & give out the message with the similar entry. 

Can you please help me in that or point to the example script. Hoping to hear fro

February 6th, 2015 4:14pm

You can search the gallery for some examples to help you get started. What you need specifically is going to depend on exactly what kind of comparison you need to do.

To get started, check out the Get-Help entries for Import-Csv, Read-Host, and about_Foreach.

You might also find this information useful if you want to display popup messages.

If you run into issues, share what you've come up with and we can help.

Free Windows Admin Tool Kit Click here and download it now
February 6th, 2015 4:45pm

Thanks Adam...I got this script & its getting almost closer to what I would like, but thing that I am trying to find is - the similar values (from 2 files) to show in the output screen when this script is run 

I saved a file1 called test1.txt (Contents below) in C:


5
6
7
8
9

a file2 called test2.txt (Contents below) in C:

1
2
3
4
5

Here is the script - 

$text1 = Read-Host "Enter file one"
$text2 = Read-Host "Enter file two" 

foreach ($text1 in $text2)
{
   Compare-Object -ReferenceObject (Get-Content $text1) -ExcludeDifferent (Get-Content $text2) -IncludeEqual
}

When I save and run the script, I got the input to enter the file1: C:\text1.txt & input to enter the file2: C:\test2.txt & then as result I am getting output 1 2 3 4 5 

But I wanted to get the similar values in those 2 files which is 4, 5

Any help pls let me k

February 6th, 2015 11:31pm

You don't need the foreach loop in there. Your script should work as you're expecting just like this:

$text1 = Read-Host "Enter file one"
$text2 = Read-Host "Enter file two" 

Compare-Object -ReferenceObject (Get-Content $text1) -ExcludeDifferent (Get-Content $text2) -IncludeEqual

This is definitely a great start. Will this script work for the actual files you need to compare?

Free Windows Admin Tool Kit Click here and download it now
February 7th, 2015 11:00pm

You don't need the foreach loop in there. Your script should work as you're expecting just like this:

$text1 = Read-Host "Enter file one"
$text2 = Read-Host "Enter file two" 

Compare-Object -ReferenceObject (Get-Content $text1) -ExcludeDifferent (Get-Content $text2) -IncludeEqual

This is definitely a great start. Will this script work for the actual files you need to compare?

  • Marked as answer by mywindows Thursday, February 12, 2015 2:17 AM
February 8th, 2015 3:56am

Thanks for the help Adam. I have tested in the test environment, worked fine. But I wanted to compare between 2 CSV files (which will have lots Server name - FQDN) in Production environment. Will test and update you...
Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 5:42pm

Compare-object compares two objects record by record. It can be used to test for identical files or to process those lines that differ between the two files.

In any case, the data it returns will be lines from the file. If you compare CSV files, it will not provide any information about specific column data in the file. For CSV files you will need to use the import-csv cmdlet.

It is not clear from what you posted exactly what it is you are trying to accomplish. I suggest that you Post two short (five lines or so) CSV files such as you might be comparing, and indicate what your script needs to do with them.

February 8th, 2015 7:36pm

Hi Al Dunbar, thanks for your input...but I just tried to compare 2 csv files (in test environment) and worked fine with the same script what Adam had mentioned. 

The thing that I am trying to accomplish is to compare 2 csv files and give an output that's got the similar entries in the 2 csv files. 

Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 8:13pm

Hi Al Dunbar, thanks for your input...but I just tried to compare 2 csv files (in test environment) and worked fine with the same script what Adam had mentioned. 

The thing that I am trying to accomplish is to compare 2 csv files and give an output that's got the similar entries in the 2 csv files.

February 8th, 2015 8:34pm

Hi Al Dunbar... My apologies for not making that clear, its not to enter the entry name, but the file name of the csv. I was trying to have that way to have a pop up so that we can enter the csv file name.
Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 9:37pm