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
You can sort by CreationTime descending and skip the first 10. If there are less than 10 files it will not remove any.
CreationTime
gci C:\temp\ -Recurse| where{-not $_.PsIsContainer}| sort CreationTime -desc| select -Skip 10| Remove-Item -Force