How to retrieve the object id of a list of users from a csv file with UserPrincipleName?

Brass Contributor

I'm trying to bulk load a csv file of users into a security group.

 

The line below would do what I want to do except that is asking for all users from a specific domain.

 

$users = get-msoluser | select userprincipalname,objectid | where {$_.userprincipalname -like “*acme.com*”}

Once the users loaded into $users, I would use the below command line to add them to the relevant security group

 

 

$users | foreach {add-msolgroupmember -groupobjectid $group.objectid -groupmembertype “user” -GroupMemberObjectId $_.objectid}

However, what should I do if I have a csv file with UserPrincipleName and I wantto get the object id of the users from that very list?

 

Best

1 Reply

You can try something like this:

 

$users | foreach {add-msolgroupmember -groupobjectid $group.objectid -groupmembertype “user” -GroupMemberObjectId (Get-MsolUser -UserPrincipalName $_.UserPrincipalName).objectid}