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

This PowerShell tutorial we will discuss, how we can 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 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

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.

>