metinsaylan
metinsaylan
As an Amazon Associate, I earn from qualifying purchases.
  1. Home
  2. How To
  3. Snippets

Powershell – Sort Files by Last Modified Date

Staff // (Updated: )

If you want to sort files by last modified date in a folder, I would suggest you use PowerShell for this task. PowerShell makes it extremely easy to create a list of files by modified date. Here is a snippet you can use to create a files.dat including files list sorted by date:

gci -rec -file | sort LastWriteTime -Descending | select-object fullname,lastwritetime | out-file "files.dat" -width 4096

Usage

You can use copy and paste this line to a PowerShell terminal, or you can save it in a text formatted file with ps1 extension. When you right click and select Run With PowerShell option on that file, it will create files.dat with your files list. powershell order by


Explanation

gci -rec -file | sort LastWriteTime -Descending | select-object fullname,lastwritetime | out-file "files.dat" -width 4096

If you want to learn or extend this snippet, let me explain each step for you.

  • gci -rec -file
    This line is an alias for Get-ChildItem -Recurse -File. As you can understand it gets a list of files including subfolders using recursive search.
  • sort LastWriteTime -Descending
    tells powershell to sort all files using LastWriteTime column/property in descending order. This way the last modified file will be on top.
  • select-object fullname,lastwritetime
    We select only full path and lastwritetime, so other extra information is not needed
  • out-file "files.dat" -width 4096
    This section redirects all text output to “files.dat”

I hope you find this snippet useful somewhere, Enjoy!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More How To
Share Share Tweet