title: Intune PowerShell: find devices where UPN is null
body: <p>I'm struggling a bit with the Intune Powershell cmdlets. I need to clean the devices list which contains thousands of <em>Intune registered</em> devices that have an enrolment date and no last-checking date (and therefore these would not be caught by the auto-purge). </p>
  
  <p>I could easily retrieve the list of devices where the users had left our Azure AD by running this command:</p>
  
  <pre><code>$devices = Get-IntuneManagedDevice -Filter "deviceName eq 'User deleted for this device'"
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>$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 ''"
  </code></pre>
  
  <p>I have used the Intune Admin portal (<a href="https://devicemanagement.microsoft.com/" rel="nofollow noreferrer">M365 Device Management</a>) to filter and export these devices to at least get a CSV list. The CSV file indicates that the UPN is equal to "None". </p>
  
  <p>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.</p>
  
  <pre><code>$item = Get-IntuneManagedDevice -managedDeviceId 1c0123a4-8324-48f7-b1c2-d4512345228a
  $item
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/hx5UH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hx5UH.png" alt="$item device object properties"></a></p>
  
  <p>Any advice ? 
  What is wrong with the UPN-based query ?</p>
  
  <p>Any help appreciated.</p>