-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate Entra ID PowerShell scripts
- Loading branch information
Showing
1 changed file
with
12 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 } | ||
} |