Skip to content

Instantly share code, notes, and snippets.

@win2000b
Created February 19, 2026 16:39
Show Gist options
  • Select an option

  • Save win2000b/0a03c9d1cacc794ae22f157b039662cf to your computer and use it in GitHub Desktop.

Select an option

Save win2000b/0a03c9d1cacc794ae22f157b039662cf to your computer and use it in GitHub Desktop.
Add Users to Entra ID Group using Graph API
# Requires: Microsoft.Graph PowerShell module
# Install (once): Install-Module Microsoft.Graph -Scope CurrentUser
$CsvPath = "C:\Temp\users.csv"
$GroupId = "00000000-0000-0000-0000-000000000000" # <-- Security Group ObjectId
# Connect to Microsoft Graph
# You may be prompted to sign in and consent
Connect-MgGraph -Scopes "Group.ReadWrite.All","User.Read.All"
# Import the CSV
$users = Import-Csv -Path $CsvPath
foreach ($u in $users) {
$upn = $u.UPN.Trim()
if ([string]::IsNullOrWhiteSpace($upn)) {
Write-Warning "Skipping blank UPN row."
continue
}
try {
# Find the user by UPN
$user = Get-MgUser -UserId $upn -ErrorAction Stop
# Add user to the group
New-MgGroupMemberByRef -GroupId $GroupId -BodyParameter @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/$($user.Id)"
} -ErrorAction Stop
Write-Host "Added: $upn" -ForegroundColor Green
}
catch {
# Common cases: user not found, already a member, permissions, etc.
Write-Warning "Failed for $upn : $($_.Exception.Message)"
}
}
Disconnect-MgGraph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment