OneDrive for Business Unlink PC with Powershell

Welcome Forums General PowerShell Q&A OneDrive for Business Unlink PC with Powershell

Viewing 2 reply threads
  • Author
    Posts
    • #171772
      Participant
      Topics: 7
      Replies: 14
      Points: 29
      Rank: Member

      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.

      Thanks.

    • #171826
      Participant
      Topics: 0
      Replies: 115
      Points: 433
      Helping Hand
      Rank: Contributor

      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

       

    • #175084
      Participant
      Topics: 10
      Replies: 117
      Points: 457
      Helping Hand
      Rank: Contributor
      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 (
              [Alias("UserPrincipalName")]
              [Alias("UserEmail")]
              [Alias("EmailAddress")]
              [MailAddress]$Account
          )
      
          $accountsRegKeyPath = 'HKCU:\Software\Microsoft\OneDrive\Accounts'
      
          # 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
      
              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) {
      
                          if ($PSCmdlet.ShouldProcess("Delete OneDrive registry keys for $Account")) {
      
                              # Found (Default) value matching DisplayName
                              # Safe to delete all relative keys
      
                              Write-Output "Name is ""$oneDriveName"""
      
                              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"
      
                              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).
Viewing 2 reply threads
  • The topic ‘OneDrive for Business Unlink PC with Powershell’ is closed to new replies.