How to create bulk user with auto added in group using entra ID

Sanket Shelar 20 Reputation points
2026-07-13T15:46:38.39+00:00

need to create multiple user in entra id with auto added in group , i tried using bulk creation but later added in group work is manual.

Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments

Answer accepted by question author
Marcin Policht 107.9K Reputation points MVP Volunteer Moderator
2026-07-13T18:19:35.0266667+00:00

Use Dynamic Groups (if your users have a shared attribute such as Department, Job Title, or City and you have Premium licensing of Entra ID) or Microsoft Graph PowerShell instead

# --- CONFIGURATION ---
$csvPath = "C:\temp\users.csv"
$groupObjectId = "YOUR-GROUP-OBJECT-ID-HERE"  # Replace with your Entra ID Group Object ID
# ---------------------

# 1. Install Microsoft Graph Identity Module if not already installed
if (-not (Get-Module -ListAvailable -Name Microsoft.Graph.Identity.DirectoryManagement)) {
    Write-Host "Installing Microsoft Graph module..." -ForegroundColor Cyan
    Install-Module Microsoft.Graph -Scope CurrentUser -Force
}

# 2. Connect to Microsoft Entra ID
Write-Host "Connecting to Microsoft Entra ID..." -ForegroundColor Cyan
Connect-MgGraph -Scopes "User.ReadWrite.All", "Group.ReadWrite.All"

# 3. Import the CSV file
if (-not (Test-Path $csvPath)) {
    Write-Error "CSV file not found at $csvPath. Please check the path."
    return
}
$users = Import-Csv -Path $csvPath

# 4. Loop through each user in the CSV
foreach ($user in $users) {
    Write-Host "--------------------------------------------" -ForegroundColor Tool
    Write-Host "Processing user: $($user.DisplayName)" -ForegroundColor Cyan

    # Define user parameters
    $passwordProfile = @{
        Password = $user.Password
        ForceChangePasswordNextSignIn = $true
    }

    $userParams = @{
        AccountEnabled    = $true
        DisplayName       = $user.DisplayName
        UserPrincipalName = $user.UserPrincipalName
        MailNickname      = $user.MailNickname
        GivenName         = $user.GivenName
        Surname           = $user.Surname
        Department        = $user.Department
        PasswordProfile   = $passwordProfile
    }

    try {
        # Create the user in Entra ID
        $newUser = New-MgUser @userParams
        Write-Host "Successfully created user: $($newUser.UserPrincipalName) (ID: $($newUser.Id))" -ForegroundColor Green

        # Immediately add the user to the specified group
        Write-Host "Adding user to group..." -ForegroundColor Cyan
        New-MgGroupMemberByRef -GroupId $groupObjectId -OdataId "https://microsoft.com"
        Write-Host "Successfully added to group!" -ForegroundColor Green

    } catch {
        Write-Error "Failed to process user $($user.UserPrincipalName). Error: $_"
    }
}

# 5. Disconnect session
Disconnect-MgGraph
Write-Host "Process complete and disconnected." -ForegroundColor Green


If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

hth

Marcin

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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