Created
February 19, 2026 16:39
-
-
Save win2000b/0a03c9d1cacc794ae22f157b039662cf to your computer and use it in GitHub Desktop.
Add Users to Entra ID Group using Graph API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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