我如何在C:\ drive上完全卸载OnEdrive和OneDrive相关文件夹?

发布于 01-22 23:14 字数 1542 浏览 3 评论 0原文

我正在努力删除已预装在许多计算机上的膨胀软件。

我已经能够创建一个小脚本来删除从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦开始←不甜2025-01-29 23:14:52

您应该使用一个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}
}

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}
}
晚雾2025-01-29 23:14:52
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...
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...
泪是无色的血2025-01-29 23:14:52

还有更多要删除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名称的值)以使所有内容都起作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文