Recently, I worked on a PowerShell script that required date comparisons. In this tutorial, I will explain how to compare dates in PowerShell with examples.
To compare dates in PowerShell, you can use the DateTime object along with comparison operators such as -eq (equal), -ne (not equal), -lt (less than), -le (less than or equal), -gt (greater than), and -ge (greater than or equal). For example, to check if one date is earlier than another, you can use:
$date1 = Get-Date "2024-09-18"
$date2 = Get-Date "2024-09-19"
if ($date1 -lt $date2) {
Write-Output "Date1 is earlier than Date2"
} else {
Write-Output "Date1 is not earlier than Date2"
}This script compares two dates and outputs whether the first date is earlier than the second.
PowerShell Date Comparison
In PowerShell, dates are typically handled using the DateTime object. This powerful object allows you to perform various operations, including comparisons. Let me show you the syntax and methods for comparing dates in PowerShell.
Basic Syntax
To compare dates in PowerShell, you can use comparison operators such as -eq (equal), -ne (not equal), -lt (less than), -le (less than or equal), -gt (greater than), and -ge (greater than or equal). Here’s a basic example:
$date1 = Get-Date "2024-09-18"
$date2 = Get-Date "2024-09-19"
if ($date1 -lt $date2) {
Write-Output "Date1 is earlier than Date2"
} else {
Write-Output "Date1 is not earlier than Date2"
}In this example, we’re comparing two dates to determine if $date1 is earlier than $date2.
I executed the above PowerShell code, and you can see the output in the screenshot below:

Read How to Get Yesterday’s Date in PowerShell
Compare Dates Without Time
Sometimes, you might want to compare dates without considering the time component in PowerShell. To do this, you can format the dates to exclude the time part. Here’s how you can do it:
$date1 = (Get-Date "2024-09-18").ToString("yyyy-MM-dd")
$date2 = (Get-Date "2024-09-19").ToString("yyyy-MM-dd")
if ($date1 -eq $date2) {
Write-Output "The dates are the same."
} else {
Write-Output "The dates are different."
}We ensure that only the date part is compared by converting the dates to the “yyyy-MM-dd” format.
Here is the output in the screenshot below:

Read PowerShell Get-Date Format ISO 8601
PowerShell Date Comparison Examples
Now, let me show you some practical examples of date comparison in PowerShell.
Example 1: Check if a Date is in the Future
Imagine you need to check if a specific date is in the future. Here’s how you can do it:
$targetDate = Get-Date "2024-12-25"
$currentDate = Get-Date
if ($targetDate -gt $currentDate) {
Write-Output "The target date is in the future."
} else {
Write-Output "The target date is not in the future."
}In this scenario, we’re comparing $targetDate with the current date to determine if it’s a future date.
You can see the exact output in the screenshot below:

Example 2: Find Users Who Haven’t Logged In Recently
Suppose you’re an IT admin in Los Angeles, and you need to find users who haven’t logged in for the past 30 days. You can use the following script:
$cutoffDate = (Get-Date).AddDays(-30)
Get-ADUser -Filter * -Property LastLogonDate | ForEach-Object {
if ($_.LastLogonDate -lt $cutoffDate) {
Write-Output "$($_.Name) has not logged in for the past 30 days."
}
}This script retrieves all Active Directory users and checks their LastLogonDate against the cutoff date, which is 30 days ago.
Read How to Check If Date Is Older Than 30 Days in PowerShell?
Example 3: Compare Dates from Strings
Sometimes, dates are stored as strings and need to be converted to DateTime objects before comparison. Here’s an example:
$dateString1 = "2024-09-18"
$dateString2 = "2024-09-19"
$date1 = [DateTime]::ParseExact($dateString1, "yyyy-MM-dd", $null)
$date2 = [DateTime]::ParseExact($dateString2, "yyyy-MM-dd", $null)
if ($date1 -lt $date2) {
Write-Output "Date1 is earlier than Date2."
} else {
Write-Output "Date1 is not earlier than Date2."
}By using the ParseExact method, we convert the date strings into DateTime objects for accurate comparison.
Here is the exact output you can see in the screenshot below:

Conclusion
In this tutorial, I have explained how to compare dates in PowerShell and shown three real examples.
You may also like:
- Get Date Differences Between Two Dates in PowerShell
- How to Add Days to a Date in PowerShell?
- How to Add Months to Date in PowerShell?
- PowerShell Get Day of Week
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.