Skip to content

Instantly share code, notes, and snippets.

@johnmatczak
Last active June 18, 2018 21:02
Show Gist options
  • Save johnmatczak/3677d5eceb059de7a1fd660d3260d2ed to your computer and use it in GitHub Desktop.
Save johnmatczak/3677d5eceb059de7a1fd660d3260d2ed to your computer and use it in GitHub Desktop.
Removes OneDrive from the system completely via Powershell on Windows 10
# Kill OneDrive completely
# https://github.com/johnmatczak
# Self-elevate the script if not already elevated
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
echo "For better results, this script should be run as administator."
$elevate = Read-Host "Would you like to elevate this script to administator? [y\N] "
if ($elevate -like 'y') {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}
}
echo "Killing process"
taskkill.exe /F /IM "explorer.exe"
taskkill.exe /F /IM "OneDrive.exe"
echo "Running uninstaller"
if (Test-Path "$env:systemroot\SysWOW64\OneDriveSetup.exe") {
& "$env:systemroot\SysWOW64\OneDriveSetup.exe" /uninstall
}
if (Test-Path "$env:systemroot\System32\OneDriveSetup.exe") {
& "$env:systemroot\System32\OneDriveSetup.exe" /uninstall
}
echo "Removing leftover files"
rm -Recurse -Force -ErrorAction SilentlyContinue "$env:localappdata\Microsoft\OneDrive"
rm -Recurse -Force -ErrorAction SilentlyContinue "$env:programdata\Microsoft OneDrive"
rm -Recurse -Force -ErrorAction SilentlyContinue "C:\OneDriveTemp"
echo "Disabling via Group Policy"
force-mkdir "HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\OneDrive"
sp "HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\OneDrive" "DisableFileSyncNGSC" 1
echo "Removing scheduled task"
Get-ScheduledTask -TaskPath '\' -TaskName 'OneDrive*' -ea SilentlyContinue | Unregister-ScheduledTask -Confirm:$false
echo "Removing run hook"
reg load "hku\Default" "C:\Users\Default\NTUSER.DAT"
reg delete "HKEY_USERS\Default\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "OneDriveSetup" /f
reg unload "hku\Default"
echo "Removing from explorer sidebar"
New-PSDrive -PSProvider "Registry" -Root "HKEY_CLASSES_ROOT" -Name "HKCR"
mkdir -Force "HKCR:\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}"
sp "HKCR:\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}" "System.IsPinnedToNameSpaceTree" 0
mkdir -Force "HKCR:\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}"
sp "HKCR:\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}" "System.IsPinnedToNameSpaceTree" 0
Remove-PSDrive "HKCR"
echo "Removing startmenu shortcut"
rm -Force -ErrorAction SilentlyContinue "$env:userprofile\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\OneDrive.lnk"
echo "Restarting explorer.exe"
start "explorer.exe"
$dirtyremove = Read-Host "Do you want to remove all files and folders matching *onedrive* in $env:WinDir\WinSxS? [y\N] "
if ($dirtyremove -like 'y') {
echo "Removing"
foreach ($item in (ls "$env:WinDir\WinSxS\*onedrive*")) {
rm -Recurse -Force $item.FullName
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment