Get-PnPUser takes too long

Copper Contributor

I'm creating a process to get various bits of information about all of the sites and sub sites in our GCC High tenant and load it all into a SharePoint list for consumption by various business users.  Aside from the obvious issues I will eventually run into as the number of sites grows, my big issue right now is with verifying users in the target list's site and adding them if they aren't present.  I am running scripts remotely from my desktop (I've 


I have one script that actually gets all the data and saves into into an XML file.  The script that updates the SharePoint list loads that XML file and iterates through the rows, adding Add-PnPListItem or Set-SPListItem commands to batch.  I had discovered early in the process that if users were not present in my target site, the add-pnplistitem command would fail, so I added this code:




$verifiedOwners = @()
foreach($unVerifiedOwner in $row.SiteOwners)
   if($unVerifiedOwner -like "*@*") {
      $checkOwner = Get-PnPUser | ? Email -eq $unVerifiedOwner
      if($checkOwner -eq "" -or $checkOwner -eq $null ) {
         $newCheck = New-PnPUser -LoginName $unVerifiedOwner -InformationAction SilentlyContinue -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
         if($newCheck -ne "" -and $newCheck -ne $null) { $checkOwner = Get-PnPUser | ? Email -eq $unVerifiedOwner }
      if($checkOwner -ne "" -and $checkOwner -ne $null){$verifiedOwners += $checkOwner.Email}
   elseif($unVerifiedOwner -like "*\*" -or $unVerifiedOwner -like "*|*") {
            $verifiedOwners += $unVerifiedOwner



Never mind that elseif; it was there because sometimes SharePoint Service Administrator or Company Administrator were owners and they don't have email.  Adding the title works for a standalone Add/Set-PnPListItem, but not when you are doing batch, so I'll probably just eliminate it.


I added in some stop watches to see how long the Get-PnPUser and New-PnPUser cmdlets were taking and discovered that they generally were taking 30 seconds to a minute or more to complete.  Needless to say, if you have an average of even 3 site owners across 1100 sites/sub sites, it's causing almost useless run times.


Any thoughts on how I might improve performance here?  I headed down the batching for the set/add-pnplistitem to improve performance there because they were taking a long time individually to process, but the user thing is still a hang up.


Thank you.

0 Replies