Use PowerShell and MS Graph to locate an Intune device

Damien Van Robaeys

In this post I will show you how to use PowerShell and MS Graph to locate an Intune device.

Prerequisites

You will first have to enable location on your device.

You can check it in the notification center as below:

You can enable it by clicking on it.

You can also enable it with PowerShell as below:

$Path = "SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location"# Enable locationNew-ItemProperty -Path "HKLM:\$Path" -Name "Value" -Type String -Value "Allow" -Force# Disable locationNew-ItemProperty -Path "HKLM:\$Path" -Name "Value" -Type String -Value "Deny" -Force

Locate device

Microsoft has added the possibility to locate an Intune device through the portal.

See the new alert from the what’s new in Intune link.

For that, just go to a device and click on Locate device, as below:

Locate with PowerShell

Now let’s do this with PowerShell.

1. You will first need to get the ID of the device. I will check device that has a specific name as below:

$Get_Device = Get-IntuneManagedDevice | Get-MSGraphAllPages | where{$_.deviceName -like "*POSTE-MAISON*"}

2. Then I will get the ID:

$Get_Device_ID = $Get_Device.ID

3. Now I will run the locate device action, as below:

$url_locate = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/$Get_Device_ID/locateDevice"Invoke-MSGraphRequest -Url $url_locate -HttpMethod POST

4. The next step is to check device location result using below command:

$url = "https://graph.microsoft.com/v1.0/deviceManagement/managedDevices/$Get_Device_ID"(Invoke-MSGraphRequest -Url $url -HttpMethod GET).deviceActionResults.deviceLocation

5. The result is empty because you have to wait a bit.

6. On you device, you will get the below notification:

7. Now let’s check again with PowerShell:

(Invoke-MSGraphRequest -Url $url -HttpMethod GET).deviceActionResults.deviceLocation

8. See below result:

Display in MAP and address 
Now let’s see how to get address info with PowerShell and how to open a MAP with device location.1. We will first get Latitude and Longitude

$Latitude = (Invoke-MSGraphRequest -Url $url -HttpMethod GET).deviceActionResults.deviceLocation.latitude$Longitude = (Invoke-MSGraphRequest -Url $url -HttpMethod GET).deviceActionResults.deviceLocation.longitude

2. We will show location in MAPS, as below:

Start-Process "https://www.google.com/maps?q=$Latitude,$Longitude"

3. See below the result:

4. We will get full location as below:

$Lat = $Latitude.replace(",",".")$Long = $Longitude.replace(",",".")$Location = "https://geocode.xyz/" + "$Lat,$Long" + "?geoit=json"Invoke-RestMethod $Location

5. See below the result: