How To Check If File Modified In Last 24 Hours Using PowerShell?

One of my team members recently wanted to check if a file has been modified in the last 24 hours. I suggest using PowerShell as it provides different methods to check. In this tutorial, I will explain how to check if a file has been modified in the last 24 hours using PowerShell with examples.

I will show you how to check for single and multiple files in a directory here.

Check if a file has been modified in the last 24 hours in PowerShell

Now, let us explore various methods to check if a file has been modified in the last 24 hours in PowerShell.

Method 1: Using Get-Item and LastWriteTime

The best way to check if a file was modified in the last 24 hours in PowerShell is by using the Get-Item cmdlet to retrieve the file properties and then comparing the LastWriteTime property with the current time. Here is the complete PowerShell script:

# Define the path to the file
$filePath = "C:\MyFolder\Resume.pdf"

# Get the file item
$file = Get-Item $filePath

# Check if the file was modified in the last 24 hours
if ($file.LastWriteTime -gt (Get-Date).AddHours(-24)) {
    Write-Host "The file '$filePath' has been modified in the last 24 hours."
} else {
    Write-Host "The file '$filePath' has not been modified in the last 24 hours."
}

Once you run the PowerShell script using Visual Studio Code, you can see the output in the below screenshot.

powershell check if file modified in last 24 hours

I recommend the above method to check if a file has been modified in the last 24 hours using PowerShell.

Check out How to Create a Folder If Not Exists in PowerShell?

Method 2: Using Get-ChildItem for Multiple Files

If you need to check multiple files in a directory if they are files modified in the last 24 hours, you can use the Get-ChildItem cmdlet to retrieve all files and then filter them based on the LastWriteTime property in PowerShell. Here is the complete code:

# Define the path to the file
$directoryPath = "C:\MyFolder"

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

# Check each file to see if it was modified in the last 24 hours
foreach ($file in $files) {
    if ($file.LastWriteTime -gt (Get-Date).AddHours(-24)) {
        Write-Host "The file '$($file.FullName)' has been modified in the last 24 hours."
    }
}

Here, you can see the screenshot below after I ran the code using VS Code.

check if file modified in last 24 hours powershell

Read How to Create a File in PowerShell if it Doesn’t Exist?

Method 3: Using Test-Path and LastWriteTime

If you need to first check if the file exists before checking its modification time, you can combine Test-Path with the LastWriteTime property check. Here is the complete PowerShell script.

# Define the path to the file
$filePath = "C:\MyFolder\Resume.pdf"

# Check if the file exists
if (Test-Path $filePath) {
    # Get the file item
    $file = Get-Item $filePath

    # Check if the file was modified in the last 24 hours
    if ($file.LastWriteTime -gt (Get-Date).AddHours(-24)) {
        Write-Host "The file '$filePath' has been modified in the last 24 hours."
    } else {
        Write-Host "The file '$filePath' has not been modified in the last 24 hours."
    }
} else {
    Write-Host "The file '$filePath' does not exist."
}

Read PowerShell Get-date Add Days Examples

Find Files Created in the Last 24 Hours using PowerShell

Let us see the PowerShell commands to identify and list files within a specific directory that have been created within the past 24-hour period.

To do this, PowerShell provides cmdlets such as Get-ChildItem, which can be used to retrieve a list of files and directories in a specified path, and properties like CreationTime, which indicates when a file or directory was created.

Here’s a basic example of how to use PowerShell to find files that were created in the last 24 hours:

# Define the path to the directory you want to check
$directoryPath = "C:\MyFolder"

# Get the current date and time
$currentDate = Get-Date

# Find and list files in the directory that were created in the last 24 hours
Get-ChildItem -Path $directoryPath | Where-Object { $_.CreationTime -gt $currentDate.AddHours(-24) }
powershell find files created in last 24 hours

This PowerShell script retrieves a list of all files in the specified directory and filters them using the Where-Object cmdlet to only include those whose CreationTime is greater than (more recent than) the current date and time minus 24 hours. The result is a list of files that have been created within the last 24 hours in that directory.

Check out How to Create a Log File using PowerShell?

Find Files Modified in The Last Hour using PowerShell

Now, let me show you how to find files modified in the last hour using PowerShell. You can use the Get-ChildItem cmdlet to list files and directories, and it allows you to access file attributes, including LastWriteTime, which indicates the last time a file was written to or modified.

Here is an example of how you can use PowerShell to find files that were modified in the last hour:

# Define the path to the directory you want to search
$directoryPath = "C:\MyFolder"

# Calculate the time one hour ago from the current time
$oneHourAgo = (Get-Date).AddHours(-1)

# Retrieve and list files modified in the last hour
Get-ChildItem -Path $directoryPath -File | Where-Object { $_.LastWriteTime -gt $oneHourAgo }

This script lists all the files in the specified directory that have a LastWriteTime greater than the time calculated for one hour ago. The -File switch is used with Get-ChildItem to ensure that only files (not directories) are considered. The resulting output will include all files modified within the last hour from the time the script is run.

For the output, check the below screenshot.

powershell find files modified in last hour

Conclusion

In this PowerShell tutorial, I have explained different methods to check if a file has been modified in the last 24 hours using PowerShell. With that, I also explained the below examples:

  • Find Files Created in the Last 24 Hours using PowerShell
  • Find Files Modified in The Last Hour using PowerShell

You may also like the following tutorials:

  • Thanks for these valuable information. I hope next time, you’ll post new commands that would tell us WHO on the network modified the files. I wonder if this is possible even when Audit is not turned on.

  • >