public/Get-UninstallString.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
<#
.SYNOPSIS
  Returns Uninstall String for installed applications.

.DESCRIPTION
  Providing just the partial name of an application, the result returned will be the uninstall string from the registry
  for x86 and/or x64 program installations.
  
.PARAMETER Application
  Partial name of application for lookup. Asterix not required.

.PARAMETER FullDetail
  Return all details of string lookup.

.INPUTS
  None

.OUTPUTS
  UninstallString object.

.NOTES
  Version: 1.0.1.36
  Author: Copyright � 2020 RePass Cloud Pty Ltd (https://repasscloud.com/). All rights reserved.
  License: Apache-2.0
  Creation Date: 2020-07-17
  Last Updated: 2020-07-27
  
.EXAMPLE
  Get-UninstallString -Application Firefox

.EXAMPLE
  Get-UninstallString -Application Firefox -FullDetail

#>

function Get-UninstallString {
    param(
        [Parameter(Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            HelpMessage='Name of Application to search for.',
            Position=0)]
        [Alias('App')]
        [String]$Application,

        [Parameter(Mandatory=$false,
            HelpMessage='Return all properties of application matching Application name.',
            Position=1)]
        [Alias('Detail')]
        [switch]$FullDetail
    )

    if ($Application -match '^\w.*$') { $Value = '*' + $Application + '*' } #~> no asterisk
    elseif ($Application -match '^\*\w.*$') { $Value = $Application + '*' } #~> asterisk at start only
    elseif ($Application -match '^\w.*\*$') { $Value = '*' + $Application } #~> asterisk at end only
    elseif ($Application -match '^\*\w.*\*$') { $Value = $Application } #~> asterisk at start and end
    else { $Value = '*' + $Application + '*' } #~> catch all backup
    
    if ($FullDetail) {
        $Path = @(
            'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
            'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
        )
        [String]$returnObj = Get-ChildItem -Path $Path | Get-ItemProperty | Where-Object {$_.DisplayName -like "${Value}" } | Select-Object -Property *
        return $returnObj.UninstallString
    }
    else {
        $Path = @(
            'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
            'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
        )
        $returnObj = Get-ChildItem -Path $Path | Get-ItemProperty | Where-Object {$_.DisplayName -like "${Value}" } | Select-Object -Property UninstallString
        return $returnObj
    }
}