A cloud-based identity and access management service for securing user authentication and resource access
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