How To Monitor File Age Using PowerShell?

What is PowerShell Scripting used for?

PowerShell is mostly used for automating the management of systems. Besides, automation management is used to build, test, and deploy solutions, usually in CI/CD environments. PowerShell is built on the .NET CLR (Common Language Runtime). All inputs and outputs are .NET objects, and no need to parse text output to extract information from the output. You can find more about PowerShell from Microsoft Docs – What is PowerShell?

Sometimes you need to monitor file age using PowerShell. How can you do this with a PowerShell script?

Example: Suppose Exchange Admin like to monitor a file age using PowerShell. If the file is more than 48 hours old, it will send an email to the relevant users. 

For this, the best method is do with PowerShell script to perform this task.

Solution

Below I have added a script that always works for me. You can also find some other scripts to accomplish this task.

1.	 $fileObj = Get-Item -Path C:\Junk\testfile.txt
2.	 # Creation Date
3.	 if (($fileObj.CreationTime) -lt (Get-Date).AddHours(-48)) {Write-Output "Old file"}
4.	 else {Write-Output "New file"}
5.	 # Last Modified Date
6.	 if (($fileObj.LastWriteTime) -lt (Get-Date).AddHours(-48)) {Write-Output "Old file"}
7.	 else {Write-Output "New file"}

I hope this script solves your Monitor file Age Using PowerShell problem.

Amit Singh

Amit Singh

I am a dynamic Tech Enthusiast, specializing in various Microsoft technologies, including Office 365, Exchange, PowerShell, and Active Directory. I loved to helped customers worldwide design, implement, migrate, and secure Exchange and Office 365 through various roles for different employers, and more recently, as an independent consultant.