Script to Sync Windows Devices in Microsoft Graph

1 min. readlast update: 09.21.2024

This script connects to Microsoft Graph, retrieves all Windows devices, and sends a synchronization request to each device. The script:

  1. Prompts for Tenant ID: It asks the user to input the Tenant ID or primary domain.
  2. Connects to Microsoft Graph: Authenticates to Microsoft Graph using the provided tenant ID and the required scopes, including privileges for managing devices.
  3. Fetches Windows Devices: Retrieves all managed devices running Windows OS.
  4. Syncs Devices: Sends a synchronization request to each Windows device found, displaying the progress.

Here is the Script:

$TenantId = Read-Host "Please enter Tenant Id or Primary domain"
$Scopes = "DeviceManagementManagedDevices.ReadWrite.All, DeviceManagementManagedDevices.PrivilegedOperations.All"

Connect-MgGraph -TenantId $TenantId -Scopes $Scopes

$Devices = Get-MgDeviceManagementManagedDevice -All | Where-Object {$_.operatingSystem -eq 'Windows'}
Write-host " Number of Devices found: $($Devices.id.Count)" -ForegroundColor cyan

Foreach ($Device in $Devices) {
    $DeviceId = $Device.id
    Sync-MgDeviceManagementManagedDevice -ManagedDeviceId $DeviceId
    Write-Host "Sending Sync request to Device $($Device.deviceName)" -ForegroundColor Yellow
}

Was this article helpful?