As an IT administrator, I recently needed to find the most recent log files in a directory to troubleshoot an issue. Using the Get-ChildItem cmdlet combined with the Sort-Object cmdlet, I quickly sorted the files by date and identified the relevant logs. In this tutorial, I will explain how to use the Get-ChildItem cmdlet in PowerShell to sort files by date.
PowerShell Get-ChildItem Sort By Date
The Get-ChildItem cmdlet in PowerShell allows you to retrieve information about files and directories. It can be used to list files and folders, filter results based on various criteria, and retrieve properties of files such as creation date, modification date, and size.
Get-ChildItem Sort By Date using Sort-Object
To sort the files retrieved by Get-ChildItem, we can pipe the results to the Sort-Object cmdlet. The Sort-Object cmdlet allows you to sort objects based on one or more properties in ascending or descending order.
Here’s an example of how to sort files by creation date in descending order:
Get-ChildItem -Path "C:\Logs" | Sort-Object -Property CreationTime -Descending
In this command:
- We use Get-ChildItem to retrieve the files in the “C:\Logs” directory.
- The results are piped (|) to the Sort-Object cmdlet.
- We specify the property to sort by using the -Property parameter, which in this case is “CreationTime”.
- The -Descending switch is used to sort the files in descending order, with the most recent files appearing first.
Get-ChildItem Sort Files by Creation Date
To sort files by their creation date in PowerShell, we can pipe the output of Get-ChildItem
to the Sort-Object
cmdlet. The Sort-Object
cmdlet allows us to sort objects based on their properties. Here is how you can sort files by creation date:
Get-ChildItem -Path "C:\MyFolder" | Sort-Object CreationTime
This command sorts the files in the specified directory by their creation date in ascending order.
Here is the exact output you can see in the screenshot below:
To sort in descending order, use the -Descending
parameter:
Get-ChildItem -Path "C:\MyFolder" | Sort-Object CreationTime -Descending
Check out Get the Last Modified Date of a File in PowerShell
Get-ChildItem Sort By Modified Date
Sometimes, you may need to sort files by their last modification date instead of the creation date. You can achieve this using the LastWriteTime
property.
Here is an example.
Get-ChildItem -Path "C:\MyFolder" | Sort-Object LastWriteTime
Again, to sort in descending order:
Get-ChildItem -Path "C:\MyFolder" | Sort-Object LastWriteTime -Descending
I executed the above cmdlet using VS code, and you can see the exact output in the screenshot below:
Read PowerShell Get-date Format Milliseconds
Filter and Sort Files
In many scenarios, you may want to filter files based on certain criteria before sorting them. For example, you might only be interested in text files. You can use the -Filter
parameter to achieve this:
Get-ChildItem -Path "C:\MyFolder" -Filter "*.txt" | Sort-Object LastWriteTime
This command filters the files to include only .txt
files before sorting them by their last modification date.
In some cases, you may need to sort files based on multiple criteria. For example, you might want to sort files first by their extension and then by their creation date. You can achieve this using multiple properties with Sort-Object
:
Get-ChildItem -Path "C:\MyFolder" | Sort-Object Extension, CreationTime
This command sorts the files first by their extension and then by their creation date within each extension group.
Read How to Get-Date Without Time in PowerShell?
PowerShell Get-ChildItem Sort By Date – Real Example
Let’s say you have a directory filled with various reports, and you want to find the most recently modified report. Here’s how you could use Get-ChildItem
to accomplish this:
Get-ChildItem -Path 'C:\MyFolder' -Filter *.pdf | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
This command will:
- List all PDF files in the
C:\MyFolder
directory. - Sort them by the last modified date, with the most recent first.
- Select the first item from the sorted list.
The result will be the most recently modified PDF report in the C:\MyFolder
directory.
You can see the output in the screenshot below once you execute the PowerShell script using Visual Studio code.
Conclusion
In this tutorial, I explained how to use PowerShell’s Get-ChildItem
cmdlet to sort files by date. We covered sorting by creation date and last write time, filtering files, and even automating file management with PowerShell scripts.
You may also like:
- PowerShell If Date Is Older Than 30 Days
- PowerShell Get-Date Add Days
- PowerShell Get-Date To String
- PowerShell Get-Date Format
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com