Recently, someone asked me about sorting files by date in PowerShell. In this tutorial, I will show you how to sort files by date in PowerShell using different methods with examples.
To sort files by date in PowerShell, you can use the Get-ChildItem cmdlet combined with Sort-Object. For example, to sort files in the C:\Logs directory by their last modified date in ascending order, you would use the command Get-ChildItem -Path C:\Logs | Sort-Object -Property LastWriteTime. To sort in descending order, simply add the -Descending switch: Get-ChildItem -Path C:\Logs | Sort-Object -Property LastWriteTime -Descending.
PowerShell Sort Files by Date
Sometimes, you might want to sort files by date, such as when they were last modified or created.
Now, let me show you different methods to sort files by date in PowerShell.
Method 1: Using Get-ChildItem and Sort-Object
The best method to sort files by date in PowerShell is by using the Get-ChildItem cmdlet in combination with Sort-Object. Here’s how you can do it:
Syntax
Get-ChildItem -Path <DirectoryPath> | Sort-Object -Property LastWriteTimeGet-ChildItem: Retrieves the files and directories in the specified path.Sort-Object: Sorts objects based on the specified property, in this case,LastWriteTime.
Example
Let’s say you have a directory C:\MyFolder, and you want to sort the files by their last modified date:
Get-ChildItem -Path C:\MyFolder | Sort-Object -Property LastWriteTimeThis command lists all files in C:\MyFolder sorted by their last write time in ascending order, making it easier to identify the most recently modified files.
I executed the above PowerShell script, and you can see the output in the screenshot below:

Sort in Descending Order
If you want to sort the files in descending order, you can add the -Descending switch to the Sort-Object cmdlet:
Get-ChildItem -Path C:\MyFolder | Sort-Object -Property LastWriteTime -DescendingThis command sorts the files so that the most recently modified files appear at the top of the list.
Check out Get the First 10 Files in a Folder Using PowerShell
Method 2: Filter Files by Date Range
Sometimes, you might need to filter files within a specific date range before sorting them. You can achieve this using the Where-Object cmdlet.
Syntax
Get-ChildItem -Path <DirectoryPath> | Where-Object { $_.LastWriteTime -ge <StartDate> -and $_.LastWriteTime -le <EndDate> } | Sort-Object -Property LastWriteTimeDescription
Where-Object: Filters objects based on a script block condition.LastWriteTime -ge <StartDate>: Specifies the start date.LastWriteTime -le <EndDate>: Specifies the end date.
Example
To filter and sort files in C:\MyFolder modified between January 1, 2024, and September 1, 2024, you can use the following script:
$startDate = Get-Date "2024-01-01"
$endDate = Get-Date "2024-09-01"
Get-ChildItem -Path C:\MyFolder | Where-Object { $_.LastWriteTime -ge $startDate -and $_.LastWriteTime -le $endDate } | Sort-Object -Property LastWriteTimeThis command filters the files to include only those modified within the specified date range and then sorts them by their last write time.
Here is the output in the screenshot below:

Read Get Unique Lines from a File Using PowerShell
Method 3: Sort by Creation Date
If you prefer to sort files by their creation date, you can modify the property in the Sort-Object cmdlet to CreationTime. This is useful when you need to organize files based on when they were initially created.
Syntax
Get-ChildItem -Path <DirectoryPath> | Sort-Object -Property CreationTimeExample
To sort files in C:\MyFolder by their creation date in ascending order:
Get-ChildItem -Path C:\MyFolder | Sort-Object -Property CreationTimeThis command lists all files in C:\MyFolder sorted by their creation time, helping you identify the oldest files in the directory.
Sort in Descending Order
To sort the files by their creation date in descending order, use the -Descending switch:
Get-ChildItem -Path C:\MyFolder | Sort-Object -Property CreationTime -DescendingThis command sorts the files so that the most recently created files appear at the top of the list.
Here is the output in the screenshot below:

Read How To Create File If Not Exists In PowerShell?
Method 4: Sort by Last Access Time
Similarly, you can sort files by their last access time. This is useful for identifying files that have been recently accessed or used.
Syntax
Get-ChildItem -Path <DirectoryPath> | Sort-Object -Property LastAccessTimeExample
To sort files in C:\MyFolder by their last access time in ascending order:
Get-ChildItem -Path C:\MyFolder | Sort-Object -Property LastAccessTimeThis command lists all files in C:\MyFolder sorted by their last access time, helping you identify the files that have been accessed most recently.
Sort in Descending Order
To sort the files in PowerShell by their last access time in descending order, use the -Descending switch:
Get-ChildItem -Path C:\MyFolder | Sort-Object -Property LastAccessTime -DescendingThis command sorts the files so that the most recently accessed files appear at the top of the list.
You can see the output in the screenshot below:

Conclusion
In this tutorial, I explained how to sort files by date in PowerShell using different methods. Whether you need to sort by last modified date, creation date, or last access time, PowerShell provides methods to achieve this. I have also explained some real examples for each method.
You may also like the following tutorials:
- How to search for files recursively in PowerShell?
- Delete Files Older Than X Days in PowerShell
- Add Date to Filename Using PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.