Comparing two lists in Powershell and outputting the similarities

Viewed 64

I'm trying to run reports of software our users will get on new computers. I have one list "Reference Software", which is all the software they currently have installed. Then another list, "Difference Software", which is all the software in our company that we have to manually install. I'm trying to compare these two lists and output what items are in both lists. Looking this up for weeks now, I've taken what others have suggested and try to make it work, but nothing yet.

Has anyone done something similar and got it to work???

    $ComputerName = get-content c:\temp\ComputerList.txt


Get-InstalledSoftware $ComputerName | Out-File -FilePath C:\Temp\DifferenceSoftware.csv


$Path = "C:\Temp"
$DifferenceSoftware = Import-Csv -Path "$($path)\DifferenceSoftware.csv"
$ReferenceSoftware = Import-Csv -Path "$($path)\ReferenceSoftware.csv"


$combine = @()

foreach ($first in $DifferenceSoftware) {
  foreach ($second in $ReferenceSoftware) {
    if ($second -eq $first) {
        $match = New-Object PSObject
        $match | Add-Member Noteproperty $first
        $combine += $match
    }
  }
}
$combine | Export-Csv -Path "$($path)\combined.csv"
1 Answers

Take a look at this: https://adamtheautomator.com/powershell-compare-arrays/

I hope it can help.

$ComputerName = get-content c:\temp\ComputerList.txt

Get-InstalledSoftware $ComputerName | Out-File -FilePath C:\Temp\DifferenceSoftware.csv


$Path = "C:\Temp"
$DifferenceSoftware = Import-Csv -Path "$($path)\DifferenceSoftware.csv"
$ReferenceSoftware = Import-Csv -Path "$($path)\ReferenceSoftware.csv"


#i hope this works for u as well -- Where-Object returns all items contained in both arrays
$combine = ($ReferenceSoftware | Where-Object {$_ -in $DifferenceSoftware})
$combine | Export-Csv -Path "$($path)\combined.csv"
Related