Keep x number of files and delete all others - Powershell

前端 未结 4 650
天命终不由人
天命终不由人 2020-12-15 18:01

I am trying to write a script that will look through a set of folders and keep only the last 10 files. The files in each folder could be created daily, weekly or monthly. I

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 18:36

    You can sort by CreationTime descending and skip the first 10. If there are less than 10 files it will not remove any.

    gci C:\temp\ -Recurse| where{-not $_.PsIsContainer}| sort CreationTime -desc| 
        select -Skip 10| Remove-Item -Force
    

提交回复
热议问题