I realize this is an old post, but I'm hoping this might help someone.
I needed to be able to compare a .reg file to current values in the registry using PowerShell, but the problem I was having was that exporting registry keys for the comparison didn't always export them in the same order that they were in the .reg file I wanted to compare to. So the files may have been effectively identical, a string comparison on the raw file showed that they were different. While some advanced GUI tools might be able to sort it out and do a comparison regardless of the order of the reg keys in the .reg files, that doesn't help when it comes to automating like I was trying to do.
After a fair amount of playing around in PowerShell I found a solution that works quite well:
$liveKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\' #The live key you want to compare
$livePath = 'C:\temp\live.reg' #Temporary location for the exported reg file
$compareToPath = 'C:\temp\compareTo.reg' #The .reg file to compare to
REG EXPORT $liveKey $livePath
$compareToFile = ((Get-Content $compareToPath -Raw) -split "`r`n`r`n" | Sort-Object) -join "`r`n`r`n"
$liveFile = ((Get-Content $livePath -Raw) -split "`r`n`r`n" | Sort-Object) -join "`r`n`r`n"
$liveFile -eq $compareToFile #This will return True if they are the same, $false if they are different.
If you want to see the specific differences just don't rejoin the array and use Compare-Object:
$liveKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\' #The live key you want to compare
$livePath = 'C:\temp\live.reg' #Temporary location for the exported reg file
$compareToPath = 'C:\temp\compareTo.reg' #The .reg file to compare to
REG EXPORT $liveKey $livePath
$compareToFile = (Get-Content $compareToPath -Raw) -split "`r`n`r`n" | Sort-Object
$liveFile = (Get-Content $livePath -Raw) -split "`r`n`r`n" | Sort-Object
Compare-Object $liveFile $compareToFile #The output isn't pretty so if you need to know specific differences, you'll have to play with it a bit to get what you want
The important parts are these two lines:
$compareToFile = ((Get-Content $compareToPath -Raw) -split "`r`n`r`n" | Sort-Object) -join "`r`n`r`n"
$liveFile = ((Get-Content $livePath -Raw) -split "`r`n`r`n" | Sort-Object) -join "`r`n`r`n"
Basically, what we're doing here is reading the .reg file as a single string, splitting it into an array wherever there are two line breaks (.reg exports always have two line breaks between keys), sorting the array, and then joining it back together in a single string. Then since both strings are sorted the same way, the comparison is just a simple string comparison.
reg /query
is a starting point. See Reg - Edit Registry - Windows CMD - SS64.com – DavidPostill – 2018-09-06T20:23:05.9301The PowerShell Compare-Object cmdlet may also prove helpful if you use reg.exe to export the current to a reg file and compare it against an exported .reg file or files. – HelpingHand – 2018-09-06T20:59:47.567