Windows 删除power shell中早于X天的文件夹X

Windows 删除power shell中早于X天的文件夹X,windows,powershell,Windows,Powershell,我正在使用PowerShell脚本删除超过x天的文件夹 $limit = (Get-Date).AddDays(-15) $path = "xxxx\path" # Delete files older than the $limit. Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Forc

我正在使用PowerShell脚本删除超过x天的文件夹

$limit = (Get-Date).AddDays(-15)
$path = "xxxx\path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse`
但它并不总是起作用,我也不知道出了什么问题。是否有其他方法可以删除某些文件夹(例如,如果有超过5个文件夹)。。?还是最好删除哪个旧的

我通过Powershell脚本进行备份,该脚本非常有效,但当我使用该脚本删除较旧的备份时,由于某些原因,它并不总是有效

有没有别的方法可以做到这一点

人类有很多爱


@Alex_p谢谢,这让人困惑,但在更改脚本路径后,它们似乎正在工作。在路径类似(Users/admin/folder test/script.p1)之前,我将其更改为(Users/admin/folder test/script.p1)

更换后,问题解决了,但仍不知道为什么会发生


谢谢大家,我想“Users/admin/folder test/script.p1”应该可以用了。路径中有一个空白。因此,“需要”

什么不起作用?它基本上不会删除文件夹,有时不会出现任何错误。有时抛出的错误是什么?@Alex\p谢谢,这很混乱,但在更改脚本路径后,它们似乎起作用了。在路径类似于(Users/admin/folder test/script.p1)之前,我将其更改为(Users/admin/folder test/script.p1)您是否考虑过编写一些测试用例和脚本来创建文件夹并将其创建日期设置为较旧的日期,然后调试如何根据需要查找它们?您可以去掉管道并将结果存储在中间变量中,以帮助调试。