Monitor file age using Powershell

Mike R 81 Reputation points
2021-12-31T11:37:39.033+00:00

Hello,

I would like to monitor a file age using PowerShell. If the file is more than 24 hours old then it will send an email to the relevant users. I was just wondering what's the best PS method to monitor the file age?

Thanks

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.5K Reputation points MVP Volunteer Moderator
    2021-12-31T11:54:42.707+00:00

    Hi @Mike R ,

    maybe this helps to get the file age:

    $fileObj = Get-Item -Path C:\Junk\testfile.txt  
    # Creation Date  
    if (($fileObj.CreationTime) -lt (Get-Date).AddHours(-24)) {Write-Output "Old file"}  
    else {Write-Output "New file"}  
    # Last Modified Date  
    if (($fileObj.LastWriteTime) -lt (Get-Date).AddHours(-24)) {Write-Output "Old file"}  
    else {Write-Output "New file"}  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.