Recently, to analyze a log, I was required to compare two dates in PowerShell. PowerShell provides powerful built-in cmdlets and operators that make comparing dates easy. In this tutorial, I will show you how to compare dates in PowerShell. Let me show you different methods with practical examples.
Compare Dates Using PowerShell Comparison Operators
PowerShell allows you to directly compare DateTime objects using comparison operators such as -lt (less than), -gt (greater than), -le (less than or equal to), and -ge (greater than or equal to). Here’s an example:
$date1 = Get-Date "2025-05-15"
$date2 = Get-Date "2025-06-01"
if ($date1 -lt $date2) {
Write-Host "Date 1 is earlier than Date 2"
} else {
Write-Host "Date 1 is later than or equal to Date 2"
}
In this example, we compare two dates using the -lt operator. If $date1 is earlier than $date2, it will display “Date 1 is earlier than Date 2”. Otherwise, it will display “Date 1 is later than or equal to Date 2”.
Here is the exact output you can in the screenshot below after I executed the script using VS code.

You can also use other comparison operators like -gt, -le, and -ge based on your specific requirements. For instance, to check if a date falls within a certain range:
$startDate = Get-Date "2025-01-01"
$endDate = Get-Date "2025-12-31"
$targetDate = Get-Date "2025-07-04"
if ($targetDate -ge $startDate -and $targetDate -le $endDate) {
Write-Host "The target date falls within the specified range"
} else {
Write-Host "The target date is outside the specified range"
}
Here is the exact output in the screenshot below:

Check out How to Check If a Date Is Older Than 30 Days in PowerShell?
Compare Dates Without Time in PowerShell
Sometimes, you may want to compare dates without considering the time component. PowerShell makes this easy by allowing you to access the Date property of a DateTime object. Here’s an example:
$date1 = Get-Date "2025-05-15 09:30:00"
$date2 = Get-Date "2025-05-15 14:45:00"
if ($date1.Date -eq $date2.Date) {
Write-Host "The dates are the same, ignoring the time"
} else {
Write-Host "The dates are different"
}
In this case, even though $date1 and $date2 have different time components, the comparison only considers the date portion, and the output will be “The dates are the same, ignoring the time”.
Here is the exact output in the screenshot below:

Calculate Date Differences
PowerShell allows you to easily calculate the difference between two dates using the New-TimeSpan cmdlet. Here’s an example:
$startDate = Get-Date "2025-01-01"
$endDate = Get-Date "2025-12-31"
$dateDifference = New-TimeSpan -Start $startDate -End $endDate
Write-Host "The difference between the dates is $($dateDifference.Days) days"
In this example, we calculate the difference between $startDate and $endDate using New-TimeSpan. The resulting TimeSpan object provides properties like Days, Hours, Minutes, etc., allowing you to access the desired component of the date difference.
Check out Get-ChildItem Sort By Date in PowerShell
Compare Dates with a Threshold using PowerShell
In some scenarios, you may need to compare a date with a threshold, such as checking if a date is older than a certain number of days. Here’s an example:
$expirationDate = Get-Date "2025-06-30"
$currentDate = Get-Date
$thresholdDays = 30
if ($currentDate -gt $expirationDate.AddDays(-$thresholdDays)) {
Write-Host "The expiration date is within the threshold of $thresholdDays days"
} else {
Write-Host "The expiration date is older than the threshold of $thresholdDays days"
}
In this example, we compare the $expirationDate with the $currentDate while subtracting the $thresholdDays from the expiration date. If the current date is greater than the adjusted expiration date, it means the expiration date is within the threshold. Otherwise, it is older than the threshold.
Compare Dates in a Loop using PowerShell
Let me show you another very useful scenario.
When working with arrays or lists of dates, you can use loops to compare each date with a specific condition. Here’s an example:
$dates = @(
(Get-Date "2025-05-01"),
(Get-Date "2025-06-15"),
(Get-Date "2025-07-20"),
(Get-Date "2025-08-10")
)
$referenceDate = Get-Date "2025-07-01"
foreach ($date in $dates) {
if ($date -lt $referenceDate) {
Write-Host "$date is earlier than the reference date"
} elseif ($date -gt $referenceDate) {
Write-Host "$date is later than the reference date"
} else {
Write-Host "$date is the same as the reference date"
}
}
In this example, we have an array of dates, $dates, and a reference date, $referenceDate. We use a foreach loop to iterate over each date in the array and compare it with the reference date using the -lt, -gt, and -eq operators. The appropriate message is displayed based on the comparison result.
Check out How to Get the Last Modified Date of a File in PowerShell?
PowerShell Compare Dates With Time Zones
Here is another advanced date comparison scenario: How do you compare dates with time zones using PowerShell?
If you need to compare dates across different time zones, you can use the ToUniversalTime() method to convert the dates to UTC before comparison. For example:
$date1 = Get-Date "2025-05-15 09:30:00"
$date2 = Get-Date "2025-05-15 12:30:00"
$date1Utc = $date1.ToUniversalTime()
$date2Utc = $date2.ToUniversalTime()
if ($date1Utc -lt $date2Utc) {
Write-Host "Date 1 is earlier than Date 2 in UTC"
} else {
Write-Host "Date 1 is later than or equal to Date 2 in UTC"
}
Here is the exact output in the screenshot below:

Check out How to Convert String to DateTime in PowerShell?
Compare Dates With Custom Formats in PowerShell
There will be scenarios where you want to compare dates with custom formats using PowerShell.
PowerShell allows you to compare dates in custom formats using the -Format parameter of the Get-Date cmdlet. For example:
$dateString1 = "2025-05-15"
$dateString2 = "2025-06-01"
$date1 = Get-Date $dateString1 -Format "yyyy-MM-dd"
$date2 = Get-Date $dateString2 -Format "yyyy-MM-dd"
if ($date1 -lt $date2) {
Write-Host "Date 1 is earlier than Date 2"
} else {
Write-Host "Date 1 is later than or equal to Date 2"
}
Here is the exact output in the screenshot below:

Conclusion
In this tutorial, I explained various methods to compare dates using PowerShell. We saw how to compare dates using comparison operators in PowerShell. I have also explained how to compare dates with time zones and custom formats. We also saw, how to compare dates without time in PowerShell. Do let me know in the comment below if this tutorial helps you.
You may also like:

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.