I am trying to get the department from a list of UserPrincipalNames
I am able to get this to work for a single user outside of the foreach loop. Its adding the loop where I am having trouble.
Connect-MsolService
$users = Import-Csv C:\Users\me\Desktop\users.csv
foreach ($user in $users){
Get-MsolUser -UserPrincipalName $user | Select-Object firstname, lastname, UserPrincipalName, department |Export-Csv C:\Users\me\Desktop\test.csv
}
There are 50 email addresses listed in the CSV one email address per line. With the first line being "UserPrincipalName"
CSV Sample Data
userprincipalname
[email protected]
[email protected]
[email protected]
[email protected]
Export-Csvnothing to export - vrdseexport-csvwas in the wrong place, but even without it I get the errorGet-MsolUser : User Not Found. User: @{[email protected]}- rulecombination$useras your UPN ... but that came from importing a CSV. that means it will be an OBJECT with at least one property that has at least one value. what happens if you use$User.PropertyNameThatContainsTheUPN? - Lee_Dailey