Skip to main content
TopMiniSite

Back to all posts

How to Check If A File Is Older Than A Certain Time With Powershell?

Published on
3 min read
How to Check If A File Is Older Than A Certain Time With Powershell? image

Best PowerShell Scripts to Buy in November 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS

BUY & SAVE
$39.49
Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS
3 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

  • MASTER WORKFLOW AUTOMATION WITH POWERSHELL FOR SYSADMINS!
  • LEARN PRACTICAL SKILLS FOR EFFICIENT SYSTEM MANAGEMENT TASKS!
  • DIVE INTO A USER-FRIENDLY PAPERBACK THAT ENHANCES YOUR TOOLKIT!
BUY & SAVE
$24.82 $39.99
Save 38%
PowerShell for Sysadmins: Workflow Automation Made Easy
4 Learn PowerShell Scripting in a Month of Lunches

Learn PowerShell Scripting in a Month of Lunches

BUY & SAVE
$50.00
Learn PowerShell Scripting in a Month of Lunches
5 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
6 Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell

BUY & SAVE
$30.87
Mastering PowerShell Scripting: Automate repetitive tasks and simplify complex administrative tasks using PowerShell
7 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76 $49.95
Save 26%
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
+
ONE MORE?

To check if a file is older than a certain time with PowerShell, you can use the Get-Item cmdlet to retrieve information about the file and then compare the LastWriteTime or CreationTime property of the file to the current date and time. You can also use the New-TimeSpan cmdlet to calculate the difference between the current date and time and the LastWriteTime or CreationTime of the file to determine how old the file is. Finally, you can use an if statement to check if the file is older than a certain time by comparing the calculated time difference to a predefined threshold.

How to delete files older than a certain date in PowerShell?

You can delete files older than a certain date in PowerShell using the following approach:

  1. First, specify the date threshold by which you want to delete files. You can use the Get-Date cmdlet to create a date object that represents the cutoff date. For example, to specify a date of January 1, 2020, you can use the following command:

$thresholdDate = Get-Date "01/01/2020"

  1. Next, use the Get-ChildItem cmdlet to retrieve a list of files in a specific directory. You can use the -File parameter to only get files (not directories) and the -Recurse parameter to search subdirectories as well. For example, to retrieve a list of files in the C:\MyFolder directory, you can use the following command:

$files = Get-ChildItem -Path "C:\MyFolder" -File -Recurse

  1. Finally, iterate through the list of files and delete the ones that are older than the specified date threshold. You can use the Where-Object cmdlet to filter the files based on their LastWriteTime property (which represents the last time the file was modified). For example, to delete files older than the threshold date, you can use the following command:

$files | Where-Object { $_.LastWriteTime -lt $thresholdDate } | Remove-Item -Force

This command will delete all files in the specified directory (and subdirectories) that have a LastWriteTime older than the specified threshold date.

Please note that the -Force parameter is used with Remove-Item to suppress any confirmation prompts that PowerShell might display when deleting files. Make sure to double-check the files you are deleting before running this command to avoid accidental data loss.

What is the difference between CreationTime and LastWriteTime in PowerShell?

CreationTime represents the date and time when a file or directory was created, while LastWriteTime represents the date and time when the file or directory was last modified. In other words, CreationTime is the initial timestamp of when the file or directory was created, whereas LastWriteTime is updated every time the file or directory is modified.

What is the cmdlet to check the last write time of a file in PowerShell?

Get-ItemPropertyValue

What is the command to get a list of files older than a certain time period in PowerShell?

To get a list of files older than a certain time period in PowerShell, you can use the Get-ChildItem cmdlet with the -Filter and -Recurse parameters along with the Where-Object cmdlet to filter the results based on the LastWriteTime property of the files.

Here is an example command to get a list of files older than 7 days:

Get-ChildItem -Path C:\Path\To\Directory -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) }

Replace C:\Path\To\Directory with the actual path to the directory where you want to search for files. The command above will list all files older than 7 days in that directory and its subdirectories.