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

%3CLINGO-SUB%20id%3D%22lingo-sub-166837%22%20slang%3D%22en-US%22%3EHow%20to%20retrieve%20the%20object%20id%20of%20a%20list%20of%20users%20from%20a%20csv%20file%20with%20UserPrincipleName%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-166837%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20trying%20to%20bulk%20load%20a%20csv%20file%20of%20users%20into%20a%20security%20group.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20line%20below%20would%20do%20what%20I%20want%20to%20do%20except%20that%20is%20asking%20for%20all%20users%20from%20a%20specific%20domain.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%24users%20%3D%20get-msoluser%20%7C%20select%20userprincipalname%2Cobjectid%20%7C%20where%20%7B%24_.userprincipalname%20-like%20%E2%80%9C*acme.com*%E2%80%9D%7D%3C%2FPRE%3E%0A%3CP%3EOnce%20the%20users%20loaded%20into%20%24users%2C%20I%20would%20use%20the%20below%20command%20line%20to%20add%20them%20to%20the%20relevant%20security%20group%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%24users%20%7C%20foreach%20%7Badd-msolgroupmember%20-groupobjectid%20%24group.objectid%20-groupmembertype%20%E2%80%9Cuser%E2%80%9D%20-GroupMemberObjectId%20%24_.objectid%7D%3C%2FPRE%3E%0A%3CP%3EHowever%2C%20what%20should%20I%20do%20if%20I%20have%20a%20csv%20file%20with%26nbsp%3BUserPrincipleName%20and%20I%20wantto%20get%20the%20object%20id%20of%20the%20users%20from%20that%20very%20list%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBest%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-166837%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-166998%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20retrieve%20the%20object%20id%20of%20a%20list%20of%20users%20from%20a%20csv%20file%20with%20UserPrincipleName%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-166998%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20try%20something%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%24users%20%7C%20foreach%20%7Badd-msolgroupmember%20-groupobjectid%20%24group.objectid%20-groupmembertype%20%E2%80%9Cuser%E2%80%9D%20-GroupMemberObjectId%20(Get-MsolUser%20-UserPrincipalName%20%24_.UserPrincipalName).objectid%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional 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
Highlighted

You can try something like this:

 

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