Disabling OneDrive the nice way

A part of my PowerShell script for customizing my Windows 10 images is to uninstall OneDrive. There weren’t any problems at first until more of our staff started using Office 365. They’d go in to their OneDrive and click “Get the OneDrive apps” which would redirect to a page that says they already had it installed.

Start OneDrive

This is supposed to use the odopen association to launch your running OneDrive client. If you just uninstall OneDrive from the computer, the “Start OneDrive” button would not open anything. In my script, when I uninstall OneDrive, I also modify odopen to launch the OneDriveSetup if anyone is trying to launch from the OneDrive website.

Get-Process -Name OneDrive -ErrorAction SilentlyContinue | Stop-Process -Force
If (Test-Path "$env:WinDir\SysWOW64\OneDriveSetup.exe") {
	$run = Start-Process -FilePath "$env:WinDir\SysWOW64\OneDriveSetup.exe" -ArgumentList "/uninstall" -Wait -PassThru
	REG ADD "HKCR\odopen\shell\open\command" /ve /t REG_SZ /d "$env:WinDir\SysWOW64\OneDriveSetup.exe" /f
} ElseIf (Test-Path "$env:WinDir\System32\OneDriveSetup.exe") {
	$run = Start-Process -FilePath "$env:WinDir\System32\OneDriveSetup.exe" -ArgumentList "/uninstall" -Wait -PassThru
	REG ADD "HKCR\odopen\shell\open\command" /ve /t REG_SZ /d "$env:WinDir\System32\OneDriveSetup.exe" /f
REG ADD "HKCR\odopen" /ve /t REG_SZ /d "URL: OneDrive Client Protocol" /f
REG ADD "HKCR\odopen" /v "URL Protocol" /t REG_SZ /f

In another section of my script, where I modify the user profiles, I will remove any instances of OneDrive (and OneDriveSetup) at startup.

If ($null -eq (Get-PSDrive HKU -ErrorAction SilentlyContinue)) { New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null }
$LoggedOnUsers = (Get-CimInstance -Class Win32_LoggedOnUser | Select-Object Antecedent -Unique).Antecedent.Name
$HiveList = "HKLM:\SYSTEM\CurrentControlSet\Control\hivelist"
$Hives = Get-Item -Path $HiveList | Select-Object -ExpandProperty Property | Where-Object {$_ -like "*\REGISTRY\USER\S-*" -And $_ -notlike "*_Classes*"}
$Users = (Get-ChildItem $env:SystemDrive\Users -Force | Where-Object {$_.PSIsContainer} | Where-Object {$_.Name -ne "Public"} | Where-Object {$_.Name -ne "All Users"}).Name
ForEach ($Name in $Users) {
 $ntuserpath = "$env:SystemDrive\Users\$Name\ntuser.dat"
 If (Test-Path $ntuserpath) {
   $RegPath = $null
   If ($LoggedOnUsers -like "*$Name*") {
     ForEach ($Hive in $Hives) {
       $HiveValue = (Get-ItemPropertyValue -Path "$HiveList" -Name "$Hive") -Replace "\\Device\\HarddiskVolume[0-9]*","$env:SystemDrive"
       If ($HiveValue -like "*\$Name\*") { $RegPath = $Hive.ToUpper().Replace("\REGISTRY\USER","HKU"); break }
   If ($null -eq $RegPath) { $RegPath = "HKLM\ntuser"; REG LOAD $RegPath "$ntuserpath" | Out-Null }
   REG ADD "$RegPath\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run" /v OneDrive /t REG_BINARY /d 030000005282a114a818d101 /f
   REG DELETE "$RegPath\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v OneDrive /f
   REG DELETE "$RegPath\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v OneDriveSetup /f
   REG DELETE "$RegPath\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{018D5C66-4533-4307-9B53-224DE2ED1FE6}" /f
   If ($RegPath -eq "HKLM\ntuser") { REG UNLOAD $RegPath | Out-Null }


One thing to note, though, is that when a user relaunches OneDriveSetup, it does initiate it across the whole computer again for all other users that then log in to the computer.