-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove-ApplicationByDisplayName.ps1
67 lines (58 loc) · 2.01 KB
/
Remove-ApplicationByDisplayName.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
Function Remove-ApplicationByDisplayName
{
<#
.SYNOPSIS
Remove an application from Windows
.DESCRIPTION
Will accept a DisplayName value that will be searched for in Programs and Features and will
uninstall the applicationg.
.PARAMETER DisplayName
The DisplayName as it found in Programs and Features
.NOTES
Author: Mark Allen
Created: 16/08/2018
.LINK
http://www.markallenit.com/blog/
.EXAMPLE
Get-ApplicationByDisplayName -DisplayName 'Google Chrome'
Will find any entry in Programs and Features named 'Google Chrome and will uninstall
it.
.EXAMPLE
Get-ApplicationByDisplayName -DisplayName '7-Zip * (x64 edition)'
Will find any entry in Programs and Features like '7-Zip * (x64 edition)' and will uninstall it.
'*' is a wildcard that will be substituted by the version number in this example.
#>
param (
[parameter(
Mandatory=$true,
Position = 0
)]
[ValidateNotNullOrEmpty()]
[string]
$DisplayName
)
begin
{
[String]$UninstallString = Get-ArpPropertyByDisplayName -DisplayName $DisplayName -Property QuietUninstallString | Select-Object -First 1
if($null -eq $UninstallString) {
$UninstallString = Get-ArpPropertyByDisplayName -DisplayName $DisplayName | Select-Object -First 1
}
if($null -eq $UninstallString) {
Write-Error -Message "Can't find an uninstall string." -ErrorAction Stop
}
Write-Output "Found uninstall string: $UninstallString"
}
process
{
$UninstallString = [CommandString]::new($UninstallString).ToUninstall()
try {
Write-Output "Executing: " + $UninstallString
Start-Process cmd -ArgumentList "/c $UninstallString" -NoNewWindow -Wait
Write-Output "Finished Executing: " + $UninstallString
}
catch {
Write-Output "Failed to execute: " + $UninstallString
throw
}
}
}