How to get N files in a directory order by last modified date? How to get N files in a directory order by last modified date? powershell powershell

How to get N files in a directory order by last modified date?


  • Limit just some files => pipe to Select-Object -first 10
  • Order in descending mode => pipe to Sort-Object LastWriteTime -Descending
  • Do not list directory => pipe to Where-Object { -not $_.PsIsContainer }

So to combine them together, here an example which reads all files from D:\Temp, sort them by LastWriteTime descending and select only the first 10:

Get-ChildItem -Force -Recurse -File -Path "C:\Users" -ErrorAction SilentlyContinue | Where-Object { $_.CreationTime.Date -lt (Get-Date).Date } | Sort CreationTime -Descending | Select-Object -First 10 CreationTime,FullName | Format-Table -Wrap 


This works for me:

PS> dir | Sort-Object LastAccessTime 

Its almost the same as the bash command:

$ ls -ltr

To Filter Directories:

PS> dir | Sort-Object LastAccessTime | Out-String -Stream | Select-String -NotMatch "^d"

(Personally, I think Microsoft should merge "out-string -stream" and "sls" to make a new command called "out-grep", so that powershell works more normally for bash users without messing around with customizing your shell. Who wants to type all of that junk just to grep a command output?)

The bash command would be:

$ ls -ltr | egrep -v "^d"


Martin Brandl's answer covers the reasons, so I won't repeat that, except that in newer Powershell versions you can use Get-ChildItem -File to list only files and not directories (folders). I don't know when it was introduced but it is in Powershell 5.1

Here are some examples of combining Sort-Object and Select-Object to limit and sort

List the 5 most recently accessed files (not directories) in the current directory, in descending order

Get-ChildItem -File | Sort-Object -Property LastAccessTime -Descending | Select-Object -First 5

The 5 most recently accessed files, current directory, in ascending order

Get-ChildItem -File | Sort-Object -Property LastAccessTime | Select-Object -Last 5

List the 5 oldest accessed files, current directory, descending order

Get-ChildItem -File | Sort-Object -Property LastAccessTime -Descending | Select-Object -Last 5

List the 5 oldest accessed files, current directory, ascending order

Get-ChildItem -File | Sort-Object -Property LastAccessTime | Select-Object -First 5

Note also that there are a few datetime file properties you could sort on. To get all of them for a file

Get-ChildItem | Get-Member -MemberType Property | Where-Object -Property Definition -Like "datetime*"   TypeName: System.IO.FileInfoName              MemberType Definition                           ----              ---------- ----------                           CreationTime      Property   datetime CreationTime {get;set;}     CreationTimeUtc   Property   datetime CreationTimeUtc {get;set;}  LastAccessTime    Property   datetime LastAccessTime {get;set;}   LastAccessTimeUtc Property   datetime LastAccessTimeUtc {get;set;}LastWriteTime     Property   datetime LastWriteTime {get;set;}    LastWriteTimeUtc  Property   datetime LastWriteTimeUtc {get;set;}