Rory Claasen

DLL Version Comparison

3 min read

It’s not very exciting, but I needed a way to compare DLL versions between two folders. For some reason our build pipeline was using different versions of the same DLL in different builds, and I needed to find out why. This PowerShell script will compare the DLL versions in two specified folders and output any differences.

Compare-DllVersions.ps1
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Folder1,
[Parameter(Mandatory = $true)]
[string]$Folder2,
[Parameter(Mandatory = $false)]
[switch]$IncludeSubfolders = $false
)
function Get-DllVersions {
param(
[string]$FolderPath,
[bool]$Recurse
)
if (-not (Test-Path $FolderPath)) {
Write-Error "Folder path '$FolderPath' does not exist."
return @{}
}
$dllFiles = Get-ChildItem -Path $FolderPath -Filter "*.dll" -Recurse:$Recurse
$dllVersions = @{}
foreach ($dll in $dllFiles) {
try {
$versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($dll.FullName)
$relativePath = $dll.FullName.Replace($FolderPath, "").TrimStart('\')
$dllVersions[$relativePath] = @{
FileVersion = $versionInfo.FileVersion
ProductVersion = $versionInfo.ProductVersion
FullPath = $dll.FullName
}
}
catch {
Write-Warning "Could not get version info for: $($dll.FullName) - $($_.Exception.Message)"
$dllVersions[$relativePath] = @{
FileVersion = "Unable to read"
ProductVersion = "Unable to read"
FullPath = $dll.FullName
}
}
}
return $dllVersions
}
function Compare-DllVersions {
param(
[hashtable]$Folder1Dlls,
[hashtable]$Folder2Dlls,
[string]$Folder1Name,
[string]$Folder2Name
)
$differences = @()
# Get all unique DLL names from both folders
$allDlls = @($Folder1Dlls.Keys) + @($Folder2Dlls.Keys) | Sort-Object -Unique
foreach ($dllName in $allDlls) {
$dll1 = $Folder1Dlls[$dllName]
$dll2 = $Folder2Dlls[$dllName]
if ($null -eq $dll1) {
# DLL only exists in Folder2
$differences += [PSCustomObject]@{
DllName = $dllName
Status = "Only in $Folder2Name"
Folder1_FileVersion = "N/A"
Folder2_FileVersion = $dll2.FileVersion
}
}
elseif ($null -eq $dll2) {
# DLL only exists in Folder1
$differences += [PSCustomObject]@{
DllName = $dllName
Status = "Only in $Folder1Name"
Folder1_FileVersion = $dll1.FileVersion
Folder2_FileVersion = "N/A"
}
}
else {
if ($dll1.FileVersion -ne $dll2.FileVersion) {
$differences += [PSCustomObject]@{
DllName = $dllName
Status = "File Version Diff"
Folder1_FileVersion = $dll1.FileVersion
Folder2_FileVersion = $dll2.FileVersion
}
}
}
}
return $differences
}
$Folder1 = $Folder1 | Resolve-Path
$Folder2 = $Folder2 | Resolve-Path
# Main script execution
Write-Host "Comparing DLL versions between folders..." -ForegroundColor Green
Write-Host "Folder 1: $Folder1" -ForegroundColor Cyan
Write-Host "Folder 2: $Folder2" -ForegroundColor Cyan
Write-Host "Include Subfolders: $IncludeSubfolders" -ForegroundColor Cyan
Write-Host ""
# Get DLL versions from both folders
Write-Host "Scanning Folder 1..." -ForegroundColor Yellow
$folder1Dlls = Get-DllVersions -FolderPath $Folder1 -Recurse $IncludeSubfolders
Write-Host "Scanning Folder 2..." -ForegroundColor Yellow
$folder2Dlls = Get-DllVersions -FolderPath $Folder2 -Recurse $IncludeSubfolders
Write-Host "Found $($folder1Dlls.Count) DLLs in Folder 1" -ForegroundColor Magenta
Write-Host "Found $($folder2Dlls.Count) DLLs in Folder 2" -ForegroundColor Magenta
Write-Host ""
# Compare the versions
$differences = Compare-DllVersions -Folder1Dlls $folder1Dlls -Folder2Dlls $folder2Dlls -Folder1Name "Folder1" -Folder2Name "Folder2"
if ($differences.Count -eq 0) {
Write-Host "No differences found between the DLL versions in both folders!" -ForegroundColor Green
}
else {
Write-Host "Found $($differences.Count) differences:" -ForegroundColor Red
Write-Host ""
# Display the differences in a formatted table
$differences | Format-Table -AutoSize -Wrap
}
Write-Host ""
Write-Host "Comparison complete!" -ForegroundColor Green