Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [string]$UninstallString = Get-Package -Name "Microsoft OneDrive" -ProviderName Programs -ErrorAction Ignore | ForEach-Object -Process {$_.Meta.Attributes["UninstallString"]}
- if ($UninstallString)
- {
- Write-Verbose -Message $Localization.OneDriveUninstalling -Verbose
- Stop-Process -Name OneDrive -Force -ErrorAction Ignore
- Stop-Process -Name OneDriveSetup -Force -ErrorAction Ignore
- Stop-Process -Name FileCoAuth -Force -ErrorAction Ignore
- # Getting link to the OneDriveSetup.exe and its' argument(s)
- [string[]]$OneDriveSetup = ($UninstallString -Replace("\s*/",",/")).Split(",").Trim()
- if ($OneDriveSetup.Count -eq 2)
- {
- Start-Process -FilePath $OneDriveSetup[0] -ArgumentList $OneDriveSetup[1..1] -Wait
- }
- else
- {
- Start-Process -FilePath $OneDriveSetup[0] -ArgumentList $OneDriveSetup[1..2] -Wait
- }
- # Getting the OneDrive user folder path
- $OneDriveUserFolder = Get-ItemPropertyValue -Path HKCU:\Environment -Name OneDrive
- if ((Get-ChildItem -Path $OneDriveUserFolder | Measure-Object).Count -eq 0)
- {
- Remove-Item -Path $OneDriveUserFolder -Recurse -Force
- }
- else
- {
- Write-Error -Message ($Localization.OneDriveNotEmptyFolder -f $OneDriveUserFolder) -ErrorAction SilentlyContinue
- Invoke-Item -Path $OneDriveUserFolder
- }
- Remove-ItemProperty -Path HKCU:\Environment -Name OneDrive, OneDriveConsumer -Force -ErrorAction Ignore
- Remove-Item -Path HKCU:\SOFTWARE\Microsoft\OneDrive -Recurse -Force -ErrorAction Ignore
- Remove-Item -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\OneDrive -Recurse -Force -ErrorAction Ignore
- Remove-Item -Path "$env:ProgramData\Microsoft OneDrive" -Recurse -Force -ErrorAction Ignore
- Remove-Item -Path $env:SystemDrive\OneDriveTemp -Recurse -Force -ErrorAction Ignore
- Unregister-ScheduledTask -TaskName *OneDrive* -Confirm:$false
- # Getting the OneDrive folder path
- $OneDriveFolder = Split-Path -Path (Split-Path -Path $OneDriveSetup[0] -Parent)
- # Save all opened folders in order to restore them after File Explorer restarting
- Clear-Variable -Name OpenedFolders -Force -ErrorAction Ignore
- $OpenedFolders = {(New-Object -ComObject Shell.Application).Windows() | ForEach-Object -Process {$_.Document.Folder.Self.Path}}.Invoke()
- # Terminate File Explorer process
- TASKKILL /F /IM explorer.exe
- # Attempt to unregister FileSyncShell64.dll and remove
- $FileSyncShell64dlls = Get-ChildItem -Path "$OneDriveFolder\*\amd64\FileSyncShell64.dll" -Force
- foreach ($FileSyncShell64dll in $FileSyncShell64dlls.FullName)
- {
- Start-Process -FilePath regsvr32.exe -ArgumentList "/u /s $FileSyncShell64dll" -Wait
- Remove-Item -Path $FileSyncShell64dll -Force -ErrorAction Ignore
- if (Test-Path -Path $FileSyncShell64dll)
- {
- Write-Error -Message ($Localization.OneDriveFileSyncShell64dllBlocked -f $FileSyncShell64dll) -ErrorAction SilentlyContinue
- }
- }
- # Restoring closed folders
- Start-Process -FilePath explorer
- foreach ($OpenedFolder in $OpenedFolders)
- {
- if (Test-Path -Path $OpenedFolder)
- {
- Invoke-Item -Path $OpenedFolder
- }
- }
- Remove-Item -Path $OneDriveFolder -Recurse -Force -ErrorAction Ignore
- Remove-Item -Path $env:LOCALAPPDATA\OneDrive -Recurse -Force -ErrorAction Ignore
- Remove-Item -Path $env:LOCALAPPDATA\Microsoft\OneDrive -Recurse -Force -ErrorAction Ignore
- Remove-Item -Path "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\OneDrive.lnk" -Force -ErrorAction Ignore
- }
Advertisement
Add Comment
Please, Sign In to add comment