Skip to Content

How to Validate OneDrive Directory Folder Synchronization with PowerShell Script

Looking for a powershell script that checks each users OneDrive for the existence of the three default synced directories “Documents”, “Desktop” and “Pictures”. Purpose being to confirm their enrolled system is synchronizing to OneDrive.

Problem Description

You need a PowerShell script that can comprehensively check each user’s OneDrive to confirm the presence of the default synchronized directories: “Documents”, “Desktop”, and “Pictures”. This script serves the purpose of validating whether users’ systems are effectively syncing with OneDrive. It’s essential for maintaining data integrity and ensuring a smooth user experience across the organization.

Solution

To verify that each user’s OneDrive is synchronizing the default directories (“Documents”, “Desktop”, and “Pictures”), you can use this PowerShell script:

$users = Get-MsolUser -All | Where-Object { $_.IsLicensed -eq $true }

foreach ($user in $users) {
$oneDriveUrl = "https://graph.microsoft.com/v1.0/users/$($user.UserPrincipalName)/drive/root/children"
$token = (Get-MsalToken -Scopes "https://graph.microsoft.com/.default").AccessToken
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $token"
}

$response = Invoke-RestMethod -Uri $oneDriveUrl -Headers $headers -Method Get
$folders = $response.value | Where-Object { $_.name -in "Documents", "Desktop", "Pictures" }

Write-Host "User: $($user.UserPrincipalName)"
if ($folders.Count -eq 3) {
Write-Host "All default directories are being synced. Great!" -ForegroundColor Green
}
else {
Write-Host "Missing default directories in OneDrive sync:" -ForegroundColor Red
$missingFolders = "Documents", "Desktop", "Pictures" | Where-Object { $_ -notin $folders.name }
Write-Host ($missingFolders -join ", ")
}
Write-Host ""
}

Here’s how the script works:

  1. It retrieves all licensed users in the tenant using Get-MsolUser.
  2. For each user, it constructs the OneDrive URL to access the root directory’s children using the Microsoft Graph API.
  3. It obtains an access token using Get-MsalToken with the necessary scope.
  4. It sends a GET request to the OneDrive URL with the access token in the headers.
  5. It filters the response to check for the presence of the default directories (“Documents”, “Desktop”, “Pictures”).
  6. It displays the user’s principal name and the sync status of their default directories.
    • If all three directories are found, it prints a success message.
    • If any directories are missing, it prints the missing directories.

To run this script, you need to have the MSAL.PS module installed. You can install it by running:

Install-Module MSAL.PS

Make sure you have the necessary permissions to access users’ OneDrive using the Microsoft Graph API.

This script provides a straightforward way to confirm OneDrive synchronization of default directories for each user, helping you ensure data consistency and backup.

By deploying this PowerShell script, you ensure proactive monitoring of OneDrive synchronization, enhancing user productivity and organizational efficiency. This solution empowers you to maintain a reliable sync environment, fostering a positive user experience and operational continuity.