Get-content of files in different folders and compare if any difference


Have been stuck on this for awhile. I have a script that outputs ipconfig/routes/linkspeed, etc… to a text file in one location. After network changes, we would like to run the script again to get same information and save that text file to another location. Text files are named the same in both location, servername.log.

I would like to get the contents of each text file, and compare the text files with the same filename from the two folder against each other. Any difference in the files would be outputted to another text file. Any help appreciated as i been stuck on this.

script to get info below:

$servernames = get-content “…\serverlist.txt”
ForEach ($servers in $servernames)
Write-Host “checking $servers”
$logRoot = “…\PreChecks”
$logFilename = $servers + “.log”
$logFile = Join-Path $logRoot $logFilename
$logdate = get-date
Out-file $logFile
Write-Output “Date: $logdate” | Out-file $logFile -Append
Write-Output “Server: $servers” | Out-file $logFile -Append
$session = New-PSSession -ComputerName $servers
Write-Output “” | Out-file $logfile -Append
Write-Output “Route Print” | Out-file $logfile -Append
#route.exe print | out-file $logfile -Append
Invoke-Command -Session $session -ScriptBlock {route.exe print} | out-file $logfile -Append
Write-Output “” | Out-file $logfile -Append
Write-Output “TraceRoute” | Out-file $logfile -Append
Invoke-Command -Session $session -ScriptBlock {tracert.exe eq4innas01b} | out-file $logfile -Append
Write-Output “” | Out-file $logfile -Append
Write-Output “IPConfig” | Out-file $logfile -Append
Invoke-Command -Session $session -ScriptBlock {ipconfig.exe /all} | out-file $logfile -Append
Write-Output “” | Out-file $logfile -Append
Write-Output “NIC LinkSpeed” | Out-file $logfile -Append
Invoke-Command -Session $session -ScriptBlock {Get-WmiObject -namespace root\wmi -Class msndis_linkspeed | select-object instancename, ndislinkspeed} | fl | out-file $logfile -Append
Get-PSSession | Remove-PSSession


File diff algorithms can be a bit complex to write on your own, and Compare-Object isn’t very well suited for this since it changes the order of your lines when it does its comparison. Personally, I’d just use fc.exe , which is a command-line file comparison tool that’s included with Windows. For example:

fc.exe /U file1.txt file2.txt > differences.txt

The /U switch tells fc.exe to treat file1.txt and file2.txt as Unicode, which is what PowerShell will typically produce when you use things like Out-File.

@dave , thx for the reply. Would you know how i can loop and recursively get the files from the two folders and fc.exe the files with the same name against each other

I’m not sure what you mean by that. Assuming you have a file named ‘test.txt’ in folders A, A\SubFolder1 , B, and B\SubFolder2 , which combinations of files would you want to compare? What would you name the output “diff” file for each combination?