我刚接触PowerShell,一直在尝试编写脚本,但是到目前为止,一直没有成功。我想让它执行一个特定任务,即:
我需要能够搜索驱动器中名为"Cookies"
的特定文件夹并将其删除。问题在于该文件夹Cookies位于多个位置。
例如:
\\\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遍历所有不同的用户文件夹以搜索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
}
这个能行吗?
get-item -path D:\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies\* -filter "*.txt" -force | Where-Object {$_.LastWriteTime -lt (get-date).addDays(-60) }
- RoyRemove-Item $cookie.FullName
,因为隐含的-Path
参数(对于-LiteralPath
也是如此)将其参数绑定为一个 _字符串_,而在 Windows PowerShell 中,Get-ChildItem
在 特定情况下 字符串化为 _纯文件名_,这会导致命令失败或更糟糕的是删除错误的项目。虽然在这个特定的Get-ChildItem
调用中不会发生这种情况,但总是在传递Get-ChildItem
/Get-Item
作为 参数 时使用.FullName
是一个好习惯(管道输入不受影响)。请参见 https://dev59.com/pa_la4cB1Zd3GeqP0vKJ#53400031 获取更多信息。 - mklement0