Keep x number of files and delete all others - Powershell

前端 未结 4 664
天命终不由人
天命终不由人 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:42

    @Andy: My rep is too low to comment so posting an answer. If you are getting strange results, it may be because you are looking in Windows Explorer which by default shows the Last Modified date instead of the Creation Date. To get results consistent with that shown in Windows Explorer, swap LastWriteTime for CreationTime like so: gci C:\temp\ -Recurse| where{-not $_.PsIsContainer}| sort LastWriteTime -desc | select -Skip 10| Remove-Item -Force

提交回复
热议问题