As an IT professional, you may often need to automate tasks related to date comparisons. Recently, I got a requirement to check if a date is older than 30 days using PowerShell. In this tutorial, I will explain how to check if a date is older than 30 days in PowerShell.
To check if a date is older than 30 days in PowerShell, you can use the Get-Date cmdlet along with date arithmetic. First, subtract 30 days from the current date using (Get-Date).AddDays(-30), then compare it to your target date. For example:
$dateToCheck = [datetime]::Parse("2023-08-15")
$thresholdDate = (Get-Date).AddDays(-30)
if ($dateToCheck -lt $thresholdDate) {
Write-Output "The date is older than 30 days."
} else {
Write-Output "The date is within the last 30 days."
}This script sets $thresholdDate to 30 days before the current date and checks if $dateToCheck is older.
PowerShell If Date Is Older Than 30 Days
The Get-Date cmdlet allows us to retrieve and manipulate the current date and time. Using this cmdlet, we can easily compare dates to determine if they are older than a specified number of days.
To check if a date is older than 30 days, we’ll use the Get-Date cmdlet along with date arithmetic. The basic approach involves subtracting 30 days from the current date and comparing it to the date in question.
Here’s the syntax:
$dateToCheck = [datetime]::Parse("2023-08-15")
$thresholdDate = (Get-Date).AddDays(-30)
if ($dateToCheck -lt $thresholdDate) {
Write-Output "The date is older than 30 days."
} else {
Write-Output "The date is within the last 30 days."
}In this example:
$dateToCheckis the date you want to compare.$thresholdDateis the current date minus 30 days.- The
ifstatement checks if$dateToCheckis less than$thresholdDate.
I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Check out Get Date Differences Between Two Dates in PowerShell
Let me show you a few other methods to check if a date is older than 30 days in PowerShell.
Method 1: Using New-TimeSpan
The New-TimeSpan cmdlet can also be used to calculate the difference between two dates. Here’s an example:
$dateToCheck = [datetime]::Parse("2023-08-15")
$timeSpan = New-TimeSpan -Start $dateToCheck -End (Get-Date)
if ($timeSpan.Days -gt 30) {
Write-Output "The date is older than 30 days."
} else {
Write-Output "The date is within the last 30 days."
}In this example:
New-TimeSpancalculates the difference between$dateToCheckand the current date.- The if statement checks if the number of days in the time span is greater than 30.
You can see the output in the screenshot below:

Method 2: Using [datetime]::Now
You can directly use [datetime]::Now for date comparisons. Here’s how:
$dateToCheck = [datetime]::Parse("2023-08-15")
if ($dateToCheck -lt [datetime]::Now.AddDays(-30)) {
Write-Output "The date is older than 30 days."
} else {
Write-Output "The date is within the last 30 days."
}In this example:
[datetime]::Nowgets the current date and time.- The
AddDays(-30)method subtracts 30 days from the current date.
Here is the exact output in the screenshot below:

Check out How to Add Days to a Date in PowerShell?
Examples
Let me show you two examples of how we check if a date is older than 30 days.
Example 1: Checking File Modification Dates
Suppose you want to identify files in a directory that haven’t been modified in the last 30 days. Here’s how you can do it:
$directoryPath = "C:\MyFolder"
$thresholdDate = (Get-Date).AddDays(-30)
Get-ChildItem -Path $directoryPath | ForEach-Object {
if ($_.LastWriteTime -lt $thresholdDate) {
Write-Output "File: $($_.Name) is older than 30 days."
}
}In this script:
Get-ChildItemretrieves the files in the specified directory.ForEach-Objectiterates through each file.- The
ifstatement checks if the file’sLastWriteTimeis older than 30 days.
Example 2: Cleaning Up Old User Accounts
Imagine you need to clean up user accounts in Active Directory that haven’t been active for the last 30 days. Here’s a script to achieve that:
Import-Module ActiveDirectory
$thresholdDate = (Get-Date).AddDays(-30)
Get-ADUser -Filter * -Properties LastLogonDate | ForEach-Object {
if ($_.LastLogonDate -lt $thresholdDate) {
Write-Output "User: $($_.SamAccountName) has not logged in for over 30 days."
# Optionally, disable the user account
# Disable-ADAccount -Identity $_.SamAccountName
}
}In this script:
Get-ADUserretrieves all user accounts.- The
ifstatement checks if the user’sLastLogonDateis older than 30 days. - Optionally, you can disable the inactive user accounts.
Conclusion
In this tutorial, I explained how to use PowerShell to determine if a date is older than 30 days. You can efficiently automate tasks requiring date comparisons using the Get-Date cmdlet and simple date arithmetic. I have also explained how to use the New-TimeSpan cmdlet to check if a date is older than 30 days in PowerShell.
You may also like:
- PowerShell Get-Date Minus 1 Day
- Get Date and Time in 24-Hour Format in PowerShell
- Get Date Without Time in PowerShell
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.