OneDrive for Business Unlink PC with Powershell

Not sure if i’m searching with the right syntax, but I cannot find any hits on this. I find tones of back end scripts, but basically I want to script the process of unlinking a PC from OneDrive for Business.

The manual steps are to open the OneDrive settings, go to the account tab, and click Unlink this PC.

Sounds simple enough, and we don’t want our techs to have to do this across thousands of laptops/desktops. the idea is to stop the sync, but keep the local data folder intact as is.


Hello Jason,

Below is my recommendation. Note it still initiates a UAC prompt, but its a lot less clicking.

Stop-Process OneDrive
& $env:SystemRoot\SysWOW64\OneDriveSetup.exe /uninstall
& $env:SystemRoot\SysWOW64\OneDriveSetup.exe /install


With precaution, (in Windows) you can unlink OneDrive accounts using the registry; just wrap it in a PowerShell function.
To fully unlink an account, there are two registry keys to delete as per this Microsoft forum post.
Though this script does work, test it thoroughly before deploying in production. Ensure all needed data is fully synced.
Function Remove-OneDriveBusinessLink {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
    param (
$<span>accountsRegKeyPath = 'HKCU:\Software\Microsoft\OneDrive\Accounts'</span>

# Get subkeys matching “Business” (may be more than 1)
$businessSubKeys = (Get-Item $accountsRegKeyPath).GetSubKeyNames() -match ‘^Business’

# Loop through subkeys and find the one with the specified Account matching the value of
# the UserEmail property
foreach ($subkey in $businessSubKeys) {

    $oneDriveAccountKeyPath = Join-Path -Path $accountsRegKeyPath -ChildPath $subkey</span>

if ((Get-ItemProperty -Path $oneDriveAccountKeyPath -Name UserEmail).UserEmail -match “^$Account$”) {

Write-Output “Found $Account in $oneDriveAccountKeyPath”

# Get the DisplayName. This is the only way to reference another registry key that
# needs to be deleted
$displayName = (Get-ItemProperty -Path $oneDriveAccountKeyPath -Name DisplayName).DisplayName
$oneDriveName = “OneDrive - $displayName”

# Get all subkeys in below regkey location. The subkey containing (Default) value matching
# DisplayName will be deleted
$desktopPath = ‘HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace’
$desktopSubKeys = (Get-Item $desktopPath).GetSubKeyNames()

foreach ($subKey in $desktopSubKeys) {

$subKeyFullPath = Join-Path -Path $desktopPath -ChildPath $subKey

            if ((Get-ItemProperty -Path $subKeyFullPath).'(Default)' -eq $oneDriveName) {</span>

if ($PSCmdlet.ShouldProcess(“Delete OneDrive registry keys for $Account”)) {

# Found (Default) value matching DisplayName
# Safe to delete all relative keys

                    <span>Write-Output "Name is ""$oneDriveName"""</span>

Remove-Item -Path $subKeyFullPath -Recurse

Write-Output “Deleted (Default) in $subKeyFullPath containing “”$oneDriveName”“”

# Delete the account key path
Remove-Item -Path $oneDriveAccountKeyPath -Recurse
Write-Output “Removed $oneDriveAccountKeyPath”

                    </span><span>Stop-Process -Name OneDrive
                    Write-Output "OneDrive process stopped"


Once OneDrive has been stopped, use File Explorer to open the OneDrive folder and confirm all the synced data will be there - done it many times.
Proceed to uninstalling OneDrive (if applicable).