This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
/
Invoke-ApplicationRemoval
75 lines (69 loc) · 3.37 KB
/
Invoke-ApplicationRemoval
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
#Requires -Version 3.0
Function Invoke-ApplicationRemoval {
<#
.SYNOPSIS
Uninstalls an application
.DESCRIPTION
Uninstalls one or more applications by searching the registry based on the 'DisplayName' that matches your filter.
It is recommended that you run this function with the -WhatIf parameter first to ensure that it will uninstall the expected application(s)
By default, a 'like' search is performed; meaning if your filter is 'Adobe Acrobat', it would find Adobe Acrobat Reader DC' AND 'Adobe Acrobat Reader Pro' and attempt removal.
If this is not desired, you can use the switch -Exact and change your filter to 'Adobe Acrobat Reader DC' and then only that exact application will be removed.
.PARAMETER UninstallSearchFilter
The application name that you are looking to uninstall. This can be part of a name and a 'like' search will be performed by default.
The search is made against the applications 'DisplayName' found in the registry:
HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
or
HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
.EXAMPLE
PS C:\Scripts> 'firefox' | Invoke-ApplicationRemoval -WhatIf
What if: Performing the operation "Uninstall" on target "Mozilla Firefox 63.0.3 (x64 en-GB)"
What if: Performing the operation "Uninstall" on target "Mozilla Firefox 63.0.3 (x86 en-GB)"
.EXAMPLE
PS C:\Scripts> 'reader dc' | Invoke-ApplicationRemoval -WhatIf
What if: Performing the operation "Uninstall" on target "Adobe Acrobat Reader DC"
.PARAMETER Exact
Does not perform a 'like' search. Eg if your filter is 'Java', only applications with a display name exactly of 'Java' will be removed.
Performs a removal of any application whose registry DisplayName is an exact match for your filter.
.EXAMPLE
Invoke-ApplicationRemoval "Adobe Acrobat Reader DC" -Exact
If installed, Adobe Acrobat Reader DC will be uninstalled. If Adobe Acrobat Reader Pro is also installed, this will remain.
.NOTES
Created by OH
https://www.fearthepanda.com
Version: 1.0.1
#>
[CmdletBinding(SupportsShouldProcess=$True)]
param (
[parameter (Mandatory=$true,
ValueFromPipeline = $true)]
[string[]]
$UninstallSearchFilter,
[Switch]
$Exact
)
Begin {
$WorkingDir = $MyInvocation.PSScriptRoot
$RegUninstallPaths = @(
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')
}
process {
foreach ($Filter in $UninstallSearchFilter) {
foreach ($Path in $RegUninstallPaths) {
if (Test-Path $Path) {
if ($Exact) {
$Results = Get-ChildItem $Path | Where {$_.GetValue('DisplayName') -eq $Filter}
} else {
$Results = Get-ChildItem $Path | Where {$_.GetValue('DisplayName') -Like "*$Filter*"}
}
Foreach ($Result in $Results) {
if ($PSCmdlet.ShouldProcess($Result.getvalue('displayname'),"Uninstall")) {
Start-Process 'C:\Windows\System32\msiexec.exe' "/x $($Result.PSChildName) /qn" -Wait -NoNewWindow
}
}
}
}
}
}
end {}
}