Install PDF24 Creator and PDF-XChange Editor and remove all other PDF software.
$pdfPrograms = Get-WmiObject Win32_Product | Where-Object { $_.Name -like "*pdf*" } | Select-Object -ExpandProperty Name | Sort-Object
$expected = @("PDF24 Creator", "PDF-XChange Editor") | Sort-Object
$ProgressPreference = "SilentlyContinue"
$Path = "C:\Users\Public\Downloads"
$missingPrograms = $expected | Where-Object { $_ -notin $pdfPrograms }
$extraPrograms = $pdfPrograms | Where-Object { $_ -notin $expected }
# STEP 1: Remove extra programs FIRST
if ($extraPrograms) {
$installedProducts = Get-WmiObject Win32_Product
$uninstalledCount = 0
foreach ($programName in $extraPrograms) {
$product = $installedProducts | Where-Object { $_.Name -eq $programName }
if ($product) {
$identifyingNumber = $product.IdentifyingNumber
try {
Start-Process "msiexec.exe" -ArgumentList "/qn /norestart /X $identifyingNumber" -Wait
Write-Host "Successfully uninstalled $($product.Name)"
$uninstalledCount++
}
catch {
Write-Host "Failed to uninstall $($product.Name): $($_.Exception.Message)"
}
}
else {
Write-Host "Product $programName not found in installed products."
}
}
}
# STEP 2: Install missing programs
if ("PDF24 Creator" -in $missingPrograms) {
$Link1 = "https://download.pdf24.org/pdf24-creator-11.28.2-x64.msi"
$File1 = "pdf24-creator-11-x64.msi"
Invoke-WebRequest -Uri $Link1 -OutFile $Path\$File1 -UseBasicParsing
Start-Process -FilePath "msiexec.exe" -ArgumentList "/i $Path\$File1 /qn /L*v $Path\pdf24-install.log" -Wait
Write-Host "PDF24 Creator installed successfully" -ForegroundColor Green
}
if ("PDF-XChange Editor" -in $missingPrograms) {
$Downloads = @(
@{
Name = "PDF-XChange Editor"
Link = "https://downloads.pdf-xchange.com/EditorV10.x64.msi"
File = "EditorV10.x64.msi"
InstallArgs = "/i `"$Path\EditorV10.x64.msi`" /qn /L*v `"$Path\pdf-x-install.log`""
},
@{
Name = "Visual C++ Redistributable x86"
Link = "https://aka.ms/vs/17/release/vc_redist.x86.exe"
File = "vc_redist.x86.exe"
InstallArgs = "`"$Path\vc_redist.x86.exe`" /quiet /norestart"
},
@{
Name = "Visual C++ Redistributable x64"
Link = "https://aka.ms/vs/17/release/vc_redist.x64.exe"
File = "vc_redist.x64.exe"
InstallArgs = "`"$Path\vc_redist.x64.exe`" /quiet /norestart"
}
)
foreach ($App in $Downloads) {
try {
Invoke-WebRequest -Uri $App.Link -OutFile "$Path\$($App.File)" -UseBasicParsing
if ($App.File.EndsWith(".msi")) {
Start-Process -FilePath "msiexec.exe" -ArgumentList $App.InstallArgs -Wait
} else {
Start-Process -FilePath "cmd.exe" -ArgumentList "/c $($App.InstallArgs)" -Wait
}
Write-Host "$($App.Name) installed successfully" -ForegroundColor Green
}
catch {
Write-Host "Error with $($App.Name): $($_.Exception.Message)" -ForegroundColor Red
}
}
}
# STEP 3: Early exit if no changes were needed
if (-not $missingPrograms -and -not $extraPrograms) {
Write-Host "PDF programs: $($pdfPrograms -join ', ')"
exit 0
}
# STEP 4: Final verification
$finalPdfPrograms = Get-WmiObject Win32_Product | Where-Object { $_.Name -like "*pdf*" } | Select-Object -ExpandProperty Name | Sort-Object
if (Compare-Object $finalPdfPrograms $expected) {
Write-Host "FAIL: Configuration still not correct after remediation"
exit 1
} else {
Write-Host "PDF programs: $($finalPdfPrograms -join ', ')"
exit 0
}