Getting Installed Software Remotely

by Nov 11, 2015

In a previous tip we introduced the Get-Software function that was able to retrieve installed software from local computers.

If you have enabled PowerShell remoting on remote systems (enabled by default on Windows Server 2012 and better), and if you have the appropriate permissions, try this enhanced version. It supports both local and remote calls:

#requires -Version 2

function Get-Software
{
    param
    (
        [string]
        $DisplayName='*', 

        [string]
        $UninstallString='*',

        [string[]]
        $ComputerName 
    )

    [scriptblock]$code = 
    {

        param
        (
        [string]
        $DisplayName='*', 

        [string]
        $UninstallString='*'

        )
      $keys = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*', 
       'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
    
      Get-ItemProperty -Path $keys | 
      Where-Object { $_.DisplayName } |
      Select-Object -Property DisplayName, DisplayVersion, UninstallString |
      Where-Object { $_.DisplayName -like $DisplayName } |
      Where-Object { $_.UninstallString -like $UninstallString }

    }
    if ($PSBoundParameters.ContainsKey('ComputerName'))
    {
    Invoke-Command -ScriptBlock $code -ComputerName $ComputerName -ArgumentList $DisplayName, $UninstallString
    }
    else
    {
        & $code -DisplayName $DisplayName -UninstallString $UninstallString
    }
}

Note how this function wraps the code that does the software lookup in a script block. It then checks $PSBoundParameters to see whether the user has used the -ComputerName parameter. If not, the code is run locally.

Else, Invoke-Command is used to run it on the specified remote computer(s). Invoke-Command forwards the filtering arguments to the remote code in this case.

Twitter This Tip! ReTweet this Tip!