With precaution, (in Windows) you can unlink OneDrive accounts using the registry; just wrap it in a PowerShell function.
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).