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.