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

In this tutorial, I will explain how to check if a date is older than 30 days using PowerShell. Recently, I helped my team troubleshoot an issue where we needed to identify files in a directory created more than 30 days ago to archive them. Let me explain how we fixed it.

PowerShell If Date Is Older Than 30 Days

Now, let me show you a few methods to check if a date is older than 30 days in PowerShell. I will also show you some examples of how we need to check this type of condition.

Using Get-Date and AddDays

The simplest way to check if a date is older than 30 days in PowerShell is to use the Get-Date cmdlet along with date arithmetic. The Get-Date cmdlet retrieves the current date and time. We can then subtract 30 days from the current date and compare it to our target date.

Here’s an example:

$targetDate = Get-Date "2024-10-01"  
$cutoffDate = (Get-Date).AddDays(-30)

if ($targetDate -lt $cutoffDate) {
    Write-Host "The target date is older than 30 days"
} else {
    Write-Host "The target date is within the last 30 days"
}

In this script:

  1. We set $targetDate to the date we want to check, in this case October 1, 2024.
  2. We calculate $cutoffDate by taking the current date (returned by Get-Date) and subtracting 30 days using the AddDays(-30) method.
  3. We compare $targetDate to $cutoffDate using the -lt (less than) operator.
  4. If $targetDate is less than (earlier than) $cutoffDate, we know it is older than 30 days.

In this example, we calculate the date 30 days ago and compare it with a specified date. If the specified date is older, a message is displayed.

I executed the above script using VS code; you can see the exact output in the screenshot below:

powershell if date is older than 30 days

Check out Compare Dates in PowerShell

Check If Date is Older than 30 Days for Multiple Files

Often we need to check multiple dates, such as when identifying files older than 30 days in a directory. We can use the Get-ChildItem cmdlet to retrieve the files and their LastWriteTime property, and then compare each date to our cutoff.

Here’s an example:

$path = "C:\MyFolder"
$cutoffDate = (Get-Date).AddDays(-30)

Get-ChildItem $path | Where-Object {$_.LastWriteTime -lt $cutoffDate}

This script:

  1. Sets $path to the directory we want to search
  2. Calculates the $cutoffDate as before
  3. Uses Get-ChildItem to retrieve all files in $path
  4. Pipes the files to Where-Object, which filters them, keeping only files where the LastWriteTime is less than (earlier than) $cutoffDate

The result is a list of files in the specified directory that were last modified more than 30 days ago.

Convert to DateTime Objects

When comparing dates, it’s best to convert the inputs to DateTime objects rather than comparing them as strings. This ensures accurate comparisons regardless of the original date format.

We can use the Get-Date cmdlet to parse a date string and convert it to a DateTime object:

$dateString = "2022-10-01"
$targetDate = Get-Date $dateString

Now $targetDate is a DateTime object representing October 1, 2022, which we can easily compare to other dates.

Check out PowerShell Get-Date Format

Examples: PowerShell Check If Date Is Older Than 30 Days

Now, let me show you some real scenarios with examples where we need to check if a date is older than 30 days.

Example-1: Delete Files Older than 30 Days

Here’s a script to delete files older than 30 days in a specified directory. Here we need to check if the date is greater than 30 days before deleting the files.

# Define the directory path
$directoryPath = "C:\Logs"

# Get the current date
$currentDate = Get-Date

# Calculate the date 30 days ago
$pastDate = $currentDate.AddDays(-30)

# Get all files in the directory
$files = Get-ChildItem -Path $directoryPath

# Loop through each file
foreach ($file in $files) {
    # Check if the file is older than 30 days
    if ($file.LastWriteTime -lt $pastDate) {
        # Delete the file
        Remove-Item -Path $file.FullName -Force
        Write-Output "Deleted file: $($file.FullName)"
    }
}

This script will loop through each file in the specified directory and delete those that are older than 30 days.

Read PowerShell Get-Date Minus 1 Day

Example-2: Archive Old Files Older Than 30 Days

Here is another example PowerShell administrators come across. A common task is to archive files that are older than a certain number of days. We can combine the techniques above to identify old files and move them to an archive directory.

Here, we need to check if the files are older than 30 days before archiving them.

Here’s an example script:

$path = "C:\MyFolder" 
$archivePath = "C:\MyFolder\Archive"
$cutoffDate = (Get-Date).AddDays(-30)

$oldFiles = Get-ChildItem $path | Where-Object {$_.LastWriteTime -lt $cutoffDate}

foreach ($file in $oldFiles) {
    Move-Item $file.FullName $archivePath
}

This script:

  1. Sets $path to the directory to search and $archivePath to the directory where old files will be moved
  2. Calculates the $cutoffDate as before
  3. Retrieves the list of $oldFiles using Get-ChildItem and Where-Object
  4. Loops through each $file in $oldFiles
  5. Uses Move-Item to move the file (specified by its FullName property) to $archivePath

After running this script, all files in $path that are older than 30 days will be moved to the $archivePath directory.

Conclusion

Here, I explained how to check if a date is older than 30 days using PowerShell. We covered multiple methods, including using Get-Date with AddDays, Where-Object, and ForEach-Object.

I also provided a practical example of how to use these techniques to archive files that are older than 30 days.

You may also like:

>