OneDrive Pause Sync for Disconnected Users

shashank 136 Reputation points
2023-06-22T07:25:41.1066667+00:00

We have shared machine where multiple users login and just lock their screen later. We want onedrive sync to pause for all such users who are not in active state on that machine. I am able to get the list of users programmatically in a text file who are not in active state like user1, user2...in separate lines in a text file.

I need a script where we can take the above user list as input and pause the onedrive sync for these users. Could you please help me with a script which takes usernames as input and pauses onedrive sync for them?

Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | OneDrive | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,766 Reputation points
    2023-06-22T12:45:14.65+00:00

    Hello there,

    PowerShell script that takes a list of users as input and pauses OneDrive sync for each user:

    $users = Get-Content -Path "C:\Path\to\UserList.txt" # Update the file path with your user list

    ForEach ($user in $users) {

    $onedrivePath = "C:\Users\$user\OneDrive"  # Update the path if your OneDrive folder has a different location
    
    if (Test-Path -Path $onedrivePath) {
    
        Write-Host "Pausing OneDrive sync for user: $user"
    
        Set-ItemProperty -Path "HKCU:\Software\Microsoft\OneDrive" -Name "DisableFileSyncNGSC" -Value 1
    
        Stop-Process -Name "OneDrive" -ErrorAction SilentlyContinue
    
    } else {
    
        Write-Host "OneDrive folder not found for user: $user"
    
    }
    

    }

    Instructions for using the script:

    Create a text file containing the list of user names, with each user name on a separate line. Save it as "UserList.txt" or update the file path in the script accordingly.

    Open a text editor, such as Notepad, and paste the script into a new file.

    Save the file with a ".ps1" extension, such as "PauseOneDriveSync.ps1".

    Open PowerShell with administrative privileges.

    Navigate to the directory where you saved the script using the cd command.

    Execute the script by running .\PauseOneDriveSync.ps1. If prompted, allow the script to run by adjusting the execution policy if necessary.

    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.