【问题标题】:Powershell delete subfolders with a specific namePowershell删除具有特定名称的子文件夹
【发布时间】:2019-01-17 18:20:40
【问题描述】:

我是 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   
}

这行得通吗?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    你快到了。无需在$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
    

    【讨论】:

    • @Roy 尽可能使用管道(最后一个示例)总是更可取的。
    • @Hoi Gert Jan,谢谢您,但我们的安全管理员希望对代码进行一些小改动。这是从 cookie 文件夹中删除 60 天之前的 .txt 文件,但我遇到了内存错误。 get-item -path D:\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies\* -filter "*.txt" -force | Where-Object {$_.LastWriteTime -lt (get-date).addDays(-60) }
    • @Roy 你的代码在我看来没问题。那里有很多 cookie 文件要收集吗?
    • 做得很好,尽管您应该使用Remove-Item $cookie.FullName,因为隐含的-Path 参数(-LiteralPath 同上)将其参数绑定为 字符串,并且在 Windows PowerShell Get-ChildItem situationally 字符串化为 mere 文件名,这会导致命令失败,或者更糟糕的是,删除错误的项目。这个特定的 Get-ChildItem 调用不会发生这种情况,但在传递 Get-ChildItem / Get-Item 作为 argument 时始终使用 .FullName 是一个好习惯(管道输入不受影响)。请参阅stackoverflow.com/a/53400031/45375 了解更多信息。
    • 是的,我有大约 700 个用户,每个用户都有大约 4000 个或更多的 cookie...
    【解决方案2】:

    试试这个衬里 -

    $path = "\\myserver\test\User\Profiles\"
    Get-ChildItem $path -Recurse -Directory | Where-Object {$_.Name -eq 'Cookies'} | % {Remove-Item $_.FullName}
    

    【讨论】:

    • 让我试试,我会及时通知你
    • 嗨 Vivek,我已经稍微更改了代码,因为我们希望有一个代码删除 .txt 文件的 cookie 文件,它们需要超过 60 天。 ` get-item -path D:\User\Profiles*\AppData\Roaming\Microsoft\Windows\Cookies* -filter "*.txt" -force | Where-Object {$_.LastWriteTime -lt (get-date).addDays(-60) } ` 但现在出现内存错误。
    【解决方案3】:

    这是一个稍微简化的版本。只是为了安全起见,我会先用-WhatIf 运行它来验证结果是否正确。然后只评论另一行

    $path = ""
    $pattern = ""
    Get-ChildItem $path -recurse -directory -include $pattern |
       Remove-Item -WhatIf
       #Remove-Item -Force -ErrorAction SilentlyContinue
    

    【讨论】:

    • 嗨 Bakudan,这是为了快速回复。我不得不稍微更改代码,因为我们的安全管理员希望它删除 cookie 文件夹中超过 60 天的 .txt 文件。但现在我得到一个内存错误。 get-item -path D:\User\Profiles\*\AppData\Roaming\Microsoft\Windows\Cookies\* -filter "*.txt" -force | Where-Object {$_.LastWriteTime -lt (get-date).addDays(-60) }
    • @Roy 我没有看到这样的错误,所以我无法帮助你。另一方面,关于 60 天以上的 cookie - 最好更新您的问题。
    猜你喜欢
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2012-10-13
    • 2014-10-22
    • 2021-06-06
    • 2013-04-12
    • 2016-01-09
    相关资源
    最近更新 更多