Miles
Miles

Reputation: 89

Powershell Comparing two object properties

Loaded two JSON files with Get-Content filename.JSON | ConvertFrom-Json

One object name $SubscriberEmail has the property name email

The second object name $ADEnabledUsersEmail has a property named Work Email

Trying to compare both objects and find the ones, not in the $ADEnabledUsersEmail object and Write-Out the name.

Here is the current foreach method but seems to output all the emails multiple times.

foreach($S in $SubscriberEmail ){
    foreach($A in $ADEnabledUsersEmail){
        if($A.'Work Email' -eq $S.email ){
            Write-Host "User is active"
        }else{
            Write-Host $s.email
        }
    }
}

Upvotes: 2

Views: 297

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 59820

If you want to filter an array you can use Where-Object or .Where() method or a loop with the right comparison operator. i.e.: -notin, -notcontains:

$SubscriberEmail.email.where({ $_ -notin $ADEnabledUsersEmail.'Work Email' })
$SubscriberEmail | Where-Object {
    $ADEnabledUsersEmail.'Work Email' -notcontains $_.email
}
foreach($email in $SubscriberEmail.email)
{
    if($email -notin $ADEnabledUsersEmail.'Work Email')
    {
        $email
    }
}

There are many more options to filter arrays, just search filtering arrays powershell on Google.

Upvotes: 2

Related Questions