fred
fred

Reputation: 763

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

I want to search for one or few files with latest modified date in a big directory. Trying some PowerShell code but it does not work well for me.

Get-ChildItem 'D:\Temp' | Sort-Object LastWriteTime

I know that I can use -Recurse to search in all directories. But how to:

Thanks for your help!

Upvotes: 43

Views: 98287

Answers (4)

Kellen Stuart
Kellen Stuart

Reputation: 8893

The shortest and sweetest way I can write this

ls | sort LastAccessTime -Descending

These are just aliases for this

Get-ChildItem | Sort-Object LastAccessTime -Descending

Upvotes: 21

Jason S
Jason S

Reputation: 1544

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.FileInfo

Name              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;} 

Upvotes: 6

Martin Brandl
Martin Brandl

Reputation: 58931

  • 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 

Upvotes: 77

Bimo
Bimo

Reputation: 6597

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"

Upvotes: 23

Related Questions