Intune PowerShell: find devices where UPN is null

I'm struggling a bit with the Intune Powershell cmdlets. I need to clean the devices list which contains thousands of Intune registered devices that have an enrolment date and no last-checking date (and therefore these would not be caught by the auto-purge).

I could easily retrieve the list of devices where the users had left our Azure AD by running this command:

$devices = Get-IntuneManagedDevice -Filter "deviceName eq 'User deleted for this device'"

However I cannot get a working query to identify devices where the userPrincipalName (UPN) is null. I tried multiple variation of the filter and never got the same results as in the CSV. I copy below the variations I have tried:

$devices = Get-IntuneManagedDevice -Filter "userPrincipalName eq 'null'"
$devices = Get-IntuneManagedDevice -Filter "'userPrincipalName' eq 'null'"
$devices = Get-IntuneManagedDevice -Filter "userPrincipalName eq ''"
$devices = Get-IntuneManagedDevice -Filter "'userPrincipalName' eq ''"

I have used the Intune Admin portal (M365 Device Management) to filter and export these devices to at least get a CSV list. The CSV file indicates that the UPN is equal to "None".

If I then query a single device using another key from the CSV (like the managedDeviceId) then I get an object and I can confirm the UPN property is empty.

$item = Get-IntuneManagedDevice -managedDeviceId 1c0123a4-8324-48f7-b1c2-d4512345228a
$item

$item device object properties

Any advice ? What is wrong with the UPN-based query ?

Any help appreciated.


Solution 1:

Sorry for keeping this post alive after years. This is how I managed to get the devices without UPN:

get-intunemanageddevice | Get-MSGraphAllPages | where "userprincipalname" -eq ''