# OneDrive for Business Unlink PC with Powershell

**URL:** <https://forums.powershell.org/t/onedrive-for-business-unlink-pc-with-powershell/13024>\
**Category:** PowerShell Help\
**Created:** [August 20, 2019, 1:52pm UTC](https://forums.powershell.org/t/onedrive-for-business-unlink-pc-with-powershell/13024 "2019-08-20T13:52:37Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![aaron-hardy](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@aaron-hardy](https://forums.powershell.org/u/aaron-hardy)\
**Post date:** [September 6, 2019, 1:35pm UTC](https://forums.powershell.org/t/onedrive-for-business-unlink-pc-with-powershell/13024/3 "2019-09-06T13:35:26Z")

</div>

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](https://answers.microsoft.com/en-us/msoffice/forum/all/one-drive-unlink-account-via-command-line/d6439f7a-c847-4d27-97ae-3f772271f63f).

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 ( [Alias("UserPrincipalName")] [Alias("UserEmail")] [Alias("EmailAddress")] [MailAddress]$Account )

```
$<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).

---

_[View the full topic](https://forums.powershell.org/t/onedrive-for-business-unlink-pc-with-powershell/13024)._
