使用Powershell从大文件夹中删除旧文件

时间:2019-03-14 03:10:27

标签: powershell delete-file

我有一个很大的文件夹(在几个级别中包含子文件夹,总共包含数百万个文件)。我只想删除早于X天(例如10天)的文件。

我的以下脚本对于包含数千个文件的文件夹正常工作,但不适用于该大文件夹。有什么想法可以优化吗?谢谢!

$tmpList = Get-ChildItem -Path $sourceFolder -Recurse
$fileObjects = $tmpList `
        | Where-Object { !$_.PSIsContainer -and ($_.LastWriteTime -le $maxDateToProcess) } `
        | Sort-Object -Property "LastWriteTime" -Descending
$allFiles = $fileObjects | Select -ExpandProperty "FullName"
Remove-Item -Path $allFiles

1 个答案:

答案 0 :(得分:0)

键入以下命令以删除过去30天内未修改的文件,然后按Enter:

Get-ChildItem –Path "C:\path\to\folder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item