Skip to content

Instantly share code, notes, and snippets.

@harrdou
Last active January 23, 2026 16:56
Show Gist options
  • Select an option

  • Save harrdou/7c97640caad482fba6952e4274102681 to your computer and use it in GitHub Desktop.

Select an option

Save harrdou/7c97640caad482fba6952e4274102681 to your computer and use it in GitHub Desktop.
Pupulate a security group with migratged users
# A script to populate an Entra ID security group with users who have
# migrated at least one device to EMDM-Cloud
# Place the group name here
$groupName = "EMDM Cloud Users"
# Connect to MS Graph
Connect-MgGraph -Scopes "Device.Read.All, Group.Read.All, GroupMember.ReadWrite.All" -NoWelcome
# Retreive current group members
$group = Get-MgGroup -Filter "DisplayName eq '$groupName'"
$members = [System.Collections.Generic.HashSet[string]]@(Get-MgGroupMember -GroupId $group.Id -All | Select-Object -ExpandProperty Id)
# Locate migrated devices and their owners
$migratedDevices = Get-MgDevice -Filter "MdmAppId eq '0000000a-0000-0000-c000-000000000000' AND OperatingSystem in ('iOS', 'Android')" -All -Expand RegisteredOwners |
Where-Object {$_.DeviceOwnerhip -eq $null}
# Add migrated users who are not already in the group
foreach ($device in $migratedDevices) {
$ownerId = $device.RegisteredOwners.Id
if (-not $members.Contains($ownerId)) {
New-MgGroupMember -GroupId $group.Id -DirectoryObjectId $ownerId
[void]$members.Add($ownerId)
}
}
Disconnect-MgGraph | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment