1

I have couple of remote devices, where in i need to compare the add remove programs the devices using powershell. how can i do it. Can anyone help me.

Example:-

Need to compare appremove programs from "A Computer" with "A1 Computer" and "B Computer" with "B2 Computer".

Ananth
  • 11
  • 3
  • sure, that's easy enough...can you show us where you're having trouble with in your code? – Abraham Zinala Mar 10 '21 at 17:22
  • $Computer = get-content "C:\computername.txt" Get-WmiObject Win32_Product -ComputerName $Computer| select PSComputerName, Name,Version | export-csv c:\tools\Current-Installed-Applications.csv Currently iam using above code, where in this will work only to get the addremove data from multple remote devices. But iam checking for data to be pulled from multiple devices and then compare. – Ananth Mar 10 '21 at 17:26
  • So you want to compare the software against each computer? That makes sense if youre only comparing 2 but, if you compare multiple it doesn't make sense as far on what youre looking for. You can use `Foreach($Computer in $Computers){}` to iterate through the computers returning the data for each one. Do you have a "master" pc youd like to compare against? – Abraham Zinala Mar 10 '21 at 18:07
  • in my scenario, "A computer" is master PC to "A1 Computer" and "B Computer" master PC to "B1 Computer" and similarly i have 50 master computers and 50 other computers(here each computer is master to only one other computer) – Ananth Mar 10 '21 at 18:20

1 Answers1

1

Microsoft Scripting Guy recommends checking the registry instead of the very slow Win32_Product class. Here's their example where they search a list of computers by name:

$computers = Import-Csv “C:\PowerShell\computerlist.csv”
$array = @()

foreach($pc in $computers){
    $computername=$pc.computername

    #Define the variable to hold the location of Currently Installed Programs
    $UninstallKey=”SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall” 

    #Create an instance of the Registry Object and open the HKLM base key
    $reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey(‘LocalMachine’,$computername) 

    #Drill down into the Uninstall key using the OpenSubKey Method
    $regkey=$reg.OpenSubKey($UninstallKey) 

    #Retrieve an array of string that contain all the subkey names
    $subkeys=$regkey.GetSubKeyNames() 

    #Open each Subkey and use GetValue Method to return the required values for each
    foreach($key in $subkeys){
        $thisKey=$UninstallKey+”\\”+$key 
        $thisSubKey=$reg.OpenSubKey($thisKey) 
        $obj = New-Object PSObject
        $obj | Add-Member -MemberType NoteProperty -Name “ComputerName” -Value $computername
        $obj | Add-Member -MemberType NoteProperty -Name “DisplayName” -Value $($thisSubKey.GetValue(“DisplayName”))
        $obj | Add-Member -MemberType NoteProperty -Name “DisplayVersion” -Value $($thisSubKey.GetValue(“DisplayVersion”))
        $obj | Add-Member -MemberType NoteProperty -Name “InstallLocation” -Value $($thisSubKey.GetValue(“InstallLocation”))
        $obj | Add-Member -MemberType NoteProperty -Name “Publisher” -Value $($thisSubKey.GetValue(“Publisher”))
        $array += $obj
    } 
}
$array | Where-Object { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion

And we can compare lists of objects with Compare-Object to see what software is missing from the B PCs:

$ComputerA = $array | Where-Object { $_.ComputerName -like 'ComputerA' } 
$ComputerB = $array | Where-Object { $_.ComputerName -like 'ComputerB' } 

Compare-Object $ComputerA $ComputerB -Property DisplayName,DisplayVersion

DisplayName            DisplayVersion SideIndicator
-----------            -------------- -------------
Notepad++ (64-bit x64) 7.8.9          <=           
Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16