Script to check if files or folders syncing with Onedrive

Prabhjot Singh 165 Reputation points
2024-05-08T09:25:16.3766667+00:00

Hi Team, Hope you are doing well. I am facing the issue related to get the Onedrive sync status via Powershell. Like my concern is before unlinking the account from onedrive need to check the Onedrive sync status, means if any files or folders are still syncing or status is Up to date. So, I need help to automate the process of checking sync status of onedrive, then stop onedrive process and unlink the account from Onedrive.

Below is the script, I am working on -

Check if the OneDrive process is running and stop it forcibly if it is

$onedriveProcess = Get-Process -Name "OneDrive" -ErrorAction SilentlyContinue

if ($onedriveProcess) {

$onedriveProcess | Stop-Process -Force

Write-Host "OneDrive process stopped successfully."
```} else {

Write-Host "OneDrive process not found or already stopped."


# Only attempt to check sync status if the process was found

if ($onedriveProcess) {

try {

$syncing = $true

while ($syncing) {

    $syncStatus = $onedriveProcess.Process.MainWindowTitle

    if ($syncStatus -eq "OneDrive is up to date") {

        $syncing = $false

    } else {

        Start-Sleep -Seconds 5

      Break

    }

}

} catch {

Write-Host "Could not retrieve OneDrive sync status. Assuming syncing is complete or not required."

}


When running this variable  $syncStatus only, I don't see any out. 

$syncStatus = $onedriveProcess.Process.MainWindowTitle

Can you help me to figure this out?

Thanks!
OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
879 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,187 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emi Zhang-MSFT 22,716 Reputation points Microsoft Vendor
    2024-05-09T00:29:19.31+00:00

    Hi,

    I suggest you post this issue to OneDrive for Developer forum:

    https://techcommunity.microsoft.com/t5/onedrive-developer/bd-p/OneDriveDeveloper

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments