0

This compare call returns all rows that have the same Name equal in both Tables - But the other fields, like the DateField are returning no data and they have date data.

How can I correctly use the compare to return all the data from these rows that have the same Name values?

birth_date FirstName object_id

blank John blank blank David blank

$otherField1 = "birth_date" 
$otherField2 = "object_id"
$Name = "FirstName"
$exportObject=@() 
$exportObject = Compare-Object -ReferenceObject $1_resultsDataTable -DifferenceObject $1_resultsDataTable -IncludeEqual -Property $Name| Where-Object{($_.SideIndicator -eq '==') } | Select-object $otherField1, $Name, $otherField2
4
  • 1
    It looks like you're using variables instead of the property names. What is $Name you are using it as an argument to -IncludeProperty. Doing this will expand the variable's value in that spot when it looks like you want the name of the property. It looks like you're doing the same thing with the Select-Object command. Unless that's intentional and those are defined elsewhere. Commented Mar 1, 2021 at 23:39
  • the Name variable is populating the correct column FirstName with names below - the other field DateField column header is populating its Date header but no data. The same for the other fields - their column headers appear but no data. I'm not doing the Compare correctly - right? Just using variables to be flexible for field names. Commented Mar 1, 2021 at 23:49
  • Can you give example objects, and show where the aforementioned variables are defined. Commented Mar 1, 2021 at 23:58
  • if you have two tables where there are names that are duplicated in both tables - you want those complete rows that have 4 columns that contain different data not duplicated in the tables. How would you use the compare to all the rows including data from all 4 columns? Commented Mar 2, 2021 at 0:14

1 Answer 1

1

Given you are comparing complex objects. I don't think Compare-Object is the right tool. When using the -Property parameter Compare-Object will strip the original input objects from it's output objects. -PassThru will amend the input objects with the SideIndicator property, but it will not include data from both the objects for whom the given property was equivalent. In cases where other property values differ it will not be represented in the output.

$ObjectArray1 =
@(
    [PSCustomObject]@{
        Birth_Date = [DateTime]'4/22/65'
        FirstName  = "Otto"
        Object_id  = 1234
    }    
    [PSCustomObject]@{
        Birth_Date = [DateTime]'5/30/67'
        FirstName  = "Jean"
        Object_id  = 9999
    }
)

$ObjectArray2 =
@(
    [PSCustomObject]@{
        Birth_Date = [DateTime]'4/1/80'
        FirstName  = "Bob"
        Object_id  = 1234
    }    
    [PSCustomObject]@{
        Birth_Date = [DateTime]'5/30/67'
        FirstName  = "Jean"
        Object_id  = 9998
        Prop4      = "Some other val"
    }
)

Compare-Object $ObjectArray1 $ObjectArray2 -IncludeEqual -ExcludeDifferent -Property FirstName -PassThru

This returns

Birth_Date            FirstName Object_id SideIndicator
----------            --------- --------- -------------
5/30/1967 12:00:00 AM Jean           9999 ==

Notice there's only 1 object. There's no indication of Prop4, nor that the second object had a different Object_id. Only the reference object is output.

Something like below would reveal such differences.

$Output = [Collections.ArrayList]@()

$ObjectArray1 |
ForEach-Object{ 
    If( $_.FirstName -in $ObjectArray2.FirstName ) {
        [Void]$Output.Add($_)
    }
    
}

$ObjectArray2 | 
ForEach-Object{ 
    If( $_.FirstName -in $ObjectArray1.FirstName ) {
        [Void]$Output.Add($_)
    }    
}

$Output

Output:

Birth_Date            FirstName Object_id
----------            --------- ---------
5/30/1967 12:00:00 AM Jean           9999
5/30/1967 12:00:00 AM Jean           9998

I suppose it depends on the nature of the objects and tables. However, it strikes me that operators like -contains & -on it just strikes me that are better options.

It would be even easier if when the first name is the same all other properties are the same. Essentially you would only need to output from one of the collections:

$ObjectArray1 =
@(
    #...
    [PSCustomObject]@{
        Birth_Date = [DateTime]'5/30/67'
        FirstName  = "Jean"
        Object_id  = 9999
    }
)

$ObjectArray2 =
@(
    # ...   
    [PSCustomObject]@{
        Birth_Date = [DateTime]'5/30/67'
        FirstName  = "Jean"
        Object_id  = 9999
    }
)

$ObjectArray1 |
Where-Object{ $_.FirstName -in $ObjectArray2.FirstName }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.