PowerShell get-childitem sort by date

In this PowerShell tutorial, I will show you a few examples of PowerShell get-childitem sort by date and PowerShell get-childitem sort by modified date. We will see how to sort files by their creation and modification dates using PowerShell.

PowerShell get-childitem sort by date

Let us see how to sort by creation date in PowerShell get-childitem.

The Get-ChildItem cmdlet is a versatile command used to list files and directories in PowerShell. By default, Get-ChildItem will retrieve items in an unsorted order. To sort these items by their creation date, you can pipe the results into the Sort-Object cmdlet.

Here’s a simple example that demonstrates how to sort files by creation date:

# This script will sort all items in the current directory by their creation date.

# Sorting in ascending order (oldest to newest)
Get-ChildItem | Sort-Object -Property CreationTime

This command will list all the items in the current directory, sorted by their creation date in ascending order (oldest first).

If you want to reverse the order and see the newest items first, you can add the -Descending switch:

Get-ChildItem | Sort-Object -Property CreationTime -Descending

Here is the PowerShell script:

# This script will sort all items in the current directory by their creation date.

# Sorting in ascending order (oldest to newest)
Get-ChildItem | Sort-Object -Property CreationTime

# Sorting in descending order (newest to oldest)
Get-ChildItem | Sort-Object -Property CreationTime -Descending

PowerShell Get-ChildItem Sort by Modified Date

Sorting by the modified date is just as easy. Instead of using the CreationTime property, you’ll use the LastWriteTime property, which reflects the last time the file was written to or modified.

Here’s how you can sort by the modified date:

Get-ChildItem | Sort-Object -Property LastWriteTime

And again, to sort in descending order:

Get-ChildItem | Sort-Object -Property LastWriteTime -Descending

Here is the complete script:

# This script will sort all items in the current directory by their last modified date.

# Sorting in ascending order (oldest to newest)
Get-ChildItem | Sort-Object -Property LastWriteTime

# Sorting in descending order (newest to oldest)
Get-ChildItem | Sort-Object -Property LastWriteTime -Descending

Practical Example – PowerShell get-childitem sort by date

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:

  1. List all PDF files in the C:\MyFolder directory.
  2. Sort them by the last modified date, with the most recent first.
  3. 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.

PowerShell get-childitem sort by date

Conclusion

Sorting files by date using Get-ChildItem in PowerShell is a powerful way to manage your file system. Whether you need to organize files by creation date or find the latest modified documents, Get-ChildItem combined with Sort-Object provides a simple yet effective solution.

  • PowerShell get-childitem sort by date
  • PowerShell get-childitem sort by modified date

You may also like:

>