Home > database >  How can I Completely Uninstalling OneDrive and delete OneDrive related Folders on C:\ Drive?
How can I Completely Uninstalling OneDrive and delete OneDrive related Folders on C:\ Drive?

Time:04-22

I'm working on removing bloatware that is preinstalled on a number of computers.

I've been able to create a small script to remove the items that are preinstalled from the Microsoft Store and one that uninstalls Teams completely.

However; I'm having some troubles creating a solid script to uninstall OneDrive completely.

So far I have the below:

#Instructions found on https://www.wintips.org/how-to-disable-uninstall-install-onedrive-in-windows-10-8-7/]
#Modified slightly for simplicity and to kill the OneDrive process before uninstallation of application

#To Kill OneDrive.exe process
taskkill /f /im OneDrive.exe

#To uninstall OneDrive if using 64-bit System:
C:\windows\SysWOW64\OneDriveSetup.exe /uninstall

#To uninstall Onedrive if using a 32-bit system:
C:\windows\System32\OneDriveSetup.exe /uninstall

#Added to Removes the OneDrive Folders that are on the laptop.

$dirpath = "C:\Users\$env:UserName\OneDrive"
$dirpath2 = "C:\Users\$env:UserName\OneDrive - CompanyName"

#conditional to delete OneDrive related folders of C Drive. This is where I run into trouble
if ((test-path -LiteralPath $dirpath) -or (test-path -LiteralPath $dirpath2)) {(remove-Item -LiteralPath $dirpath) -or (remove-Item -LiteralPath $dirpath2)}


#Remove-Item -LiteralPath "C:\Users\$env:UserName\OneDrive" -Force -Recurse
#Remove-Item -LiteralPath "C:\Users\$env:UserName\OneDrive - CompanyName" -Force -Recurse

exit

It seems that there might be a logic issue with my conditional statement. When I run this script it does delete both folders that I'm intending to delete, but it returns "False" instead of "True" as I would expect.

I think what is happening is that it is running the remove-Item -LiteralPath $dirpath portion before it is able to reach the logical operator. I'm under this impression, because if I use the -and operator it will only remove the first Folder "C:\Users\$env:UserName\OneDrive"

Any suggestions to resolve this issue or improve the script overall would be appreciated. Thank you.

CodePudding user response:

You should use a foreach

$dirpaths = "C:\Users\$env:UserName\OneDrive", "C:\Users\$env:UserName\OneDrive - CompanyName"
Foreach ($dirpath in $dirpaths) {
if (test-path -LiteralPath $dirpath) {remove-Item -LiteralPath $dirpath}
}

CodePudding user response:

if ((test-path -LiteralPath $dirpath) -or (test-path -LiteralPath $dirpath2)) {(remove-Item -LiteralPath $dirpath) -or (remove-Item -LiteralPath $dirpath2)}

That logic is broken.

Try this:

if (test-path -LiteralPath $dirpath) {

  Remove-Item -LiteralPath $dirpath
  }
  
  elseif (test-path -LiteralPath $dirpath2){
    remove-Item -LiteralPath $dirpath2
  }
  else {
    Write-Error -Message "We ain't found shit!"
  }

Lastly

You don't need to do any of that because Remove-Item does not stop if it cannot find a file/path/directory - it continues.

Remove-Item  c:\thisisafakepath\, e:\anotherfakepath\, C:\Powershell\TestCSVs\out.csv -WhatIf -ErrorAction SilentlyContinue

Write-Host "Script continues..."

Outputs:

C:> . 'C:\Powershell\Scripts\trydelete.ps1'
What if: Performing the operation "Remove File" on target "C:\Powershell\TestCSVs\out.csv".
Script continues...
  • Related