Skip to content

Commit

Permalink
Consolidate Entra ID PowerShell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
SamErde committed Nov 30, 2023
1 parent d0714aa commit 4f43a41
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Entra ID/Remove Azure AD Duplicate Devices - Windows.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Connect-MgGraph
# Find duplicate devices by pulling all and grouping by DisplayName, then only keep a list where there are more than one entries with the same display name.
$DuplicateDevices = Get-MgDevice -All | Group-Object DisplayName | Where-Object {$_.Count -gt 1 -and $_.OperatingSystem -eq "Windows"} | Sort-Object Count

foreach ($group in $DuplicateDevices) {
# For each group of duplicate DisplayName,
# Expand the group object and sort it by the approximate last sign-in timestamp,
# Then exclude the most recently active one so it is kept when the rest are removed.
$OldDuplicates = $group | Select-Object -ExpandProperty Group | Sort-Object ApproximateLastSigninDateTime | Select-Object -SkipLast 1
# Having excluded the most recent object, remove each "old" duplicate device in the group.
$OldDuplicates | ForEach-Object { Remove-MgDevice -DeviceId $_.Id }
}

0 comments on commit 4f43a41

Please sign in to comment.