【问题标题】:How do I remove a Folder in Powershell?如何在 Powershell 中删除文件夹?
【发布时间】:2019-03-24 06:25:22
【问题描述】:

我们有一个小脚本,可以创建按每日日期命名的文件夹。我有一个脚本可以删除超过 30 天的文件夹。

dir "\\nas\Backup_old\*" -ErrorAction SilentlyContinue |
Where { ((Get-Date) - $_.LastWriteTime).days -gt 30} |
Get-ChildItem -Recurse | Remove-Item -Recurse -Force

主要是它工作正常。具有竞争的子文件夹将被删除。 但是主文件夹仍然存在,并且 LastWriteTime 被绑定到脚本的运行时。文件夹是空的。有人有解决这个问题的想法吗?

【问题讨论】:

  • 我认为你可以查看 fol emty 文件夹和删除文件夹查看这里 hihttps://stackoverflow.com/questions/28631419/how-to-recursively-remove-all-empty-folders-in-powershell

标签: powershell scripting


【解决方案1】:

您可能只需要删除Get-ChildItem 的第二个实例(注意dir 只是Get-ChildItem 的别名),因为这会导致它删除第一个返回的每个目录的子目录:

Get-ChildItem "\\nas\Backup_old\*" -ErrorAction SilentlyContinue |
    Where-Object { ((Get-Date) - $_.LastWriteTime).days -gt 30} |
    Remove-Item -Recurse -Force -WhatIf

查看 WhatIf 输出,如果它现在看起来会删除您期望的内容,请删除 -WhatIf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2016-08-01
    • 2021-07-05
    相关资源
    最近更新 更多