'PowerShell Look Up UserPrincipalName using DisplayNames

I'm trying to write a script that will import a CSV file, and return the UserPrincipalName from AzureAD.

For some reason, it's just not reading the names in the files and returns an empty list. But if I manually type in the names, it works. The CSV file only has one column, and is formatted as:

User
FirstName1 LastName1
FirstName2 LastName2
FirstName3 LastName3

Script:

$UserCSV = Import-Csv -Path "C:Path\to\file.csv"

foreach($user in $UserCSV)
    {
        Get-AzureADUser -SearchString $user | Select DisplayName, UserPrincipalName, Email
     
     }

The thing is, if I write $UserCSV = "FirstName1 LastName1", "FirstName2 LastName2" it works without issues.

Any guidance will be greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source