PowerShell find files modified in last 24 hours and PowerShell get last modified time of files in folder

In this PowerShell tutorial, we will discuss, how to find all files modified in the last 24 hours from a folder using PowerShell script. And also we will get the last modified time of files in a folder using PowerShell.

We will see, how can we retrieve files created in the last 3 days from a particular folder using PowerShell.

In my previous post, I have explained the PowerShell variable, PowerShell global variable, and PowerShell reference variable.

PowerShell finds files modified in the last 24 hours

Recently I got one requirement to check what are the files modified in the last 24 hours using PowerShell.

Here I have a folder known as FilesToUpload which contains few files. From that folder, our requirement is to retrieve files modified in the last 24 hours.

PowerShell Script to find files modified in last 24 hours

Here first we are retrieving all the files from the folder using Get-Item.

And in the Foreach loop, we are retrieving the last modified time of each file and then we are calculating the total hours from the current time and last modified time.

-le is to compare for less than or equal in PowerShell.

The $_ variable represents the current object in PowerShell.

We can run the PowerShell using PowerShell ISE where you can debug the PowerShell script also. You can also use Visual Studio code to run and debug PowerShell.

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | Foreach {
$lastupdatetime=$_.LastWriteTime
$nowtime = get-date
if (($nowtime - $lastupdatetime).totalhours -le 24)
{
Write-Host "File modified within 24 hours "$_.Name
}
else
{
Write-Host "File modified before 26 hours"
}
}

Once you run the above PowerShell script, it will find files modified in last 24 hours like below:

PowerShell find files modified in last 24 hours
PowerShell find files modified in last 24 hours

PowerShell Script to get files modified in the last 3 days from a folder

Now, we will see how we can get files modified in the last 3 days from a folder using PowerShell.

Below is the PowerShell script to find files modified in the last 3 days.

Get-ChildItem -Path "E:\Folder1" |
Where-Object { $_.CreationTime -gt (Get-Date).AddDays(-3) } |
Select-Object Fullname

PowerShell get last modified time of files in a folder

Now we will see how we find the last modified time of files inside a folder using PowerShell. Here I have a folder which contains few folders and we will see how we can get last modified time of files in a folder.

Below is the PowerShell command to get last modified time of files in a folder:

We can retrieve all the files from a folder using Get-Item PowerShell cmdlets.

Then we can use the Foreach to iterate all the files from the folder. $_ represents the current object and we can use $_ to retrieve the file name, last modified time like:
$_.Name
$_.LastWriteTime etc.

The PowerShell script we can write inside Windows PowerShell ISE. Also, you can use Visual studio code to write and debug PowerShell scripts.

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | Foreach {"File Name: "+$_.Name +" Last Modified Time: "+$_.LastWriteTime}

Once you execute the above script, you can see the output like below:

PowerShell get last modified time of files in folder
PowerShell get last modified time of files in a folder

If you want instead of file name, if you want to show full file path, then you can modify the script like below:

Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | Foreach {"File Name: "+$_.FullName +" Last Modified Time: "+$_.LastWriteTime}

Once you run the above PowerShell script, you can see the file name with full path like below:

PowerShell cmdlets get last modified file in directory
PowerShell cmdlets get last modified file in a directory

Check if a file created last 24 hours using PowerShell

Let us discuss how to check if one file is created in the last 24 hours using PowerShell. Sometimes you may need to check if the file is created in the last 24 hours.

Below is the PowerShell command which you can write using visual studio code or PowerShell ISE.

$file = 'E:\Desktop\useful.txt'
$createtime = $file.CreationTime
Write-Host $createtime
$nowtime = get-date
$timediff=$nowtime - $createtime
if ($timediff.totalhours -gt 24)
{
Write-Host "The file is created 24 hours back."
}
Else
{
Write-Host "The file is created within 24 hours."
}

Here we are taking a file from E drive and it will check the file creation time and then it will show the difference and it shows if the file is checked in 24 hours or not.

PowerShell find files modified in last N days

Let us check, how to get files modified in last N days using PowerShell. In this particular example, we will discuss how to find files modified in the last 7 days using a PowerShell script.

Here I have a folder, in my local drive which has few files. By using the PowerShell script, we will find out files modified in the last 7 days.

Below is the PowerShell script:

Here we are first calculating, for how many days we want data. We are checking here for the last 7 days.

In PowerShell, by using Get-Item we can retrieve all items from a folder, and then we are writing the where condition and filtering record for the last 7 days.

Then in the Foreach we are getting the file names. In PowerShell $_ represents the current object.

$days_to_check=$(Get-Date).AddDays(-7)
Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where { $_.LastWriteTime -gt $days_to_check } | Foreach {
"File Name: " + $_.Name
}
PowerShell find files modified in last 7 days
PowerShell find files modified in last 7 days

Download in CSV file format:

If you want to download the file in csv format, then you can modify like below:

$days_to_check=$(Get-Date).AddDays(-7)
Get-Item C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\*.* | where { $_.LastWriteTime -gt $days_to_check } | Foreach {
"File Name: " + $_.Name >> C:\Users\Bijaya.Sahoo\Desktop\FilesToUpload\filenames.csv
}

If you run the PowerShell script, it will download the result in csv format.

This is how to find files modified in the last N days in PowerShell.

Read some PowerShell tutorials:

I hope this will be helpful to get files modified in the last 24 hours using PowerShell. Also, we saw how to get last modified time of files or folders using PowerShell.

>