question

DeniBeslic-6655 avatar image
0 Votes"
DeniBeslic-6655 asked AndreasBaumgarten answered

Populate Azure groups with users from CSV.

Hi,
I have a CSV file which contains Azure AD group names and users UPNs.
The user and corresponding group are in the same row, for example:
Group1 UserUPN1
Group1 UserUPN2
Group2 UserUPN1
Group3 UserUPN2
...

I need to create a Powershell script that will populate corresponding group with a user that is in the same row in CSV file.
Anyone has ideas on how to accomplish that?

windows-server-powershellazure-ad-group-management
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

You can do this with 3 scripts

  • Script1: Create the groups (using the first column of the csv)

  • Script2 (if user not exists already): Create the users (using the second column of the csv)

  • Script3: Add user to group( using the both column of the csv)

Or with one script with some more logic in the script:

  • Use both columns of csv and walk trough

  • Create group if not exists

  • Create user if not exists

  • Add user to group

The following cmdlets will do the job:
https://docs.microsoft.com/en-us/powershell/module/azuread/new-azureaduser?view=azureadps-2.0
https://docs.microsoft.com/en-us/powershell/module/azuread/new-azureadgroup?view=azureadps-2.0
https://docs.microsoft.com/en-us/powershell/module/azuread/get-azureaduser?view=azureadps-2.0
https://docs.microsoft.com/en-us/powershell/module/azuread/get-azureadgroup?view=azureadps-2.0
https://docs.microsoft.com/en-us/powershell/module/azuread/add-azureadgroupmember?view=azureadps-2.0


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DeniBeslic-6655 avatar image
0 Votes"
DeniBeslic-6655 answered

Hi, I already have the users and groups. I need to create script that would associate them and I have problems with that part.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

@DeniBeslic-6655 ,

Here we go:

 $file = "<yourCSVfile>"
 $entries = Import-Csv -path $file -delimiter ";"
 foreach ($entry in $entries)
     {
         $grp = Get-AzureADGroup -Searchstring $entry.group
         $usr = Get-AzureADuser -Searchstring $entry.upn
         Add-AzureADGroupMember -ObjectId $grp.ObjectID -RefObjectId $usr.ObjectID
     }

The CSV file looks like this:

 group;upn
 test1;luigi@<yourdomain>.<something>
 test2;luigi@<yourdomain>.<something>
 test3;mario@<yourdomain>.<something>
 test2;paul@<yourdomain>.<something>


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.