PowerShell Where-Object Between Two Dates

If you want to filter items using Where-Object based on dates, then check out this PowerShell tutorial.

To filter objects by date in PowerShell using the Where-Object cmdlet, you can create DateTime objects for your desired date range and use comparison operators to specify the range. For example, to select files modified between two dates, you can use Get-ChildItem | Where-Object { $_.LastWriteTime -gt 'startDate' -and $_.LastWriteTime -lt 'endDate' }. To find items with a date greater than a certain date, use -gt (greater than), and for a specific date range, combine -gt and -lt (less than) with your date variables.

PowerShell where-object between two dates

One common task is to filter objects that fall between two specific dates in PowerShell. This can be useful for generating reports within a given time frame or cleaning up old files. In PowerShell, you can achieve this by creating two date objects to define your range and then using Where-Object to filter out the items that fall within this range.

Here’s an example that demonstrates how to filter files in a directory that were last written to between two dates using PowerShell Where-Object:

$startDate = Get-Date '2024-01-01'
$endDate = Get-Date '2024-01-31'
Get-ChildItem -Path C:\MyFolder | Where-Object { $_.LastWriteTime -gt $startDate -and $_.LastWriteTime -lt $endDate }

In this snippet, $startDate and $endDate are the date range we’re interested in. The Get-ChildItem cmdlet retrieves the files in the C:\Logs directory, and Where-Object filters them based on the LastWriteTime property.

You can see in the below screenshot the output, after I executed the PowerShell script using VS code.

PowerShell Where-Object Between Two Dates

PowerShell where-object date greater than

Sometimes, you may want to filter objects where the date is greater than a certain date in PowerShell. For instance, you might want to find all files created after a certain date to ensure you’re working with the most recent data.

Here’s how you can use Where-Object to filter for dates greater than a specified date:

$cutOffDate = Get-Date "2024-01-02"
Get-ChildItem -Path C:\MyFolder | Where-Object { $_.CreationTime -gt $cutOffDate }

In this example, $cutOffDate represents the date you want to use as your filter. The CreationTime property is used in conjunction with Where-Object to filter out files that were created after this date.

PowerShell where-object date range

Filtering within a date range is similar to filtering between two dates but often involves more precise control over the start and end times. This can be particularly useful when you need to generate reports for specific work hours or when you’re interested in events that occurred during a particular window of time.

Here’s an example that filters event logs within a specific date range:

$startDate = Get-Date "2024-02-01 08:00"
$endDate = Get-Date "2024-02-01 17:00"
Get-EventLog -LogName Application | Where-Object { $_.TimeGenerated -ge $startDate -and $_.TimeGenerated -le $endDate }

In this code, we’re looking at the Application event log and filtering for events that were generated during work hours on Feb 1, 2024. The -ge operator is used for “greater than or equal to” and -le for “less than or equal to”, ensuring that we include events at the exact start and end times.

Advanced Date Filtering in PowerShell using Where-Object

you can also use Where-Object for more advanced scenarios. For example, you might want to filter out files that were modified on the last day of any month or within a particular fiscal quarter.

Here’s a more complex example that finds files modified on the last day of any month:

Get-ChildItem -Path C:\Logs | Where-Object {
    $_.LastWriteTime.Day -eq [DateTime]::DaysInMonth($_.LastWriteTime.Year, $_.LastWriteTime.Month)


PowerShell’s Where-Object cmdlet is very much useful for filtering objects based on date properties. Whether you need to filter between two dates, for dates greater than a specific date, or within a date range, Where-Object provides the flexibility to handle these tasks efficiently.

In this PowerShell tutorial, I have explained how to filter items using where-object between two dates in PowerShell.

You may also like: