Reputation: 1
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.
Upvotes: 0
Views: 4532
Reputation: 520
There's more to removing OneDrive. After the uninstall, instead of deleting OneDrive folders, you may want to first move their contents to the "regular" user folders before deleting them. In so doing you could have merge issues depending on how fresh the machine is.
The other thing I know of is how Windows interprets shell folders. Look in the registry at
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
and
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
for the directories Windows will use for Documents, Pictures, Downloads, Music, etc. The former uses absolute paths, while the latter uses %USERPROFILE%\
prefixes on the paths.
I ended up updating all of these (including values with GUID names) to get everything working.
Upvotes: 0
Reputation: 821
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!"
}
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...
Upvotes: 0
Reputation: 558
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}
}
Upvotes: 1