compare 2csv whithin if statments

Ed7 96 Reputation points
2021-12-06T13:00:45.943+00:00

Hello,

I would like to compare 2 csv values (Names and MD5).

The script I do have so far does not what I aim for. I want it to display whether the values match or not and for each action to export as csv file.
Could help me on this please.

Below is my script

$file1 = import-csv "C:\pathtofile.csv"
$file2 = import-csv "C:\Users\users\Desktop\file.csv"

Compare-Object -ReferenceObject $file1 -DifferenceObject $file2 -Property 'Name', 'Hash' -IncludeEqual

first check that properties match (can omit this step if you know for sure they will be)

if(Compare-Object $file1.Hash -eq $file2.Hash)
{
trow "Properties are not the same!" $file1.Hash $file2.Hash
}

pass properties list to Compare-Object

else
{
Compare-Object $file1 $file2 -Property Hash
}

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,084 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,458 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,638 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 44,696 Reputation points
    2021-12-06T21:19:04.363+00:00

    When you post code please use the "Code Sample" editor. The icon is the 5th from the left on the Format Bar and has the graphic "101 010". The normal editor makes a mess of code, as you can see by looking at your post!

    In any case, is this more-or-less what you want to do?

    $f1 = [PSCustomObject]@{Hash = "abc"},[PSCustomObject]@{Hash = "def"},[PSCustomObject]@{Hash = "ghi"},[PSCustomObject]@{Hash = "jkl"}
    $f2 = [PSCustomObject]@{Hash = "abc"},[PSCustomObject]@{Hash = "xyz"},[PSCustomObject]@{Hash = "ghi"},[PSCustomObject]@{Hash = "jkl"}
    
    $f1p = $f1.psobject[0].Properties.name | Sort-Object
    $f2p = $f2.psobject[0].Properties.name | Sort-Object
    
    $x = Compare-Object $f1p $f2p
    if ($x.Length -ne 0){
        Throw "Properties are not the same!`r`n$f1p`r`n`r`n$f2p"
    }
    
    Compare-Object $f1 $f2 -IncludeEqual -Property Hash |
        Where-Object {$_.SideIndicator -eq "=="} |                  # just find what's equal
            Select-Object Hash |
                Export-Csv c:\junk\equalvalues.csv -NoTypeInformation
    
    Compare-Object $f1 $f2 -Property Hash |
        Where-Object {$_.SideIndicator -eq "=>"} |
            Select-Object Hash |
                Export-CSV c:\junk\NotInReferenceSet.csv -NoTypeInformation
    
    Compare-Object $f1 $f2 -Property Hash |
        Where-Object {$_.SideIndicator -eq "<="} |
            Select-Object Hash |
                Export-CSV c:\junk\NotInDifferenceSet.csv -NoTypeInformation
    
    0 comments No comments

  2. Ed7 96 Reputation points
    2021-12-07T09:28:24.793+00:00

    Hi,

    Its nearly there but I need to have the information displayed on the screen and keep the outcomes on a csv file.

    The code looks good


  3. Limitless Technology 39,336 Reputation points
    2021-12-07T20:02:27.467+00:00

    Hello @Ed7

    I would use the Compare-Object: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7.2&viewFallbackFrom=powershell-6

    For example I have this process for disabled users in my AD:

    TEST_AD.csv - this is my csv file export from AD from all my 'active' users. (System of Record).  
    TEST_APP.csv - this is my csv file from the application.  
    TEST_DISABLED_APP_USERS.csv - this is my csv file that I would upload to the application to disable any users not found in the TEST_AD.csv but are found in the TEST_APP.csv file.  
      
    $ADCSV = Import-CSV -Path "C:\temp\TestCSV\TEST_AD.csv"  
    $APPCSV = Import-CSV -Path "C:\temp\TestCSV\TEST_APP.csv"  
    #for the compare I chose the "Email" header in the CSV file as my 'primary key'.  Something you know will exist to identify items (users) in each file that must exist in both files.  
    $compareCSV = Compare-Object $ADCSV $APPCSV -Property Email -IncludeEqual -PassThru  
    $output = ForEach ($item in $compareCSV) {  
      
        If ($item.SideIndicator -eq "=>") {  
            #this item (aka User) exists in the 'difference set' (TEST_APP.csv file) so we'll need to add them to our export CSV.  
            [pscustomobject]@{  
                #needed to create our output csv file.  
                #first column is the export CSV headers, second (after '=') is the values/data.  
                #remove any unnecessary headers/values that the upload file doesn't need.  
                "Employee ID"     = $item.'Employee ID'  
                "First Name" = $item.'First Name'  
                "Last Name" = $item.'Last Name'  
                "Display Name" = $item.'Display Name'  
                "Telephone" = $item.Telephone  
                "Email" = $item.Email  
                "Street" = $item.Street  
                "City" = $item.City  
                "State" = $item.State  
                "Zip Code" = $item.'Zip Code'  
                "Home" = $item.Home  
                "Mobile" = $item.Mobile  
                "Fax" = $item.Fax  
                "Job Title" = $item.'Job Title'  
                "Department" = $item.Department  
                "Company" = $item.Company  
                "Status" = "Disabled" #this is static and should remain. Or set to whatever value you need for our application to disable the user.  
            }  
        }  
    }  
    $output | Export-CSV -Path "C:\temp\TestCSV\TEST_DISABLED_APP_USERS.CSV" -NoTypeInformation  
    

    Hope this helps with your query,

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

    0 comments No comments