Entra / Microsoft 365 · Devices
Report Entra registered devices
Generate a report about Entra registered devices using the Microsoft Graph PowerShell SDK.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -Scopes User.Read.All, Directory.Read.All -NoWelcome
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Connect-MgGraph -Scope User.Read.All, Directory.Read.AllWrite-Host "Finding registered devices"[array]$Devices = Get-MgDevice -All -PageSize 999If (!($Devices)) {Write-Host "No registered devices found - exiting" ; break}Write-Host ("Processing details for {0} devices" -f $Devices.count)$Report = [System.Collections.Generic.List[Object]]::new()$i = 0ForEach ($Device in $Devices) {$i++Write-Host ("Reporting device {0} ({1}/{2})" -f $Device.DisplayName, $i, $Devices.count)$DeviceOwner = $NullTry {[array]$OwnerIds = Get-MgDeviceRegisteredOwner -DeviceId $Device.Id$DeviceOwner = Get-MgUser -UserId $OwnerIds[0].Id `-Property Id, displayName, Department, OfficeLocation, City, Country, UserPrincipalName -ErrorAction Stop}Catch {Write-Host ("Error fetching owners for {0}" -f $Device.DisplayName)}$ReportLine = [PSCustomObject][Ordered]@{Device = $Device.DisplayNameId = $Device.Id"Device last signed in" = $Device.ApproximateLastSignInDateTime"Days since sign in" = (New-TimeSpan($Device.ApproximateLastSignInDateTime)).DaysOwner = $DeviceOwner.DisplayNameOwnerUPN = $DeviceOwner.UserPrincipalNameDepartment = $DeviceOwner.DepartmentOffice = $DeviceOwner.OfficeLocationCity = $DeviceOwner.CityCountry = $DeviceOwner.Country"Operating System" = $Device.OperatingSystem"O/S Version" = $Device.OperatingSystemVersionRegistered = $Device.RegistrationDateTime"Account Enabled" = $Device.AccountEnabledDeviceId = $Device.DeviceIdTrustType = $Device.TrustType}$Report.Add($ReportLine)} #End Foreach Device# Sort in order of last signed in date$Report = $Report | Sort-Object {$_.LastSignIn -as [datetime]} -Descending$Report | Out-GridView[array]$OldDevices = $Report | Where-Object {$_.'Days Since sign in' -ge 365}Write-Host ("There are {0} devices that have not signed in for over a year" -f $OldDevices.Count)$OldDevices | Format-Table Device, 'Device last signed in', 'Days since sign in', Owner, OwnerUPN, Department -AutoSize
Attribution
Author
Office365itpros