Last active
February 26, 2025 11:00
-
-
Save OlinB/62c93d8803527846f8c649b0c5d0f146 to your computer and use it in GitHub Desktop.
Powershell recursive folder comparaison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# Goes in Documents\PowerShell\profile.ps1 #> | |
<# usage: CompareDir -path1 path\to\folder\1 -path2 path\to\folder\2 #> | |
function Get-Files { | |
param ( | |
[string]$path, | |
[string[]]$exclude | |
) | |
if (-not $path) { | |
Write-Host "Usage: Get-Files -path <string> (-exclude <string>)" | |
return | |
} | |
foreach ($item in Get-ChildItem -Path $path) { | |
if ($excludedFiles -contains $item.Name) { continue } | |
if ((Get-Item $item.FullName) -is [System.IO.DirectoryInfo]) { | |
Write-Output '[' + $item.Name + ']' | |
} else { | |
Write-Output $item.Name | |
} | |
if (Test-Path -Path $item.FullName -PathType Container) { | |
Get-Files -path $item.FullName -excludedFiles $exclude | |
} | |
} | |
} | |
function Compare-Dir { | |
param ( | |
[string]$path1, | |
[string]$path2 | |
) | |
if (-not $path1 -or -not $path2) { | |
Write-Host "Usage: Compare-Dir -path1 <string> -path2 <string>" | |
return | |
} | |
$env1 = Get-Files -path $path1 -exclude @() | |
$env2 = Get-Files -path $path2 -exclude @() | |
$comparison = Compare-Object -DifferenceObject $env1 -ReferenceObject $env2 | |
foreach ($difference in $comparison) { | |
if ($difference.SideIndicator -eq '=>') { | |
$difference.SideIndicator = '-->' | |
} elseif ($difference.SideIndicator -eq '<=') { | |
$difference.SideIndicator = '<--' | |
} | |
Write-Output $difference | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment