How to Create Azure AD Groups in Bulk using PowerShell

How to Create Azure AD Groups in Bulk using PowerShell – TrulyBlogs
Create Azure AD Groups in Bulk with PowerShell | TrulyBlogs

Are you an IT admin or cloud engineer tired of manually creating security groups in Azure Active Directory?
Well, you’re not alone. Whether you’re managing user access, setting up role-based access control, or organizing groups for Microsoft 365 services—doing it one by one is time-consuming and prone to human error.

In this blog, I’ll walk you through a practical PowerShell script that helps you create multiple Azure AD groups in bulk. I’ll explain every line of the script in plain, human-friendly language. This guide is crafted to be easy to follow—even if you’re not a scripting expert.

Let’s dive in!

Prerequisites

Before you run the script, make sure you meet the following requirements:
– You have the necessary admin rights to create groups and assign owners in Azure AD.
– You have PowerShell installed with the AzureAD module.
– You are connected to both Exchange Online and Azure AD using PowerShell.
– A CSV file with the group names is prepared in advance (more on that below).

Step-by-Step PowerShell Script Explanation

Here’s the full script we’ll be using:

PowerShell
Connect-ExchangeOnline

Install-Module -Name AzureAD

Connect-AzureAD

$Names = Import-Csv "C:\Users\Desktop\ProvidedNames.csv"

foreach ($name in $Names) {
    $group = New-AzureADGroup -DisplayName $name.dpname -Description "enter your description" -MailEnabled $false -SecurityEnabled $true -MailNickname $mailbox.dpname
    $owner = "OwnerEmailAddress"
    Add-AzureADGroupOwner -ObjectId $group.ObjectId -RefObjectId (Get-AzureADUser -ObjectId $owner).ObjectId
}

Now let’s break it down:

PowerShell
Connect-ExchangeOnline

This command is used to establish a remote session with Exchange Online. It’s essential if you plan to work with mail-enabled groups later.

PowerShell
Install-Module -Name AzureAD

This installs the AzureAD PowerShell module which allows you to manage Azure Active Directory resources.

PowerShell
Connect-AzureAD

This command authenticates your session to Azure AD. A login prompt will appear where you’ll enter your credentials.

PowerShell
$Names = Import-Csv

This line imports group names from a CSV file located on your desktop. Each row in the CSV should contain a column named ‘dpname’.

PowerShell
foreach loop

This loop goes through each entry in the CSV file, creating a new Azure AD group for each name found.

PowerShell
New-AzureADGroup

This command creates the actual group. You can customize the description and change the MailEnabled or SecurityEnabled flags as per your needs.

PowerShell
Add-AzureADGroupOwner

This command assigns an owner to the newly created group. The owner email should be an existing user in Azure AD.

Sample Output

Here’s what you can expect after running the script successfully:

Creating Group: IT_Admins
Owner Assigned: admin@yourdomain.com

Creating Group: HR_Team
Owner Assigned: hrmanager@yourdomain.com

… and so on for every entry in the CSV file.

No errors? Congratulations! Your groups have been created successfully.

Common Issues and Troubleshooting Tips


• Error: ‘New-AzureADGroup is not recognized’ → Make sure AzureAD module is installed and imported properly.
• Error: ‘Object reference not set’ → Check that your CSV file is correctly formatted and that the ‘dpname’ column exists.
• Authentication failures? → Make sure MFA is not blocking the session or use a service principal for automation.

Conclusion

Creating Azure AD groups in bulk using PowerShell can save you a ton of time—especially in large organizations. With the help of a well-structured script and a clear CSV file, you can automate this task and focus on more strategic work.

Hope you found this guide helpful. If you did, feel free to share it and drop your comments below. Happy scripting!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *