PowerShell: Use Get-ChildItem with Specific Date Range


You can use the following syntax in PowerShell to use the Get-ChildItem cmdlet to retrieve all files that were created during a specific date range:

Get-ChildItem -Recurse | Select Name, CreationTime | Sort CreationTime |
? { $_.CreationTime -ge "04/25/2024" -and $_.CreationTime -le "05/04/2024" }

This particular example will return all files in the current directory that were created between 4/25/2024 and 5/4/2024.

Note that the ? statement is an alias for the Where-Object cmdlet.

Also note that we use the Select statement to only return the file name and the creation date instead of any other information about the files. This is optional.

The following example shows how to use this syntax in practice.

Example: Use Get-ChildItem with Specific Date Range in PowerShell

We can use the following syntax to view all files in our current directory, sorted by their creation date:

Get-ChildItem -Recurse | Select Name, CreationTime | Sort CreationTime

The following screenshot shows how to use this syntax in practice:

We can see that the creation dates for the files in our current directory range from 3/8/2024 to 5/4/2024.

Suppose that we would like to use the Get-ChildItem cmdlet to only return the files that were created between 4/25/2024 and 5/4/2024.

We can use the following syntax to do so:

Get-ChildItem -Recurse | Select Name, CreationTime | Sort CreationTime |
? { $_.CreationTime -ge "04/25/2024" -and $_.CreationTime -le "05/04/2024" }

The following screenshot shows how to use this syntax in practice:

PowerShell Get-ChildItem with specific date range

Notice that this only returns the files in the current directory that were created between 4/25/2024 and 5/4/2024.

Feel free to change the start and end dates to any dates that you would like to filter using a different date range.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: Use Get-ChildItem to Return Files Only
PowerShell: Use Get-ChildItem and Exclude Specific Items
PowerShell: Use Get-ChildItem and Limit Recursion Depth
PowerShell: Use Get-ChildItem with Multiple Filters

Leave a Reply