Populate Azure groups with users from CSV.

Deni Beslic 21 Reputation points
2021-02-20T16:38:51.187+00:00

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 PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,387 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,640 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Andreas Baumgarten 97,486 Reputation points MVP
    2021-02-20T17:17:02.777+00:00

    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://learn.microsoft.com/en-us/powershell/module/azuread/new-azureaduser?view=azureadps-2.0
    https://learn.microsoft.com/en-us/powershell/module/azuread/new-azureadgroup?view=azureadps-2.0
    https://learn.microsoft.com/en-us/powershell/module/azuread/get-azureaduser?view=azureadps-2.0
    https://learn.microsoft.com/en-us/powershell/module/azuread/get-azureadgroup?view=azureadps-2.0
    https://learn.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

    0 comments No comments

  2. Deni Beslic 21 Reputation points
    2021-02-20T17:25:35.68+00:00

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

    0 comments No comments

  3. Andreas Baumgarten 97,486 Reputation points MVP
    2021-02-20T18:22:51.657+00:00

    @Deni Beslic ,

    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

    0 comments No comments