2016-06-29 154 views
0

如何使用PowerShell删除文件夹而不是文件?我想删除超过3天的文件夹。Powershell删除文件夹

Get-ChildItem "D:\test" | 
    Where-Object { $_.PSIsContainer -and $_.LastWriteTime -le (Get-Date).AddDays(-3) } | 
    ForEach-Object { Remove-Item $_ -Force } 

这是行不通的。我没有收到任何错误,也没有删除d:\ test内的任何文件夹。

+2

嗨,有什么不工作?它不会删除任何东西?它会产生一个错误信息?请编辑您的问题以包含更多详细信息。 – sodawillow

回答

0

尝试:

Get-ChildItem "D:\test" | 
    Where-Object { $_.PSIsContainer -and $_.LastWriteTime -le (Get-Date).AddDays(-3) } | 
    Remove-Item -Force 

或:

Get-ChildItem "D:\test" | 
    Where-Object { $_.PSIsContainer -and $_.LastWriteTime -le (Get-Date).AddDays(-3) } | 
    ForEach-Object { Remove-Item $_.FullName -Force } 
+0

第一个工作完美,谢谢! – seanirishi

+0

不客气。此处的用法是通过单击答案投票旁边的复选标记来接受您喜欢的答案。 – sodawillow

-2

使用PowerShell 4+你不需要做所有花哨过滤你的假设,该-file开关会给你你需要什么。

Get-ChildItem -Path D:\test -File | Remove-Item 

以上将给你你需要的东西。

+0

-File会删除文件,而不是文件夹,就像OP问的...? –

+0

另外,'-File'开关是作为3.0的一部分引入的 – Matt