I imported a CSV into a variable and it contains an OwnerId column with valid Azure AD user IDs. I want to look up each user with Microsoft Graph PowerShell, but my loop keeps failing with errors such as "Id cannot be a string" or invalid filter/date parsing. I have tried both `Get-MgUser -UserId $ownerId` and filters like `Get-MgUser -Filter "id eq '$wbs.ownerid'"`. What is the correct way to loop through the CSV rows and pass the OwnerId value to `Get-MgUser`?
2 Answers
If you use a filter, PowerShell must expand the object property inside the quoted string with a subexpression. `$wbs.ownerid` is also a collection when `$wbs` contains multiple rows. Use this inside the loop:
```powershell
foreach ($row in $wbs) {
$id = [string]$row.OwnerId
Get-MgUser -Filter "id eq '$id'"
}
```
Or directly:
```powershell
Get-MgUser -Filter "id eq '$($row.OwnerId)'"
```
The unexpanded expression can produce an invalid filter, and if the wrong column or whole row is being passed, Graph may interpret the resulting text incorrectly.
`Import-Csv` returns objects, so looping over `$wbs` gives you an entire row—not the value in its OwnerId column. Either loop through the property directly or access the property inside the loop:
```powershell
$wbs = Import-Csv 'pathfile.csv'
foreach ($row in $wbs) {
$ownerId = $row.OwnerId.Trim()
Get-MgUser -UserId $ownerId
}
```
You can also write it as `foreach ($ownerId in $wbs.OwnerId)`, assuming the column is really named `OwnerId`. `-UserId` expects one string containing a user GUID, so make sure the value is not blank and does not contain extra whitespace.

The CSV contains several columns, including OwnerId, so looping over each row and then using `$row.OwnerId` makes sense. I’ll also output the value and its type to check for whitespace or unexpected data.