-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabadmin-uninstall-program.ps1
200 lines (173 loc) · 8.62 KB
/
labadmin-uninstall-program.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Try silent noGUI uninstall program using some methods
.PARAMETER programName
Name of package to match (if multiple matches will be cancelled)
.PARAMETER uninstallCustomString
Uninstall using custom command and arguments. Example: ... -uninstallCustomString "`"c:\path to\command.exe`" /arg1 /arg2 /arg3"
.PARAMETER uninstallWmiObject
Try WmiObject method to uninstall
.PARAMETER uninstallUninstallPackage
Try Uninstall-Package cmdlet method to uninstall
.PARAMETER uninstallWinget
Try winget method to uninstall
.PARAMETER uninstallRegistryUninstaller
Try uninstall string from registry method to uninstall
.PARAMETER uninstallArgs
Optional argument list to use for uninstall.exe
Typical arguments for silent noGUI are:
* /s
* /S
* /S /v"/qn"
* /SILENT
* /VERYSILENT
* /VERYSILENT /SUPPRESSMSGBOXES
* /quiet
* Try uninstall.exe /? to get specific method
.NOTES
File Name: labadmin-uninstall-program.ps1
Author : Leonardo Marco
#>
Param(
[parameter(Mandatory=$true, ParameterSetName="uninstall")]
[String]$programName,
[parameter(Mandatory=$false, ParameterSetName="uninstall")]
[Switch]$uninstallWmiObject,
[parameter(Mandatory=$false, ParameterSetName="uninstall")]
[Switch]$uninstallUninstallPackage,
[parameter(Mandatory=$false, ParameterSetName="uninstall")]
[Switch]$uninstallWinget,
[parameter(Mandatory=$false, ParameterSetName="uninstall")]
[Switch]$uninstallRegistryUninstaller,
[parameter(Mandatory=$false, ParameterSetName="uninstall")]
[String]$uninstallArgs,
[parameter(Mandatory=$false, ParameterSetName="uninstallcustom")]
[String]$uninstallCustomString
)
#########################################################################################
##### LIST ##############################################################################
#########################################################################################
if(!$uninstallCustomString -AND !$uninstallWmiObject -AND !$uninstallUninstallPackage -AND !$uninstallWinget -AND !$uninstallRegistryUninstaller) { $list=$true }
if($list) {
$list2=Get-Package | Select-Object -Property Name | Where-Object { $_.Name -match $programName }
$literalName=$list2.Name
# Multiple result: show list
if($list2 -is [Array]) { $list2; exit 0 }
# 1 result: Show app info
elseif ($list2) {
# Check REGISTRY UNINSTALLER
$list2; Write-Output "`n"
Write-Output "Available methods`n-----------------"
$app=gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_.DisplayName -eq $literalName }
if(!$app) { $app = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_.DisplayName -eq $literalName } }
if($app) {
$ui=$app.UninstallString
$qui=$app.QuietUninstallString
Write-Output " * uninstallRegistryUninstaller : yes"
Write-Output " Uninstall string : $ui"
Write-Output " Quiet uninstall string : $qui`n"
} else { Write-Output " * uninstallRegistryUninstaller : no" }
# Check WMIOBJECT
if(Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -eq $literalName }) {
Write-Output " * uninstallWmiObject : yes"
} else { Write-Output " * uninstallWmiObject : no" }
# Check WINGET
if((Get-Command winget -ErrorAction SilentlyContinue) -AND (winget list $literalName)) {
Write-Output " * uninstallWinget : yes"
} else { Write-Output " * uninstallWinget : no" }
# Check UNINSTALL-PACKAGE CMDLET
if(Get-Package | Where-Object { $_.ProviderName -ne "Programs" -AND $_.Name -eq "$literalName" } -ErrorAction SilentlyContinue) {
Write-Output " * uninstallUninstallPackage : yes"
} else { Write-Output " * uninstallUninstallPackage : no" }
exit 0
}
exit 1
}
#########################################################################################
##### UNINSTALL #########################################################################
#########################################################################################
# TRY UNINSTALL: custom command
if($uninstallCustomString) {
Write-Output "Trying uninstall using custom command"
# Split uninstallCustomString: uninstallPath + uninstallArgs
$uninstallPath=$uninstallCustomString
if($uninstallPath.StartsWith("`"")) { $uninstallPath=$uninstallCustomString.Split("`"")[1]; $uninstallArgs=$uninstallCustomString.Split("`"")|Select -Skip 2 }
else { $uninstallPath=$uninstallCustomString.Split(" ")[0]; $uninstallArgs=$uninstallCustomString.Split(" ")|Select -Skip 1 }
Write-Output "Executing command: ${uninstallPath} ${uninstallArgs}"
Start-Process -FilePath $uninstallPath -ArgumentList $uninstallArgs -Verb runas -Wait
if(!(Get-Package $literalName -ErrorAction SilentlyContinue)) { Write-Output "Uninstall successful!"; exit 0 }
exit 1
}
# GET LITERAL NAME MATCH
if(!$programName) { Write-Error "-programName param required"; exit 1 }
$literalName=Get-Package | Select-Object -Property Name | Where-Object { $_.Name -match $programName }
if(!$literalName) { Write-Error "Cant find installed program $programName"; exit 1 }
if($literalName -is [Array]) { $literalName; Write-Error "Multiple matching for name: $programName"; exit 1 }
$literalName=$literalName.Name
if($programName -ne $literalName) { Write-Output "Literal program name found: $literalName" }
# TRY UNINSTALL: WmiObject
if($uninstallWmiObject) {
Write-Output "Trying uninstall using WmiObject..."
$app=Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -eq $literalName }
if($app) {
$app.Uninstall()
if(!(Get-Package $literalName -ErrorAction SilentlyContinue)) { Write-Output "Uninstall successful!"; exit 0 }
}
}
# TRY UNINSTALL: Uninstall-Package
if($uninstallUninstallPackage) {
Write-Output "Trying uninstall using Uninstall-Package..."
$app=Get-Package $literalName
if($app) {
Uninstall-Package -Name $literalName -Force
if(!(Get-Package $literalName -ErrorAction SilentlyContinue)) { Write-Output "Uninstall successful!"; exit 0 }
}
}
# TRY UNINSTALL: winget
if($uninstallWinget) {
if(Get-Command winget -ErrorAction SilentlyContinue) {
winget uninstall --exact --force --silent --disable-interactivity --accept-source-agreements $literalName
if(!(Get-Package $literalName -ErrorAction SilentlyContinue)) { Write-Output "Uninstall successful!"; exit 0 }
}
}
# TRY REGEDIT uninstall string
if($uninstallRegistryUninstaller) {
Write-Output "Trying uninstall using register uninstall string path..."
$app=gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_.DisplayName -eq $literalName }
if(!$app) { $app = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_.DisplayName -eq $literalName } }
if($app) {
# Get uninstall string
$uninstallString=$app.UninstallString
if($app.QuietUninstallString) { $uninstallString=$app.QuietUninstallString }
# .MSI UNINSTALL
if($uninstallString -match "msiexec.exe") {
$msiID=($uninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X","").Trim()
Write-Output "Executing MSI uninstall: msiexec.exe /x ${msiID} /qn"
start-process "msiexec.exe" -arg "/x $msiID /qn" -Wait
if(!(Get-Package $literalName -ErrorAction SilentlyContinue)) { Write-Output "Uninstall successful!"; exit 0 }
# .EXE UNINSTALL?
} else {
$uninstallPath=$uninstallString
# Split uninstallString: uninstallPath + uninstallArgs
if($uninstallPath.StartsWith("`"")) { $uninstallPath=$uninstallString.Split("`"")[1]; $uninstallArgs=($uninstallString.Split("`"")|Select -Skip 2)+" ${uninstallArgs}" }
else { $uninstallPath=$uninstallString.Split(" ")[0]; $uninstallArgs=($uninstallString.Split(" ")|Select -Skip 1)+" ${uninstallArgs}" }
Write-Output "Executing uninstall: ${uninstallPath} ${uninstallArgs}"
Start-Process -FilePath $uninstallPath -ArgumentList $uninstallArgs -Verb runas -Wait
if(!(Get-Package $literalName -ErrorAction SilentlyContinue)) { Write-Output "Uninstall successful!"; exit 0 }
Write-Output "ERROR!: Cant found uninstall method for $literalName package
Typical uninstall.exe uninstallArgs parameters are:
* /s
* /S
* /S /v`"/qn`"
* /SILENT
* /VERYSILENT
* /VERYSILENT /SUPPRESSMSGBOXES
* /quiet
* Try & `"$uninstallPath`" /? to get specific method"
}
}
}
# NO METHOD FOUND!
Write-Error "No uninstall method found!"
exit 1