Home > Enterprise >  Powershell Comparing two object properties
Powershell Comparing two object properties

Time:11-02

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
        }
    }
}

CodePudding user response:

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.

  • Related