I have the following PnP PowerShell script:

$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=$DBServer;Integrated Security=True;Initial Catalog=$DBName")                        
$query = $Query                        
$dap = new-object System.Data.SqlClient.SqlDataAdapter($query,$conn);                        
$dt = new-object System.Data.DataTable;                        
$null = $dap.Fill($dt)  
      foreach($r in $dt.Rows )   {                         
$SPUserEmail = $r["email_address"];
$SPuser = (Get-PnPUser | ? Email -eq ($SPUserEmail));}

but I always get this error:

The specified user  could not be found.

but if I type the email address manually I get the user details:

$SPuser = (Get-PnPUser | ? Email -eq [email protected]);

although when I do Write-Host $SPUserEmail I will get [email protected]

So what could be the reason?

  • Is $SPUserEmail holding string/text value? Can you try running script after removing ( ) around $SPUserEmail? Let me know if this works for you. Jan 5 at 6:10
  • @GaneshSanap removing the () did not change any thing
    – John John
    Jan 5 at 10:39

1 Answer 1


It's because $SPUserEmail holds a object, and the email is a property. Get-PnpUser expects a string value.

PropertyName is the property name found in the object.

To get only the value out of the object, either select it when setting the variable:

$SPUserEmail = $r["email_address"] | Select-Object PropertyName -ExpandProperty PropertyName 

Or by expanding the property value you need:

(Get-PnPUser | Where-Object Email -eq ($SPUserEmail.PropertyName))
  • so what i need to define for the PropertyName ?
    – John John
    Jan 5 at 10:39
  • also when i tried $r["email_address"].TypeOf() i will get the following IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object so seems it is a string
    – John John
    Jan 5 at 10:46
  • By printing $r["email_address"] it should print the propertyname + the value. Jan 5 at 11:11
  • i think the problem is when calling the function i opened another question @ sharepoint.stackexchange.com/questions/305623/…
    – John John
    Jan 5 at 11:45

Your Answer

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

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