-
Notifications
You must be signed in to change notification settings - Fork 15
/
Winget-InstallDetection.ps1
70 lines (66 loc) · 2.37 KB
/
Winget-InstallDetection.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
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2021 v5.8.195
Created on: 3/7/2022 2:14 PM
Created by: Dave Just
Organization:
Filename: WingetInstallDetection.ps1
===========================================================================
.DESCRIPTION
Simple Win32App detection script. Detects the presence of an uninstall key matching the displayname of the variable $SoftwareName.
If a key is matched, return to Intune that the software is installed.
.EXAMPLE
$SoftwareName = 'Chrome' # Search for an uninstall key with Displayname 'Chrome' for Google Chrome
#>
# Edit the software displayname below
$SoftwareName = ''
function Get-RegUninstallKey
{
param (
[string]$DisplayName
)
$uninstallKeys = @(
"registry::HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall"
"registry::HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
)
$LoggedOnUser = (Get-CimInstance -ClassName Win32_ComputerSystem).UserName
if ($LoggedOnUser){
$UserSID = ([System.Security.Principal.NTAccount](Get-CimInstance -ClassName Win32_ComputerSystem).UserName).Translate([System.Security.Principal.SecurityIdentifier]).Value
$UninstallKeys += @("registry::HKU\$UserSID\Software\Microsoft\Windows\CurrentVersion\Uninstall" , "registry::HKU\$UserSID\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall")
}
$softwareTable =@()
foreach ($key in $uninstallKeys){
if (-Not (Test-Path $Key)){
Write-Warning "$Key not found"
continue
}
$softwareTable += Get-Childitem $key |
foreach {
try {
Get-ItemProperty $_.pspath | Where-Object { $_.displayname } | Sort-Object -Property displayname
}
catch [System.InvalidCastException] {
# Ignore error as I was occasionally getting an invalid cast error on Get-ItemProperty
}
}
}
if ($DisplayName)
{
$softwareTable | Where-Object { $_.displayname -Like "*$DisplayName*" }
}
else
{
$softwareTable | Sort-Object -Property displayname -Unique
}
}
$UninstallKey = Get-RegUninstallKey -DisplayName $SoftwareName
if ($UninstallKey)
{
Write-Output "$($SoftwareName) is installed"
exit 0
}
else
{
exit 1
}