0

I am using PnPPowershell to import some CSV files into SharePoint lists and am having trouble with the person columns. I want to compare the emails in several CSV columns to a list of known site users and then use some other techniques with the "unknowns." But first I have to identify the unknowns.

I can use Get-PnPUser to get all the site (collection) confirmed users and it is an array of object something like this. Currently there are about 720 users in our site.

  [PSCustomObject]@{Email=first.mi.last@example.org,
                    Id=9,
                    Title=Rothrock}

And then I have the array of object being read from the CSV file. There are about 11,000 of these records and they have many more properties than I've shown here.

  [PSCustomObject]@{OwnerEmail=SomePersonOrOther@example.com,
                    ContactEmail=SomebodyElse@example.com,
                    Title=Something or other,
                    ReviewDate=7/25/2022,
                    ...}

I need to compare all of the "email" properties/columns from the CSV to the known users and find the "unknown" people.

I found this page about performing set operations. It uses Compare-Object and I read the MS documentation, which has a -Property switch for which object property should be compared. For example if I want to compare the email properties of the objects.

$result = Compare-Object -DifferenceObject $users $inputCSV -Property email

This is working well for me when my object both have a property with the same names. But I want to compare the email property from users with several differently named properties in the CSV. For example compare the User.Email to InputCSV.OwnerEmail and also to InputCSV.ContactEmail and so on.

Is there a way to quickly compare objects with different property names?

2
  • So do you need to compare both OwnerEmail and ContactEmail at the same time or just find out if any of the two values exist in the user array? Jun 22 at 9:39
  • I eventually need to find all the emails that are unknown to the site regardless of which csv column they're in. Doesn't have to be at the same time, but I need to find them all. And the goal for this is to separate out the csv rows that would fail when trying to Add-PnPListItem
    – Rothrock
    Jun 22 at 14:45

1 Answer 1

0

Without having seen your code this might be what you are looking for.

#Array with users to check against
$users = @("userOne@domain.com","userTwo@domain.com")
#$users = Get-PnPUser

#Array with the rows from the CSV that were found in the 'users' array
$found = [System.Collections.ArrayList]@()
#Array with the rows from the CSV that were NOT found in the 'users' array
$unknown = [System.Collections.ArrayList]@()

#Import the CSV normally
$csv = Import-Csv -Path "csvPath\fileName.csv" -Delimiter ";" -Encoding UTF8

#Loop through each row in the CSV
ForEach($row in $csv) {
    #check if the array 'users' contain either the current CSV row property 'OwnerEmail or 'ContactEmail'
    If(($users -contains $row.OwnerEmail) -or ($users -contains $row.ContactEmail)) {
        $found.Add($row) | Out-Null
    }
    Else {
        $unknown.Add($row) | Out-Null
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.