Use the Get-ChildItem or ls command in PowerShell to get a list of files from directories and sort them by file attributes like creationtime or lastwritetime. It will list files sorted by date in ascending or descending order in PowerShell.
The file has attributes like creationtime, and lastwritetime that specify when was the file created and the file was last modified. Using these attributes, we can list files and sort them by DateTime.
In this article, we will discuss how to ls sort by date, and sort files based on their last updated time.
PowerShell ls sort by date
Unix command ls can be used in PowerShell to get the list of files and directories in the file system and their information.
To list files sorted by date, use the following PowerShell script.
ls | sort LastWriteTime -Descending | Select -First 5
In the above PowerShell script, the ls command gets the list of the files in the current directory and pipes the output to the sort command. sort command uses the file attribute LastWriteTime to sort files based on their last updated time with Descending property.
The Select command uses the First parameter to display the top 5 old files in order by date modified.
The output of the above PowerShell to ls sort by date is:
Cool Tip: How to get the last modified file in the directory using PowerShell!
PowerShell List files by Datetime using Get-ChildItem
The Get-ChildItem cmdlet in PowerShell gets the item in the directory and its information. To get the list of files and sort them by date and time, use the following script.
Get-ChildItem -Path D:\PS\ -File | Where-Object {$_.CreationTime} | Sort-Object CreationTime -Descending| Select-Object -First 5 | Select Length, Name, LastWriteTime,CreationTime
In the above PowerShell script, the Get-ChildItem gets the list of files from the path specified and pipes the output to the Where-Object command.
The where-Object
command uses the file attribute CreationTime
and pipes the output to Sort-Object. The Sort-Object
command uses the CreationTime to sort the files by creation time.
The above PowerShell script lists the top 5 files in order by their creation time.
The output of the above PowerShell to get files in directory order by creation time is:
PS D:\PS> Get-ChildItem -Path D:\PS\ -File | Where-Object {$_.CreationTime} | Sort-Object CreationTime -Descending | Select-Object -First 10 | Select Length, Name, LastWriteTime,CreationTime
Length Name LastWriteTime CreationTime
------ ---- ------------- ------------
23001 powershell-ls-files-sort-by-date.png 22-01-2023 09:40:58 22-01-2023 09...
38040 powershell-ls-sort-by-date.png 22-01-2023 09:28:44 22-01-2023 09...
9703 convert-timestamp-to-datetime-in-powershell.png 21-01-2023 21:17:36 21-01-2023 21...
22002 powershell-compare-file-modified-date-to-specific-date.png 21-01-2023 20:17:43 21-01-2023 20...
21729 powershell-compare-file-creation-date.png 21-01-2023 20:42:23 21-01-2023 20...
11529 powershell-compare-datetime-objects.png 21-01-2023 18:27:30 21-01-2023 18...
14098 powershell-compare-dates.png 21-01-2023 18:21:16 21-01-2023 18...
13091 powershell-get-date-minus-one-day.png 21-01-2023 17:18:12 21-01-2023 17...
720 employee.txt 21-01-2023 11:24:24 21-01-2023 11...
10936 get-windows-os-version.png 04-09-2022 22:00:55 04-09-2022 22...
PS D:\PS>
PowerShell Tip: How to get file creation date in PowerShell!
Use dir to sort files by date
In PowerShell, you can use the dir command to get the list of the files and their information. To perform the sorting files and get the latest filename by their last writetime, use the following script.
dir -File | sort LastWriteTime -Descending | Select -First 5
In the above PowerShell script, dir uses the File parameter to get the list of files from the current directory and pipes the output to the sort command.
sort
command uses the lastwritetime
attribute of the file to order files in descending.
The output of the above PowerShell script to find old files in orders by descending is:
PS D:\PS> dir -File | sort LastWriteTime -Descending | Select -First 5
Directory: D:\PS
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 22-01-2023 09:40 23001 powershell-ls-files-sort-by-date.png
-a---- 22-01-2023 09:28 38040 powershell-ls-sort-by-date.png
-a---- 21-01-2023 21:17 9703 convert-timestamp-to-datetime-in-powershell.png
-a---- 21-01-2023 20:42 21729 powershell-compare-file-creation-date.png
-a---- 21-01-2023 20:17 22002 powershell-compare-file-modified-date-to-specific-date.png
Cool Tip: How to list files in the directory in PowerShell!
Conclusion
I hope the above article on how to list files sorted by date and time is helpful to you.
You can use the Get-ChildItem, ls, or dir command to get the list of the files and the Sort
command to sort the files in ascending or descending order.
You can use the ls command with the -Hidden parameter to view hidden files in PowerShell.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.