-
Notifications
You must be signed in to change notification settings - Fork 76
/
Uninstall-Application.ps1
89 lines (81 loc) · 4.43 KB
/
Uninstall-Application.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<#
.SYNOPSIS
Uninstalls the applications registered with the set displayname in the Windows installer.
.DESCRIPTION
Searches registry for applications registered with the set displayname.
If any found, the uninstall string is retrieved and used to uninstall the application.
Works with installation made via .MSI as well as some .EXE compilers with unins000.exe used for uninstallation
.NOTES
Filename: Uninstall-Application.ps1
Version: 3.0
Author: Martin Bengtsson
Blog: www.imab.dk
Twitter: @mwbengtsson
Version history:
1.0 - Script created
2.0 - Realized not all applications are properly registered in Windows installer to use msiexec.exe /x as the UninstallString
Some applications are registered with msiexec.exe /i, which requires a slight change in the script below
3.0 - Added support to pass multiple displaynames (applications) to the script: .\Uninstall-Application.ps1 -displayName "Application1","Application2"
.LINK
https://www.imab.dk/uninstall-any-application-in-a-jiffy-using-powershell-and-configuration-manager
#>
[cmdletbinding()]
param(
[Parameter(Mandatory=$true)]
[string[]]$displayName
)
function Uninstall-ApplicationLocalMachine() {
foreach ($object in $displayName) {
Write-Verbose -Verbose -Message "Running Uninstall-ApplicationLocalMachine function"
Write-Verbose -Verbose -Message "Looking for installed application: $object"
$registryPaths = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall","HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
foreach ($path in $registryPaths) {
Write-Verbose -Verbose -Message "Looping through $path"
if (Test-Path -Path $path) {
$installedApps = Get-ChildItem -Path $path -Recurse | Get-ItemProperty | Where-Object {$_.DisplayName -like "*$object*"} | Select-Object Displayname,UninstallString,PSChildName
if ($installedApps) {
Write-Verbose -Verbose -Message "Installed applications matching '$object' found in $path"
foreach ($App in $installedApps) {
if ($App.UninstallString) {
if ($App.UninstallString.Contains("MsiExec.exe")) {
try {
Write-Verbose -Verbose -Message "Uninstalling application: $($App.DisplayName) via $($App.UninstallString)"
Start-Process 'cmd.exe' -ArgumentList ("/c" + "MsiExec.exe /x" + $($App.PSChildName) + " /quiet" + " /norestart") -Wait
} catch {
Write-Error -Message "Failed to uninstall application: $($App.DisplayName)"
}
}
if ($App.UninstallString.Contains("unins000.exe")) {
try {
Write-Verbose -Verbose -Message "Uninstalling application: $($App.DisplayName) via $($App.UninstallString)"
Start-Process 'cmd.exe' -ArgumentList ("/c" + $($App.UninstallString) + " /SILENT" + " /NORESTART") -Wait
} catch {
Write-Error -Message "Failed to uninstall application: $($App.DisplayName)"
}
}
else {
# If script reaches this point, the application is installed with an unsupported installer. Feel free to add further mechanisms.
}
}
}
}
else {
Write-Verbose -Verbose -Message "No installed apps that matches displayname: $object found in $path"
}
}
else {
Write-Verbose -Verbose -Message "Path: $path does not exist"
}
}
}
}
try {
Write-Verbose -Verbose -Message "Script is running"
Uninstall-ApplicationLocalMachine
}
catch {
Write-Verbose -Verbose -Message "Something went wrong during running of the script: $($_.Exception.Message)"
}
finally {
Write-Verbose -Verbose -Message "Script is done running"
}