Asked by:
Verify path from Registry UninstallString to remove QGIS software

Question
-
Hi!
I'm trying to make a script that will search the registry for uninstall string of different QGIS versions and uninstall them all.
The problem is that on some computers there are few versions of QGIS and some of them are already uninstalled but the registry key is still there and the script fails when it can't find a valid path.
In this specific example when I add to the code "write-host $version.UninstallString" to list paths to uninstallers the output will be like this:
C:\Program Files\QGIS 2.14\Uninstall-QGIS.exe
C:\Program Files\QGIS 2.18\uninstall.exe
C:\Program Files\QGIS Lyon\Uninstall-QGIS.exeThe last path for QGIS Lyon dosn't exist anymore and script fails. How can I include a verification method to check if the path really exist?
Here is the code sample:
Best Regards!
$QGISUninstall = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "QGIS*" } | Select-Object -Property DisplayName, UninstallString foreach ($version in $QGISUninstall) { if ($version.UninstallString) { $uninstall =$version.UninstallString write-host $version.UninstallString Start-Process $version.UninstallString } }
- Edited by binary-lab Monday, July 23, 2018 8:11 AM Changed title to more specific
Monday, July 23, 2018 8:08 AM
All replies
-
help test-path -online
\_(ツ)_/
Monday, July 23, 2018 8:14 AM -
Hello,
Please check below:
$Path = (Test-Path "C:\Program Files\QGIS 2.14","C:\Program Files\QGIS 2.18","C:\Program Files\QGIS Lyon")
if ($Path) {
$QGISUninstall = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "QGIS*" } | Select-Object -Property DisplayName, UninstallString
foreach ($version in $QGISUninstall) {
if ($version.UninstallString) {
$uninstall =$version.UninstallString
write-host $version.UninstallString
Start-Process $version.UninstallString
}
}
}else { Write-Host "Folder doesn't exist"}
(Please remember to "Mark as Helpful/Mark as Answer" if applicable)
- Edited by SystemCenter User Monday, July 23, 2018 8:23 AM
Monday, July 23, 2018 8:22 AM -
Thanks! Your answer was helpful.
Here is my final code.
Get-Process | Where-Object { $_.Name -like "qgis*" } | Select-Object -First 2 | Stop-Process -Force $QGISUninstall = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "QGIS*" } | Select-Object -Property DisplayName, UninstallString foreach ($version in $QGISUninstall) { if ($version.UninstallString) { $uninstall =$version.UninstallString write-host $version.UninstallString if (test-path $version.UninstallString) { Start-Process $version.UninstallString /S -Wait } else { Write-host "Mappen finns inte längre"} } } Start-Sleep -Seconds 10 $rensa = 'HKLM:\SOFTWARE\QGIS*', 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\QGIS*', 'C:\program files\QGIS*', 'C:\Users\Public\Desktop\Qgis*', 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\QGIS*', 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\QGIS*' foreach ($skräp in $rensa) { if (test-path $skräp) { Remove-Item $skräp -Recurse -Force } else { Write-Host "redan rensat"} } Start-Process QGIS-OSGeo4W-3.2.1-1-Setup-x86_64.exe /S -Wait
Monday, July 30, 2018 1:30 PM