Reputation: 763
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:
Limit just some files
Order in descending mode
Do not list directory
Thanks for your help!
Upvotes: 43
Views: 98287
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
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
Reputation: 58931
Select-Object -first 10
Sort-Object LastWriteTime -Descending
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
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