# Script to fetch location coordinates on Windows devices

Fetching the location coordinates helps identify where a device is currently located. The latitude and longitude coordinates of a device are crucial in determining the precise locations of remote workforce. This helps the IT admins ensure that employees use company-owned devices from permitted locations. Using Hexnode’s [ Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) action you can execute scripts to obtain the exact location coordinates of a Windows device.

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

PowerShell script
-----------------

PowerShell script to fetch Windows location coordinates

Add-Type -AssemblyName System.Device #Required to access System.Device.Location namespace $latitude\_and\_longitude = New-Object System.Device.Location.GeoCoordinateWatcher #Create the required object $latitude\_and\_longitude.Start() #Begin resolving current locaton while (($latitude\_and\_longitude.Status -ne 'Ready') -and ($latitude\_and\_longitude.Permission -ne 'Denied')) { Start-Sleep -Milliseconds 100 #Wait for discovery. } if ($latitude\_and\_longitude.Permission -eq 'Denied'){ Write-Error 'Access Denied for Location Information' } else { $latitude\_and\_longitude.Position.Location | Select Latitude,Longitude #Select the relevent results. }

   1

2

3

4

5

6

7

8

9

10

11

12

13

  Add-Type -AssemblyName System.Device \#Required to access System.Device.Location namespace

$latitude\_and\_longitude = New-Object System.Device.Location.GeoCoordinateWatcher \#Create the required object

$latitude\_and\_longitude.Start() \#Begin resolving current locaton

while (($latitude\_and\_longitude.Status -ne 'Ready') -and ($latitude\_and\_longitude.Permission -ne 'Denied')) {

 Start-Sleep -Milliseconds 100 \#Wait for discovery.

}

if ($latitude\_and\_longitude.Permission -eq 'Denied'){

 Write-Error 'Access Denied for Location Information'

} else {

 $latitude\_and\_longitude.Position.Location | Select Latitude,Longitude \#Select the relevent results.

}

   

 

  

This script uses `system.device.location(GeoCoordinateWatcher)` method to get the location coordinates. Once the action succeeds, you can view the location coordinates by navigating to the **Action History** page of the device on which the script has been executed. Click on *Show Output* corresponding to the Execute Custom Script action that had been initiated to view the location coordinates.

[![Fetched location coordinates of a Windows device](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/04/Location-coordinates-output-of-a-Windows-device.png "Location coordinates output of a Windows device") ](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/04/Location-coordinates-output-of-a-Windows-device.png)

 Notes:- This script works only if location is enabled on the device. To enable location settings, navigate to **Start > Settings > Privacy (Privacy & Security** in case of Windows 11) **> Location** and toggle on Location service/Location access.
- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.