How to Check If Date Is Older Than 30 Days in PowerShell?

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:

  • $dateToCheck is the date you want to compare.
  • $thresholdDate is the current date minus 30 days.
  • The if statement checks if $dateToCheck is less than $thresholdDate.

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

PowerShell If Date Is Older Than 30 Days

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-TimeSpan calculates the difference between $dateToCheck and 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:

Check If Date Is Older Than 30 Days in PowerShell

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]::Now gets 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:

powershell if date is less than 30 days

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-ChildItem retrieves the files in the specified directory.
  • ForEach-Object iterates through each file.
  • The if statement checks if the file’s LastWriteTime is 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-ADUser retrieves all user accounts.
  • The if statement checks if the user’s LastLogonDate is 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.