powershell Compare two file and find the difference.

Posted: October 21, 2015 in PowerShell

Compare two file to find the difference and write the output to a file.

Q) Compare file “A” with file “B” and get the lines not present B but present in A.

A File contents:-
A man with golden arm
The man with the golden gun
die another day

B File contents:-
The man with the golden gun
die another day. what?

Required Output:-
A man with golden arm
die another day. what?

In the required out one can see 2 records
–> “A man with golden arm” is not present in B file.
–> “die another day. what?” is present in B file but it has a change(highlighted in bold font).

Powershell script to achieve the same.

$Sec_FILE = Read-Host
$TEMP = "H:\Desktop\TEMP\" + $Sec_FILE + ".TXT"
$SRC_FILE = "H:\Desktop\WA_BKP\" + $Sec_FILE + ".TXT"
$strReference = get-content   $SRC_FILE
 # Reference is the file which is A in (A-B of sql)
$strDifference = Get-Content "H:\Desktop\WA_BKP\BAI070701_20150929_BKP.TXT"
 # In our case this should be the big file
 compare-object ($strDifference) ($strReference) | ?{$_.sideIndicator -ne "<="} | out-file $TEMP
get-childItem "H:\Desktop\TEMP" | where {$_.length -eq 0} | remove-Item
 if ($? -eq "True")
 {
 Write-Host "ALL done. Now Exit"
 exit 0
 }
 else
 {
 Write-Host "Something is went wrong check the script."
 exit -1
 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s