Afternoon everyone…
I found code to check a local system for the presence of a specific software using this powershell script and listing them in an array.
My problem is we use a custom MSI install/uninstaller in powershell which does not support arrays so if more than ONE GUID is found it borks the code so I have two options:
- See below the function… if I can code this “Get-InstalledSoftware” function only to detect the first instance of the GUID and not multiple that would allow me to then re-scan the machine for other instances later on in my code. Right now if it dumps an array of GUIDs causing my uninstaller to break so if the following function can be set to only detect one GUID that would help…
function Get-InstalledSoftware {
<#
.SYNOPSIS
Retrieves a list of all software installed
.EXAMPLE
Get-InstalledSoftware
This example retrieves all software installed on the local computer
.PARAMETER Name
The software title you'd like to limit the query to.
#>
[OutputType([System.Management.Automation.PSObject])]
[CmdletBinding()]
param (
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Name
)
$UninstallKeys = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$null = New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS
$UninstallKeys += Get-ChildItem HKU: -ErrorAction SilentlyContinue | Where-Object { $_.Name -match 'S-\d-\d+-(\d+-){1,14}\d+$' } | ForEach-Object { "HKU:\$($_.PSChildName)\Software\Microsoft\Windows\CurrentVersion\Uninstall" }
if (-not $UninstallKeys) {
Write-Verbose -Message 'No software registry keys found'
} else {
foreach ($UninstallKey in $UninstallKeys) {
if ($PSBoundParameters.ContainsKey('Name')) {
$WhereBlock = { ($_.PSChildName -match '^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$') -and ($_.GetValue('DisplayName') -like "$Name*") }
} else {
$WhereBlock = { ($_.PSChildName -match '^{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}}$') -and ($_.GetValue('DisplayName')) }
}
$gciParams = @{
Path = $UninstallKey
ErrorAction = 'SilentlyContinue'
}
$selectProperties = @(
@{n='GUID'; e={$_.PSChildName}},
@{n='Name'; e={$_.GetValue('DisplayName')}},
@{n='Version'; e={$_.GetValue('DisplayVersion')}}
)
Get-ChildItem @gciParams | Where $WhereBlock | Select-Object -Property $selectProperties
}
}
}
- If not the above then I need to be able to set the command below “Execute-MSI -Action ‘Uninstall’ -Path $GUID” so that it supports an array as when when it finds $GUID its actually many GUIDs and, thus, breaks the MSI uninstall command. Is there an array one can use to support multiple Execute-MSIs? Here is the full code which calls from the function above.
#Declare specific values for first scan.
$GUIDObject = Get-InstalledSoftware "*Adobe Photoshop Elements*"
$GUID = $GUIDObject.GUID
Execute-MSI -Action 'Uninstall' -Path $GUID
Execute-MSI is a basic PS call using simple msiexec /i or /x and auto-pipes itself to a log file.
Thank you community!
e.