我如何在C:\ drive上完全卸载OnEdrive和OneDrive相关文件夹?
我正在努力删除已预装在许多计算机上的膨胀软件。
我已经能够创建一个小脚本来删除从Microsoft商店预装的项目,并且完全卸载了团队。
然而;我遇到了一些麻烦,创建一个可靠的脚本来完全卸载OneDrive。
到目前为止,我有以下内容:
#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
我的有条件陈述似乎可能存在逻辑问题。当我运行此脚本时,它确实删除了我打算删除的两个文件夹,但是它会返回“ false”,而不是我期望的“ true”。
我认为正在发生的事情是,它正在运行remove -item -literalpath $ dirPath
部分,然后才能到达逻辑运算符。我会遇到这种印象,因为如果我使用- 和
操作员,它将仅删除第一个文件夹“ c:\ users \ $ env:username \ oneedrive”
解决这个问题或改进脚本的建议将不胜感激。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
if ((test-path -LiteralPath $dirpath) -or (test-path -LiteralPath $dirpath2)) {(remove-Item -LiteralPath $dirpath) -or (remove-Item -LiteralPath $dirpath2)}
该逻辑被打破了。
尝试以下操作:
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!"
}
最后,
您无需执行任何操作,因为如果找不到文件/路径/目录,则删除项目不会停止 - 它会继续。
Remove-Item c:\thisisafakepath\, e:\anotherfakepath\, C:\Powershell\TestCSVs\out.csv -WhatIf -ErrorAction SilentlyContinue
Write-Host "Script continues..."
输出:
C:> . 'C:\Powershell\Scripts\trydelete.ps1'
What if: Performing the operation "Remove File" on target "C:\Powershell\TestCSVs\out.csv".
Script continues...
还有更多要删除OneDrive。卸载后,您可能不再删除OnEdrive文件夹,而是首先将其内容移至“常规”用户文件夹之前,然后再删除它们。这样做,您可能会根据机器的新鲜度来合并问题。
我知道的另一件事是Windows如何解释Shell文件夹。 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目录Windows将用于文档,图片,下载,音乐等。前者使用绝对路径,而后者则使用%userProfile%\
路径上的前缀。
我最终更新了所有这些(包括带有GUID名称的值)以使所有内容都起作用。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您应该使用一个foreach
You should use a foreach