Forum Discussion
Prabhjot768
May 02, 2024Copper Contributor
Unlink OneDrive account from PC
Hi Team,
Is there a way to automate the process of unlinking of OneDrive account?
As for 1-2 accounts, we perform manual steps: open the OneDrive settings, go to the account tab, and click Unlink this PC.
But for thousands Laptops/desktop we don’t want our techs to have to do the same. The idea is to stop the sync, unlink the account from this PC and from explorer remove OneDrive.
I am trying to run this script - # Function: Remove-OneDriveBusinessLink
# Details of the function and its purpose can be provided here
Param (
[Parameter(Mandatory = $true)]
[string]$UserEmail
)
$accountsRegKeyPath = 'HKCU:\Software\Microsoft\OneDrive\Accounts'
# Get subkeys matching “Business”
$businessSubKeys = Get-ChildItem -Path $accountsRegKeyPath | Where-Object { $_.Name -match 'Business' }
foreach ($subkey in $businessSubKeys) {
$oneDriveAccountKeyPath = "$accountsRegKeyPath\$($subkey.Name)"
if (Test-Path -Path $oneDriveAccountKeyPath) {
$accountEmail = (Get-ItemProperty -Path $oneDriveAccountKeyPath -Name UserEmail).UserEmail
if ($accountEmail -eq $UserEmail) {
Write-Output "Found matching account for $UserEmail in $oneDriveAccountKeyPath"
$displayName = (Get-ItemProperty -Path $oneDriveAccountKeyPath -Name DisplayName).DisplayName
$oneDriveName = "OneDrive - $displayName"
$desktopPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace'
$desktopSubKeys = Get-ChildItem -Path $desktopPath
foreach ($desktopSubKey in $desktopSubKeys) {
$subKeyName = $desktopSubKey.Name
$subKeyFullPath = Join-Path -Path $desktopPath -ChildPath $subKeyName
if ((Get-ItemProperty -Path $subKeyFullPath -Name '(Default)').'(Default)' -eq $oneDriveName) {
Write-Output "Found OneDrive link in registry directory: $subKeyFullPath"
if ($PSCmdlet.ShouldProcess("Delete OneDrive registry keys for $UserEmail")) {
Remove-Item -Path $subKeyFullPath -Recurse
Write-Output "Deleted registry key: $subKeyFullPath"
Remove-Item -Path $oneDriveAccountKeyPath -Recurse
Write-Output "Removed OneDrive account key: $oneDriveAccountKeyPath"
Stop-Process -Name OneDrive -Force
Write-Output "OneDrive process stopped"
}
}
}
}
}
}
Can anyone help me out to proceed with unlinking the account from Onedrive?
No RepliesBe the first to reply