This is a very common requirement a lot of clients asked me like how to check if a file was modified in the last 24 hours in PowerShell.
To check if a file has been modified in the last 24 hours using PowerShell, you can use the Get-Item cmdlet to retrieve the file’s properties and compare its LastWriteTime property with the current date and time minus 24 hours. For multiple files, use Get-ChildItem to list files in a directory and filter them with Where-Object to find those modified within the last day. Here’s a compact script example:
$filePath = "C:\MyFolder\file.txt"
if ((Get-Item $filePath).LastWriteTime -gt (Get-Date).AddHours(-24)) {
"File has been modified in the last 24 hours."
}Check if a File Was Modified in the Last 24 Hours in PowerShell
Now, let’s examine various methods for checking whether a file was modified in the last 24 hours in PowerShell.
1. Using the Get-Item Cmdlet
The simplest and easiest way to check if a file was modified in the last 24 hours in PowerShell is by using the Get-Item cmdlet. This cmdlet retrieves the file and its properties, including the last write time, which indicates the last modification time.
Here is the complete script:
$filePath = "C:\MyFolder\Users.txt"
$file = Get-Item $filePath
if ($file.LastWriteTime -gt (Get-Date).AddHours(-24)) {
Write-Host "The file has been modified in the last 24 hours."
} else {
Write-Host "The file has not been modified in the last 24 hours."
}This script sets the $filePath variable to the path of the file you’re checking, retrieves the file’s properties with Get-Item, and then compares the LastWriteTime property to the current date and time minus 24 hours.
You can see in the below screenshot, it is showing me that the Users.txt file has been modified in the last 24 hours. I executed the PowerShell script using VS code.

2. Using the Get-ChildItem Cmdlet
If you want to check how many files have been modified in the last 24 hours in a directory using PowerShell, you can use the Get-ChildItem cmdlet. This cmdlet allows you to list files and directories and filter them based on their properties.
Here’s how you can use Get-ChildItem to find all files modified in the last 24 hours in a specific folder:
Here is the complete PowerShell script.
$directoryPath = "C:\MyFolder"
$files = Get-ChildItem $directoryPath
foreach ($file in $files) {
if ($file.LastWriteTime -gt (Get-Date).AddHours(-24)) {
Write-Host "The file $($file.Name) has been modified in the last 24 hours."
}
}This script sets the $directoryPath variable to the path of the directory you want to check. It then retrieves a list of all files in that directory and iterates over them, checking each file’s LastWriteTime in the same way as the previous example.
3. Advanced Filtering with the Where-Object Cmdlet
Sometimes, you may want to perform more complex filtering. The Where-Object cmdlet can be used alongside Get-ChildItem to filter files based on their modification time.
Here’s a script that finds all files in a directory modified within the last day:
$directoryPath = "C:\MyFolder"
$files = Get-ChildItem $directoryPath | Where-Object { $_.LastWriteTime -gt (Get-Date).AddHours(-24) }
foreach ($file in $files) {
Write-Host "The file $($file.Name) has been modified in the last 24 hours."
}This script filters the files right as they are retrieved by Get-ChildItem, only passing those modified in the last 24 hours to the foreach loop, which then outputs their names.
4. Checking in Large Directories and Performance Considerations
When working with large directories, performance can become an issue in PowerShell. To handle this, you can use the -Filter parameter with Get-ChildItem to narrow down the results before applying additional filtering with Where-Object.
Here’s a modified script that uses -Filter for better performance:
$directoryPath = "C:\MyFolder"
$files = Get-ChildItem $directoryPath -Filter "*.txt" | Where-Object { $_.LastWriteTime -gt (Get-Date).AddHours(-24) }
foreach ($file in $files) {
Write-Host "The file $($file.Name) has been modified in the last 24 hours."
}This script uses -Filter "*.txt" to only retrieve .txt files before checking their modification time. This reduces the number of files that Where-Object needs to process.
Conclusion
Checking if a file has been modified in the last 24 hours with PowerShell is a straightforward task that can be accomplished using various cmdlets like Get-Item, Get-ChildItem, and Where-Object. By combining these cmdlets with conditional logic and other actions, you can create powerful scripts to manage and automate tasks related to file modifications.
Remember to always test your scripts in a controlled environment before deploying them in a production setting to ensure they perform as expected and handle all necessary edge cases. With these PowerShell techniques, you’ll be well-equipped to keep track of file modifications and maintain your systems efficiently.
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.