The following function recursively checks multiple folders (though two at a time) for deletions (only in the former), additions (only in the latter), AND changes (where files sharing a name have different content)
'folder1','folder2' | DiffFolders
The function:
Function DiffFolders {
Begin {
$last = $NULL
}
Process {
$current = @{}
$unchanged = 0
$parent = $_
$parentPath = (Get-Item -Path $parent).FullName
$parentRegex = "^$([regex]::escape($parentPath))"
Get-ChildItem -Path $parentPath -Recurse -File `
| %{
$name = $_.FullName -replace $parentRegex,''
$current.Add($name, (Get-FileHash -LiteralPath $_.FullName).Hash)
if (!$last) {
return
}
if (!$last.Contains($name)) {
[PSCustomObject]@{
parent = $parent
event = 'Added'
value = $name
}
return
}
if ($last[$name] -eq $current[$name]) {
++$unchanged
}
else {
[PSCustomObject]@{
parent = $parent
event = 'Changed'
value = $name
}
}
$last.Remove($name)
}
if ($last) {
[PSCustomObject]@{
parent = $parent
event = 'Unchanged'
value = $unchanged
}
$last.Keys `
| %{
[PSCustomObject]@{
parent = $parent
event = 'Deleted'
value = $_
}
}
}
$last = $current
}
}
Here's a neat demo that should be on most win10 machines:
PS C:\Program Files\WindowsApps> gci 'Microsoft.NET.Native.Runtime.*_x64__8wekyb3d8bbwe' | %{ $_.Name }
Microsoft.NET.Native.Runtime.1.7_1.7.25531.0_x64__8wekyb3d8bbwe
Microsoft.NET.Native.Runtime.1.7_1.7.27422.0_x64__8wekyb3d8bbwe
Microsoft.NET.Native.Runtime.2.1_2.1.26424.0_x64__8wekyb3d8bbwe
Microsoft.NET.Native.Runtime.2.2_2.2.27011.0_x64__8wekyb3d8bbwe
Microsoft.NET.Native.Runtime.2.2_2.2.28604.0_x64__8wekyb3d8bbwe
PS C:\Program Files\WindowsApps> gci 'Microsoft.NET.Native.Runtime.*_x64__8wekyb3d8bbwe' | %{ $_.Name } | DiffFolders | Out-GridView
We can see exactly at which versions files were added and removed from the .NET runtime, and which were changed.
Unchanged files aren't mentioned, but counted for brevity (usually you'll have way more unchanged than changed files I imagine).
Also works on linux, for you powershell users out there running it there :)
For those curious, the unchanged files were clrcompression.dll,logo.png
, logo.png
, logo.png
, and logo.png