-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuninstall_software.ps1
More file actions
206 lines (169 loc) · 7.52 KB
/
uninstall_software.ps1
File metadata and controls
206 lines (169 loc) · 7.52 KB
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
201
202
203
204
205
206
<#
.Synopsis
Allows listing and uninstalling most software on Windows. There will be a best effort to uninstall silently if there is no silent uninstall string provided.
.DESCRIPTION
Allows listing and uninstalling most software on Windows. There will be a best effort to uninstall silently if there is no silent uninstall string provided.
.INPUTS
The following script arguments are available:
-help What you are reading now
-id Filter installed software by ID
-uninstall Uninstall a specific software based on ID
Examples:
-id '{0D34D278-5FAF-4159-A4A0-4E2D2C08139D}_is1'
-id '{0D34D278-5FAF-4159-A4A0-4E2D2C08139D}_is1' -uninstall
.NOTES
See https://github.com/subzdev/uninstall_software/blob/main/uninstall_software.ps1 . If you have extra additions please feel free to contribute and create PR
v3.0 - 9/9/2021
#>
[CmdletBinding()]
param(
[switch]$help,
[string]$id,
[switch]$uninstall,
[switch]$force
)
$ErrorActionPreference = 'silentlycontinue'
$UsernameObj = (Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object username).username -Split "\\"
$Username = $UsernameObj[1]
$SID = (Get-WmiObject -Class Win32_UserAccount -Filter "Domain = '$env:userdomain' AND Name = '$Username'").SID
$UserAppsPath = Get-ItemProperty Registry::\HKEY_USERS\$SID\software\microsoft\windows\currentversion\uninstall\*
$AllUsersAppsPaths = @("HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\SOFTWARE\\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall\*")
$ApplicationsObj = New-Object System.Collections.ArrayList
Function Get-Applications {
ForEach ($App in $UserAppsPath) {
$ApplicationsObj.Add($App) | Out-Null
}
ForEach ($App in Get-ItemProperty $AllUsersAppsPaths) {
If (!$App.SystemComponent -And $App.UninstallString) {
$ApplicationsObj.Add($App) | Out-Null
}
}
$ApplicationsObj | Sort-Object DisplayName | Format-Table -Wrap -AutoSize -Property @{L = "$(($ApplicationsObj | Measure-Object).Count) results"; E = { "Name: $($_.DisplayName) `nID: $($_.PSChildName) `nVersion: $($_.DisplayVersion) `n" } }
}
Function Get-Application {
ForEach ($App in $Applicationsobj) {
If ($App.PsChildName -eq $id) {
Return $App
}
}
}
Function Get-UninstallStatus ($App) {
Start-Sleep 1
$procsWithParent = Get-WmiObject -ClassName "win32_process" | Select-Object ProcessId, ParentProcessId
$orphaned = $procsWithParent | Where-Object -Property ParentProcessId -NotIn $procsWithParent.ProcessId
$nowtime = get-date
$p = ForEach ($Process in Get-Process | Where-Object -Property Id -In $orphaned.ProcessId) {
If (($nowtime - $Process.StartTime).totalSeconds -le 5) {
$Process.ID
}
}
Do {
If ($p) {
$UninstallProcess = Get-Process -Id $p
$AllUsersAppUninstall = Get-ItemProperty $AllUsersAppsPaths | Where-object { $_.PSChildName -match $id }
}
Else {
$UninstallProcess = Get-Process -Id $proc.Id
$AllUsersAppUninstall = Get-ItemProperty $AllUsersAppsPaths | Where-object { $_.PSChildName -match $id }
}
}Until(!$UninstallProcess)
If ($AllUsersAppUninstall) {
Write-Warning "$($App.DisplayName) was not uninstalled."
Write-Output "`r"
}
Else {
Write-Output "$($App.DisplayName) was uninstalled successfully."
Write-Output "`r"
}
}
Function Uninstall-Application($App, $UninstallString) {
If ($App) {
$SilentUninstallArguments = "/S /SILENT /VERYSILENT /NORESTART"
Write-Output "Found $($App.DisplayName) [$($App.PSChildName)]"
Write-Output "Attempting best effort silent uninstall..."
Write-Output "`r"
If ($App.WindowsInstaller) {
$MsiArguments = $UninstallString -Replace "MsiExec.exe /I", "/X" -Replace "MsiExec.exe ", ""
$proc = Start-Process -FilePath msiexec -ArgumentList "$MsiArguments /quiet" -PassThru
Wait-Process -InputObject $proc
Start-Sleep 3
Get-UninstallStatus $App
}
If (!$App.WindowsInstaller) {
If ($UninstallString -Match '"') {
$UninstallStringObj = $UninstallString -Split ('"')
$Path = $UninstallStringObj[1].Trim()
$ArgumentsObj = for ($i = 2; $i -le $UninstallStringObj.Count - 1; $i++) { $UninstallStringObj[$i] }
$Arguments = $ArgumentsObj.Trim()
}
ElseIf ($UninstallString -NotMatch '"') {
$UninstallStringObj = $UninstallString.IndexOf("/")
If ($UninstallStringObj -eq -1) {
$Path = $UninstallString
}
Else {
$Path = $UninstallString.Substring(0, $UninstallStringObj)
$ArgumentsObj = $UninstallString.Substring($UninstallStringObj + 1)
$Arguments = "/" + $ArgumentsObj
}
}
If ($UninstallString -Match '"' -And !$Arguments) {
$proc = Start-Process -FilePath $Path -ArgumentList $($SilentUninstallArguments) -PassThru
Wait-Process -InputObject $proc
Start-Sleep 3
Get-UninstallStatus $App
}
ElseIf ($UninstallString -Match '"' -And $Arguments) {
$proc = Start-Process -Filepath $Path -ArgumentList $Arguments -PassThru
Wait-Process -InputObject $proc
Start-Sleep 3
Get-UninstallStatus $App
}
ElseIf ($uninstallString -NotMatch '"' -And $Arguments) {
$proc = Start-Process -Filepath $Path -ArgumentList $Arguments -PassThru
Wait-Process -InputObject $proc
Start-Sleep 3
Get-UninstallStatus $App
}
ElseIf ($uninstallString -NotMatch '"' -And !$Arguments) {
$proc = Start-Process -Filepath $UninstallString -ArgumentList $($SilentUninstallArguments) -PassThru
Wait-Process -InputObject $proc
Start-Sleep 3
Get-UninstallStatus $App
}
}
}
Else {
Write-Output "`r"
Write-Output "The application associated with the specified ID is not installed on $env:computername."
Write-Output "`r"
}
}
If (!$help -And !$id -And !$uninstall -And !$force) {
$Apps = Get-Applications
$Apps
}
If (!$help -And $id -And !$uninstall -And !$force) {
Get-Applications | Out-Null
$App = Get-Application
$App
}
If (!$help -And $id -And $uninstall -And !$force) {
Get-Applications | Out-Null
$App = Get-Application
$UninstallString = If ($App.QuietUninstallString) { $App.QuietUninstallString } Else { $App.UninstallString }
Uninstall-Application $App $UninstallString
}
If ($help -And !$id -And !$uninstall -And !$force) {
Write-Output "`r"
Write-Output "The following script arguments are available:"
Write-Output "`t -help `t `t `t What you are reading now"
Write-Output "`t -id `t `t `t Filter installed software by ID"
Write-Output "`t -uninstall `t `t Uninstall a specific software based on ID"
Write-Output "`r"
Write-Output "Examples:"
Write-Output "`t -id '{0D34D278-5FAF-4159-A4A0-4E2D2C08139D}_is1'"
Write-Output "`t -id '{0D34D278-5FAF-4159-A4A0-4E2D2C08139D}_is1' -uninstall"
Write-Output "`r"
Write-Output "`r"
}