Get-InstalledWin32Program.psm1

function Get-InstalledWin32Program
{
<#
.Synopsis
   Command to get all the installed programs
.DESCRIPTION
   Using this command you can give multiple computer names as an input along with credential(optional) to fetch list of installed programs.
.EXAMPLE
<#
.Synopsis
   Command to read the stealth client dashboard exe file information
.DESCRIPTION
   Using this command you can check the stealth client version information
.EXAMPLE
    Get-StealthClientVersionInfo -ComputerName localhost
.EXAMPLE
    Get-StealthClientVersionInfo -ComputerName localhost -Credential (Get-Credential)
.EXAMPLE
    Get-StealthClientVersionInfo -Session (New-PSSession -Computer LocalHost)
.INPUTS
   1. Computername
   2. Credential as an optional input
.OUTPUTS
   List of installed programs
.COMPONENT
   Operating system
.NOTES
    When you feed a session(s) as an input then the those sessions will not be closed but if you specify use -ComputerName parameter then at the end of the execution all the PSSessions will be closed.
#>

    [CMDLetBinding()]
    Param(
        [Parameter(Mandatory=$true, ParameterSetName="Normal")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [string[]]$ComputerName,

        [Parameter(Mandatory=$false, ParameterSetName="Normal")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [pscredential]$Credential,

        [Parameter(Mandatory=$true, ParameterSetName="PSSession")]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.Runspaces.PSSession[]]$Session
    )
    if($PSCmdlet.ParameterSetName -ne "PSSession")
    {
        $shouldPSSessionRemoved = $true
        $targetList = $ComputerName
    }
    else
    {
        $shouldPSSessionRemoved = $false
        $targetList = $Session
    }
    [int]$i = 0
    [int]$max = $targetList.Count
    [Array]$stealthSoftwareJobs = @()
    foreach($targetIdentity in $targetList)
    {
        Write-Progress -Activity "Fetching Stealth client information" -Status "InProgress" -Id 0 -PercentComplete (($i/$max)*100) -CurrentOperation "Working on $targetIdentity :: $i/$max" -Completed:$false
        try
        {
            if($PSCmdlet.ParameterSetName -eq "PSSession")
            {
                $session_temp = $targetIdentity
            }
            else
            {
                if($PSBoundParameters.ContainsKey("Credential"))
                {
                    $session_temp = New-PSSession -ComputerName $targetIdentity -Credential $Credential -ErrorAction Stop
                }
                else
                {
                    $session_temp = New-PSSession -ComputerName $targetIdentity -ErrorAction Stop
                }
            }
            $stealthSoftwareJobs+= Invoke-Command -Session $session_temp -ScriptBlock{ Get-WmiObject -Class Win32_InstalledWin32Program -ErrorAction Stop } -ErrorAction Stop -AsJob
        }
        catch
        {
            Write-Error $_.ToString() -ErrorAction Continue
        }
        $i++
    }
    if($stealthSoftwareJobs.Count -gt 0)
    {
        $results = $stealthSoftwareJobs | Receive-Job -Wait -AutoRemoveJob -Force
        Write-Output $results
    }
    if($shouldPSSessionRemoved -eq $true)
    {
        Get-PSSession | Remove-PSSession
    }
    Write-Progress -Activity "Fetching Stealth client settings.xml information" -Status "Completed" -Id 0 -PercentComplete 100 -CurrentOperation "" -Completed:$true
}