Private/ComputerStats/_GetInstalledSoftware.ps1

Function _GetInstalledSoftware {
    <#
    .SYNOPSIS
        Get-InstalledSoftware retrieves a list of installed software
    .DESCRIPTION
        Get-InstalledSoftware opens up the specified (remote) registry and scours it for installed software. When found it returns a list of the software and it's version.
    .NOTES
        Author: Anthony Howell
 
        To add directories, add to the LMkeys (LocalMachine)
    #>

    Param(
        [Parameter(Mandatory = $true)]
        [string]$Identity
    )
    Begin {
        $ScriptBlock = { $lmKeys = 'Software\Microsoft\Windows\CurrentVersion\Uninstall', 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
            $lmReg = [Microsoft.Win32.RegistryHive]::LocalMachine
            $cuKeys = 'Software\Microsoft\Windows\CurrentVersion\Uninstall'
            $cuReg = [Microsoft.Win32.RegistryHive]::CurrentUser
            $masterKeys = @()
            $remoteCURegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($cuReg, $Identity)
            $remoteLMRegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($lmReg, $Identity)
            foreach ($key in $lmKeys) {
                $regKey = $remoteLMRegKey.OpenSubkey($key)
                foreach ($subName in $regKey.GetSubkeyNames()) {
                    foreach ($sub in $regKey.OpenSubkey($subName)) {
                        $masterKeys += (New-Object PSObject -Property @{
                                'ComputerName'     = $env:COMPUTERNAME
                                'Name'             = $sub.getvalue('displayname')
                                'SystemComponent'  = $sub.getvalue('systemcomponent')
                                'ParentKeyName'    = $sub.getvalue('parentkeyname')
                                'Version'          = $sub.getvalue('DisplayVersion')
                                'UninstallCommand' = $sub.getvalue('UninstallString')
                                'InstallDate'      = if ($sub.getvalue('InstallDate')) {
                                    Get-Date ([datetime]::parseexact($sub.getvalue('InstallDate'), 'yyyyMMdd', $null)) -Format 'MM/dd/yyyy'
                                }
                                else {
                                    '10/18/1989'
                                }
                                'RegPath'          = $sub.ToString()
                            })
                    }
                }
            }
            foreach ($key in $cuKeys) {
                $regKey = $remoteCURegKey.OpenSubkey($key)
                if ($null -ne $regKey) {
                    foreach ($subName in $regKey.getsubkeynames()) {
                        foreach ($sub in $regKey.opensubkey($subName)) {
                            $masterKeys += (New-Object PSObject -Property @{
                                    'ComputerName'     = $env:COMPUTERNAME
                                    'Name'             = $sub.getvalue('displayname')
                                    'SystemComponent'  = $sub.getvalue('systemcomponent')
                                    'ParentKeyName'    = $sub.getvalue('parentkeyname')
                                    'Version'          = $sub.getvalue('DisplayVersion')
                                    'UninstallCommand' = $sub.getvalue('UninstallString')
                                    'InstallDate'      = if ($sub.getvalue('InstallDate')) {
                                        Get-Date ([datetime]::parseexact($sub.getvalue('InstallDate'), 'yyyyMMdd', $null)) -Format 'MM/dd/yyyy'
                                    }
                                    else {
                                        '10/18/1989'
                                    }
                                    'RegPath'          = $sub.ToString()
                                })
                        }
                    }
                }
            }
            $woFilter = { $null -ne $_.name -AND $_.SystemComponent -ne '1' -AND $null -eq $_.ParentKeyName }
            $props = 'Name', 'Version', 'ComputerName', 'Installdate', 'UninstallCommand' #, 'RegPath'
            $masterKeys = ($masterKeys | Where-Object $woFilter | Select-Object $props | Sort-Object Name)
            $masterKeys
        }
    }
    Process {
        if (!(Test-Connection -ComputerName $Identity -Count 1 -Quiet)) {
            $MessageSplat = @{
                MessageText  = "Unable to contact $Identity. Please verify its network connectivity and try again."
                MessageIcon  = 'Hand'
                ButtonType   = 'OK'
                MessageTitle = 'Error'
            }
            _ShowMessageBox @MessageSplat
            Break
        }

        if ($Identity -eq $env:COMPUTERNAME) {
            $Output = Invoke-Command -ScriptBlock $ScriptBlock
        }
        else {
            try {
                $Session = New-PSSession @SessionSplat -ComputerName $Identity -ErrorAction Stop
            }
            catch {
                $MessageSplat = @{
                    MessageText  = "Unable to create session on $Identity. Please verify your access and try again."
                    MessageIcon  = 'Hand'
                    ButtonType   = 'OK'
                    MessageTitle = 'Error'
                }
                _ShowMessageBox @MessageSplat
                Break
            }

            $Output = Invoke-Command -Session $Session -ScriptBlock $ScriptBlock
        }
    }
    End {
        return $Output
    }
}