Functions/Get-InstalledPrograms.ps1


function Get-InstalledPrograms {
    [CmdletBinding()]
    param (
        [Parameter()] [string] $SearchString,
        [Parameter()] [switch] $CopyToClipboard
    )

    $Installed1 = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*
    $Installed2 = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
    $Installed3 = Get-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*

    $Installed = $Installed1 + $Installed2 + $Installed3

    $Installed = $Installed | Where-Object DisplayName

    if ($SearchString) {

        $Apps = $Installed | Where-Object { ($_.DisplayName -Like "*$($SearchString)*") -or ($_.Publisher -Like "*$($SearchString)*") }

    } else {
        $Apps = $Installed
    }


    $Apps = $Apps | Select-Object PSChildName, DisplayName, DisplayVersion, Publisher, InstallDate, UninstallString

    $Apps = $Apps | Sort-Object DisplayName


    $return = @()
    foreach ($r in $Apps) {
        if($r.UninstallString -like "*msiexec*") {
            # $TmpUninstallstring = $r.UninstallString -split " "
            # $PSUninstallString = "Start-Process $($TmpUninstallstring[0]) -ArgumentList `"$($TmpUninstallstring[1])`""
            $PSUninstallString = "cmd /c 'msiexec.exe /X$(($r.PSChildName))'"
            $PSUninstallString = "Start-Process 'msiexec.exe' -Argumentlist '/X$(($r.PSChildName)) /qntr3'"
        } else {
            $PSUninstallString = "Start-Process '$($r.UninstallString)'"
        }
        # $PSUninstallString = "cmd /c '$(($r.UninstallString)'"
        $obj = [PSCustomObject]@{
            PSChildName       = $r.PSChildName
            DisplayName       = $r.DisplayName
            DisplayVersion    = $r.DisplayVersion
            Publisher         = $r.Publisher
            InstallDate       = $r.InstallDate
            UninstallString   = $r.UninstallString
            PSUninstallString = $PSUninstallString
        }
        $return += $obj

    }

    if ($CopyToClipboard) {
        ($return | Select-Object -Last 1).PSUninstallString | clip
    }

    return $return
# $return.PSChildName

}