How to check if file created last 24 hours using PowerShell?

In this PowerShell tutorial, we will 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.

Check if a file created last 24 hours using PowerShell

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.

Read some PowerShell tutorials:

I hope this will be helpful to check if file created last 24 hours using PowerShell.

  • >