← All scripts

Microsoft 365 & Entra ID

Add All Users to a Security Group

Adds every member (non-guest) user in the tenant to an Entra ID security group you choose at runtime. Note: uses the legacy AzureAD module (being retired); Microsoft Graph is the successor.

Download .ps131 lines · PowerShell

Replace generic placeholder values (tenant, domain, secrets) for your own environment before running. Read it first and test safely.

# Connect to Microsoft Entra ID (Azure AD)
Connect-AzureAD

# Prompt for the Security Group name or ID
$groupName = Read-Host "Enter the name or object ID of the Security Group to which you want to add users"

# Get the security group object
$group = Get-AzureADGroup -SearchString $groupName

if ($null -eq $group) {
    Write-Host -ForegroundColor Red "Security Group not found."
    exit
}

# Get all user accounts (excluding guest accounts)
$Users = Get-AzureADUser -All $true | Where-Object {
    $_.UserType -eq 'Member' -and $_.UserPrincipalName -notlike '*#EXT#*'
}

# Check if there are valid users to add
if ($Users.Count -gt 0) {
    # Loop through each user and add them to the security group
    foreach ($user in $Users) {
        Add-AzureADGroupMember -ObjectId $group.ObjectId -RefObjectId $user.ObjectId
        Write-Host -ForegroundColor Green "$($user.UserPrincipalName) added to the group."
    }

    Write-Host -ForegroundColor Green "All valid users have been added to the security group!"
} else {
    Write-Host -ForegroundColor Yellow "No valid users were found to add to the security group."
}