Guest User

Untitled

a guest
Nov 30th, 2021
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [string]$UninstallString = Get-Package -Name "Microsoft OneDrive" -ProviderName Programs -ErrorAction Ignore | ForEach-Object -Process {$_.Meta.Attributes["UninstallString"]}
  2. if ($UninstallString)
  3. {
  4.     Write-Verbose -Message $Localization.OneDriveUninstalling -Verbose
  5.  
  6.     Stop-Process -Name OneDrive -Force -ErrorAction Ignore
  7.     Stop-Process -Name OneDriveSetup -Force -ErrorAction Ignore
  8.     Stop-Process -Name FileCoAuth -Force -ErrorAction Ignore
  9.  
  10.     # Getting link to the OneDriveSetup.exe and its' argument(s)
  11.     [string[]]$OneDriveSetup = ($UninstallString -Replace("\s*/",",/")).Split(",").Trim()
  12.     if ($OneDriveSetup.Count -eq 2)
  13.     {
  14.         Start-Process -FilePath $OneDriveSetup[0] -ArgumentList $OneDriveSetup[1..1] -Wait
  15.     }
  16.     else
  17.     {
  18.         Start-Process -FilePath $OneDriveSetup[0] -ArgumentList $OneDriveSetup[1..2] -Wait
  19.     }
  20.  
  21.     # Getting the OneDrive user folder path
  22.     $OneDriveUserFolder = Get-ItemPropertyValue -Path HKCU:\Environment -Name OneDrive
  23.     if ((Get-ChildItem -Path $OneDriveUserFolder | Measure-Object).Count -eq 0)
  24.     {
  25.         Remove-Item -Path $OneDriveUserFolder -Recurse -Force
  26.     }
  27.     else
  28.     {
  29.         Write-Error -Message ($Localization.OneDriveNotEmptyFolder -f $OneDriveUserFolder) -ErrorAction SilentlyContinue
  30.  
  31.         Invoke-Item -Path $OneDriveUserFolder
  32.     }
  33.  
  34.     Remove-ItemProperty -Path HKCU:\Environment -Name OneDrive, OneDriveConsumer -Force -ErrorAction Ignore
  35.     Remove-Item -Path HKCU:\SOFTWARE\Microsoft\OneDrive -Recurse -Force -ErrorAction Ignore
  36.     Remove-Item -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\OneDrive -Recurse -Force -ErrorAction Ignore
  37.     Remove-Item -Path "$env:ProgramData\Microsoft OneDrive" -Recurse -Force -ErrorAction Ignore
  38.     Remove-Item -Path $env:SystemDrive\OneDriveTemp -Recurse -Force -ErrorAction Ignore
  39.     Unregister-ScheduledTask -TaskName *OneDrive* -Confirm:$false
  40.  
  41.     # Getting the OneDrive folder path
  42.     $OneDriveFolder = Split-Path -Path (Split-Path -Path $OneDriveSetup[0] -Parent)
  43.  
  44.     # Save all opened folders in order to restore them after File Explorer restarting
  45.     Clear-Variable -Name OpenedFolders -Force -ErrorAction Ignore
  46.     $OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
  47.  
  48.     # Terminate File Explorer process
  49.     TASKKILL /F /IM explorer.exe
  50.  
  51.     # Attempt to unregister FileSyncShell64.dll and remove
  52.     $FileSyncShell64dlls = Get-ChildItem -Path "$OneDriveFolder\*\amd64\FileSyncShell64.dll" -Force
  53.     foreach ($FileSyncShell64dll in $FileSyncShell64dlls.FullName)
  54.     {
  55.         Start-Process -FilePath regsvr32.exe -ArgumentList "/u /s $FileSyncShell64dll" -Wait
  56.         Remove-Item -Path $FileSyncShell64dll -Force -ErrorAction Ignore
  57.  
  58.         if (Test-Path -Path $FileSyncShell64dll)
  59.         {
  60.             Write-Error -Message ($Localization.OneDriveFileSyncShell64dllBlocked -f $FileSyncShell64dll) -ErrorAction SilentlyContinue
  61.         }
  62.     }
  63.  
  64.     # Restoring closed folders
  65.     Start-Process -FilePath explorer
  66.     foreach ($OpenedFolder in $OpenedFolders)
  67.     {
  68.         if (Test-Path -Path $OpenedFolder)
  69.         {
  70.             Invoke-Item -Path $OpenedFolder
  71.         }
  72.     }
  73.  
  74.     Remove-Item -Path $OneDriveFolder -Recurse -Force -ErrorAction Ignore
  75.     Remove-Item -Path $env:LOCALAPPDATA\OneDrive -Recurse -Force -ErrorAction Ignore
  76.     Remove-Item -Path $env:LOCALAPPDATA\Microsoft\OneDrive -Recurse -Force -ErrorAction Ignore
  77.     Remove-Item -Path "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\OneDrive.lnk" -Force -ErrorAction Ignore
  78. }
Advertisement
Add Comment
Please, Sign In to add comment