Using foreach loop in powershell to compare two csv files

Springer, Brian 1 Reputation point
2022-09-21T01:25:30.537+00:00

Hello,
I'm trying to compare two csv files using Powershell. One .csv is $ReferenceSoftware which is a list of about 500 software titles and then $DifferenceSoftware, which is what each user currently has installed. Ideally, I want to end up with a .csv file of only the software titles that appear on both lists. I've tried using the compare object with the -include equal flag but that looks to only give the == results when the exact cell row match. Do I need to use a foreach loop to so it takes each cell in $DifferenceSoftware and compares it with each individual cell in $ReferenceSoftware? Thanks in advance for any potential help you may be able to provide!

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-09-21T02:08:12.023+00:00

    In simple terms, you want the intersection of the two sets.

    You haven't provided the names of the CSV columns that hold the names of the software, or the names of any other columns. This code will produce the intersection of the sets and export only the values that appear in both of the sets. It doesn't output a CSV, but just the values in the intersection.

    $Ref = @{}  
    $Intersection = @{}  
      
    # populate the reference set  
    Import-CSV c:\junk\ReferenceSoftware.csv |  
        ForEach-Object{  
            $Ref[$_.RefSoftwareName] = ""  
        }  
    # process the difference set  
    # and find the intersection of the sets  
    Import-Csv c:\junk\DifferenceSoftware.csv |  
        ForEach-Object{  
            if ($Ref.ContainsKey($_.DifSoftwareName)){  
                $Intersection[$_.DifSoftwareName] = "" # populate the intersection  
            }  
        }  
      
    $Intersection.Keys |  
        Out-File c:\junk\Intersection.txt  
    

  2. Limitless Technology 44,746 Reputation points
    2022-09-23T10:18:11.35+00:00

    Hello there,

    The PowerShellImport-Csv cmdlet is an excellent way of reading data from a tabulated source such as a CSV file. You can use the ForEach loop to then iterate over each row in the CSV data.

    In the below script you can replace user list with reference software and user data with difference software.

    $Path = "C:\PowerShell"
    $UserList = Import-Csv -Path "$($path)\Userlisr.csv"
    $UserData = Import-Csv -Path "$($path)\UserData.csv"
    $UserOutput = @()

    ForEach ($name in $UserList)  
    {  
    
        $userMatch = $UserData | where {$_.UserName -eq $name.usernames}  
        If($userMatch)  
        {  
            # Process the data  
    
            $UserOutput += New-Object PsObject -Property @{UserName =$name.usernames;column1 =$userMatch.column1;column2 =$userMatch.column2}  
        }  
        else  
        {  
        $UserOutput += New-Object PsObject -Property @{UserName =$name.usernames;column1 ="NA";column2 ="NA"}  
        }  
    }  
    

    $UserOutput | ft

    -----------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.