How to Bulk Import CSV to create Security Groups and Add Groups Owner to those Groups Using Powershell in Azure AD

Mourya, Aditya 21 Reputation points
2022-09-17T09:05:42.503+00:00

I am Looking for an Script where i can create 500 Security Groups by Bulk Import Power shell cmdlet and Add Group Owners in Azure AD at the same time .

Please help !!!

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
24,286 questions
{count} vote

7 answers

Sort by: Most helpful
  1. Mourya, Aditya 21 Reputation points
    2022-09-18T17:06:25.28+00:00

    It seems It is working now and thank so much for your help and for your prompt replies ..

    1 person found this answer helpful.

  2. Andreas Baumgarten 120.4K Reputation points MVP
    2022-09-17T10:26:52.317+00:00

    Hi @Mourya, Aditya ,

    maybe this helps to get started: https://morgantechspace.com/2021/12/create-security-group-and-add-members-in-azure-ad-using-powershell.html

    Do you have a CSV file with the names of the new AD groups and the related AD user (owner)?

    ----------

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

    Regards
    Andreas Baumgarten


  3. Vinodh247 32,366 Reputation points MVP
    2022-09-17T10:43:12.513+00:00

    Hi Mourya,

    Thanks for reaching out to Microsoft Q&A.

    Please refer the below links, these has directions and steps.

    https://stackoverflow.com/questions/31528764/powershell-import-csv-to-add-active-directory-security-groups

    https://social.technet.microsoft.com/wiki/contents/articles/19177.powershell-script-to-add-users-to-group-from-csv-file-based-on-decision-control.aspx

    EDIT

    Please ignore the links above as it is related to Azure AD on-prem, I suggest you to refer the link posted by @Andreas Baumgarten

    Please Upvote and Accept as answer if the reply was helpful.


  4. Andreas Baumgarten 120.4K Reputation points MVP
    2022-09-18T13:20:42.333+00:00

    There is a minor mistake in the script. Sorry!

    Please try this:

    Import-Csv -Path "c:\temp\GroupsC.csv" | ForEach-Object {  
    $ownerUserObj = Get-AzureADUser -ObjectId $_.GroupOwner  
    $newGroup = New-AzureADGroup -DisplayName $_.GroupName -SecurityEnabled $true -MailEnabled $false -MailNickName "NotSet"  
    Add-AzureADGroupOwner -ObjectId $newGroup.ObjectId -RefObjectId $ownerUserObj.ObjectId  
    }  
    

    ----------

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

    Regards
    Andreas Baumgarten


  5. Mourya, Aditya 21 Reputation points
    2022-09-18T13:45:51.763+00:00

    Still No Luck ,Got this Error ...

    Get-AzureADUser : Cannot bind argument to parameter 'ObjectId' because it is an empty string.
    At line:2 char:44

    • $ownerUserObj = Get-AzureADUser -ObjectId $_.GroupOwner
    • ~~~~~~~~~~~~~
    • CategoryInfo : InvalidData: (:) [Get-AzureADUser], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.Open.AzureAD16.PowerShell.GetUser

    New-AzureADGroup : Cannot bind argument to parameter 'DisplayName' because it is an empty string.
    At line:3 char:44

    • $newGroup = New-AzureADGroup -DisplayName $_.GroupName -SecurityEnab ...
    • ~~~~~~~~~~~~
    • CategoryInfo : InvalidData: (:) [New-AzureADGroup], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.Open.AzureAD16.PowerShell.NewGroup

    Add-AzureADGroupOwner : Error occurred while executing AddGroupOwner
    Code: Request_BadRequest
    Message: One or more added object references already exist for the following modified properties: 'owners'.
    RequestId: e2945e74-a689-4209-b8ea-2110bb370b37
    DateTimeStamp: Sun, 18 Sep 2022 13:44:28 GMT
    HttpStatusCode: BadRequest
    HttpStatusDescription: Bad Request
    HttpResponseStatus: Completed
    At line:4 char:2

    • Add-AzureADGroupOwner -ObjectId $newGroup.ObjectId -RefObjectId $own ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [Add-AzureADGroupOwner], ApiException
    • FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.AddGroupOwner

    Get-AzureADUser : Cannot bind argument to parameter 'ObjectId' because it is an empty string.
    At line:2 char:44

    • $ownerUserObj = Get-AzureADUser -ObjectId $_.GroupOwner
    • ~~~~~~~~~~~~~
    • CategoryInfo : InvalidData: (:) [Get-AzureADUser], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.Open.AzureAD16.PowerShell.GetUser

    New-AzureADGroup : Cannot bind argument to parameter 'DisplayName' because it is an empty string.
    At line:3 char:44

    • $newGroup = New-AzureADGroup -DisplayName $_.GroupName -SecurityEnab ...
    • ~~~~~~~~~~~~
    • CategoryInfo : InvalidData: (:) [New-AzureADGroup], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.Open.AzureAD16.PowerShell.NewGroup

    Add-AzureADGroupOwner : Error occurred while executing AddGroupOwner
    Code: Request_BadRequest
    Message: One or more added object references already exist for the following modified properties: 'owners'.
    RequestId: 82b8d083-4ebd-44b5-b964-470111693e20
    DateTimeStamp: Sun, 18 Sep 2022 13:44:28 GMT
    HttpStatusCode: BadRequest
    HttpStatusDescription: Bad Request
    HttpResponseStatus: Completed
    At line:4 char:2

    • Add-AzureADGroupOwner -ObjectId $newGroup.ObjectId -RefObjectId $own ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [Add-AzureADGroupOwner], ApiException
    • FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.AddGroupOwner

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.