Skip to content

Instantly share code, notes, and snippets.

@OlinB
Last active February 26, 2025 11:00
Show Gist options
  • Save OlinB/62c93d8803527846f8c649b0c5d0f146 to your computer and use it in GitHub Desktop.
Save OlinB/62c93d8803527846f8c649b0c5d0f146 to your computer and use it in GitHub Desktop.
Powershell recursive folder comparaison
<# 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