gpt4 book ai didi

Powershell 删除具有特定名称的子文件夹

转载 作者:行者123 更新时间:2023-12-03 17:05:25 25 4
gpt4 key购买 nike

我是 PowerShell 的新手,我一直在尝试创建一个脚本,但到目前为止,我一直没有成功。我希望它完成一项特定的任务,即;

我需要能够在驱动器中搜索名为 "Cookies" 的特定文件夹。并删除它。问题仅在于文件夹 cookie 设置在多个位置。

例子 :

\\\myserver\test\User\Profiles\USER1\AppData\Roaming\Microsoft\Windows\Cookies
\\\myserver\test\User\Profiles\USER2\AppData\Roaming\Microsoft\Windows\Cookies
\\\myserver\test\User\Profiles\USER3\AppData\Roaming\Microsoft\Windows\Cookies
\\\myserver\test\User\Profiles\USER4\AppData\Roaming\Microsoft\Windows\Cookies

继续……

如何让 powershell 遍历所有这些不同的 USER 文件夹以搜索 Cookies 文件夹并将其删除。

我想出了这个,但我希望一个 powershell 大师可以帮助我。
$cookies= Get-ChildItem \\myserver\test\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies

foreach ($cookie in $cookies){
Remove-Item "$cookies" -Force -Recurse -ErrorAction SilentlyContinue
}

这行得通吗?

最佳答案

你快到了。无需在 $cookies 周围使用引号.

如果你做 foreach ($cookie in $cookies) ,然后操作 $cookie在脚本块中,而不是 $cookies .

这有效:

$cookies = Get-ChildItem \\myserver\test\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies

foreach ($cookie in $cookies){
Remove-Item $cookie -Force -Recurse -ErrorAction SilentlyContinue
}

但这也可以工作,没有循环:
$cookies = Get-ChildItem \\myserver\test\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies
Remove-Item $cookies -Force -Recurse -ErrorAction SilentlyContinue

如果你想要一个单线并且没有变量:
Get-ChildItem \\myserver\test\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue

关于Powershell 删除具有特定名称的子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54233099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com