Advertisement
AntoSVK

Remove One Drive Integration

Oct 18th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #   Description:
  2. # This script will remove and disable OneDrive integration.
  3.  
  4. #Import-Module -DisableNameChecking .\force-mkdir.psm1
  5. function force-mkdir($path) {
  6.     if (!(Test-Path $path)) {
  7.         #Write-Host "-- Creating full path to: " $path -ForegroundColor White -BackgroundColor DarkGreen
  8.         New-Item -ItemType Directory -Force -Path $path
  9.     }
  10. }
  11. #Import-Module -DisableNameChecking .\take-own.psm1
  12. function Takeown-File($path) {
  13.     takeown.exe /A /F $path
  14.     $acl = Get-Acl $path
  15.  
  16.     # get administraor group
  17.     $admins = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
  18.     $admins = $admins.Translate([System.Security.Principal.NTAccount])
  19.  
  20.     # add NT Authority\SYSTEM
  21.     $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($admins, "FullControl", "None", "None", "Allow")
  22.     $acl.AddAccessRule($rule)
  23.  
  24.     Set-Acl -Path $path -AclObject $acl
  25. }
  26.  
  27. function Takeown-Folder($path) {
  28.     Takeown-File $path
  29.     foreach ($item in Get-ChildItem $path) {
  30.         if (Test-Path $item -PathType Container) {
  31.             Takeown-Folder $item.FullName
  32.         } else {
  33.             Takeown-File $item.FullName
  34.         }
  35.     }
  36. }
  37.  
  38. echo "Kill OneDrive process"
  39. taskkill.exe /F /IM "OneDrive.exe"
  40. taskkill.exe /F /IM "explorer.exe"
  41.  
  42. echo "Remove OneDrive"
  43. if (Test-Path "$env:systemroot\System32\OneDriveSetup.exe") {
  44.     & "$env:systemroot\System32\OneDriveSetup.exe" /uninstall
  45. }
  46. if (Test-Path "$env:systemroot\SysWOW64\OneDriveSetup.exe") {
  47.     & "$env:systemroot\SysWOW64\OneDriveSetup.exe" /uninstall
  48. }
  49.  
  50. echo "Removing OneDrive leftovers"
  51. rm -Recurse -Force -ErrorAction SilentlyContinue "$env:localappdata\Microsoft\OneDrive"
  52. rm -Recurse -Force -ErrorAction SilentlyContinue "$env:programdata\Microsoft OneDrive"
  53. rm -Recurse -Force -ErrorAction SilentlyContinue "C:\OneDriveTemp"
  54.  
  55. echo "Disable OneDrive via Group Policies"
  56. force-mkdir "HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\OneDrive"
  57. sp "HKLM:\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows\OneDrive" "DisableFileSyncNGSC" 1
  58.  
  59. echo "Remove Onedrive from explorer sidebar"
  60. New-PSDrive -PSProvider "Registry" -Root "HKEY_CLASSES_ROOT" -Name "HKCR"
  61. mkdir -Force "HKCR:\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}"
  62. sp "HKCR:\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}" "System.IsPinnedToNameSpaceTree" 0
  63. mkdir -Force "HKCR:\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}"
  64. sp "HKCR:\Wow6432Node\CLSID\{018D5C66-4533-4307-9B53-224DE2ED1FE6}" "System.IsPinnedToNameSpaceTree" 0
  65. Remove-PSDrive "HKCR"
  66.  
  67. # Thank you Matthew Israelsson
  68. echo "Removing run hook for new users"
  69. reg load "hku\Default" "C:\Users\Default\NTUSER.DAT"
  70. reg delete "HKEY_USERS\Default\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "OneDriveSetup" /f
  71. reg unload "hku\Default"
  72.  
  73. echo "Removing startmenu entry"
  74. rm -Force -ErrorAction SilentlyContinue "$env:userprofile\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\OneDrive.lnk"
  75.  
  76. echo "Restarting explorer"
  77. start "explorer.exe"
  78.  
  79. echo "Waiting for explorer to complete loading"
  80. sleep 10
  81.  
  82. echo "Removing additional OneDrive leftovers"
  83. foreach ($item in (ls "$env:WinDir\WinSxS\*onedrive*")) {
  84.     Takeown-Folder $item.FullName
  85.     rm -Recurse -Force $item.FullName
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement