How to Sort Files by Date in PowerShell?

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 LastWriteTime
  • Get-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 LastWriteTime

This 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:

powershell sort files by date

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 -Descending

This 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 LastWriteTime

Description

  • 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 LastWriteTime

This 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:

sort files by date in powershell

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 CreationTime

Example

To sort files in C:\MyFolder by their creation date in ascending order:

Get-ChildItem -Path C:\MyFolder | Sort-Object -Property CreationTime

This 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 -Descending

This 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:

Sort Files by Date in PowerShell Examples

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 LastAccessTime

Example

To sort files in C:\MyFolder by their last access time in ascending order:

Get-ChildItem -Path C:\MyFolder | Sort-Object -Property LastAccessTime

This 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 -Descending

This 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:

how to sort files by date in powershell

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.