How to find files that have not been updated for some time?

  • 8 April 2024
  • 0 replies
  • 10 views

Userlevel 6
Badge +5

The PowerShell Data Provider in exMon can be used for various things. In this post you will create a test in exMon, using PowerShell, to watch a directory and find all files that have not been modified for the past 31 days.

  1. Create a new Query in exMon Data Governance by right-clicking the Tests folder
  2. In the query window add the following PowerShell script
    # Check for files older than $Days in the folder $TargetFolder
    $Days = "31"
    $TargetFolder = "C:\temp"

    # Create the result DataTable
    $exMonResult= New-Object system.Data.DataTable

    # Create column definition
    $fileName = New-Object system.Data.DataColumn fileName,([string])
    $exMonResult.columns.add($fileName)
    $modifiedDate = New-Object system.Data.DataColumn modifiedDate,([datetime])
    $exMonResult.columns.add($modifiedDate)

    $Now = Get-Date
    $LastWrite = $Now.AddDays(-$days)

    # Find the files
    $Files = Get-ChildItem $TargetFolder | Where { $_.LastWriteTime -le $LastWrite } | sort-object LastWriteTime
    foreach ($File in $Files)
    {
    # Add Row
    $row = $exMonResult.NewRow();
    $row.fileName = $file.name;
    $row.modifiedDate = $file.LastWriteTime;
    $exMonResult.Rows.Add($row);
    }
  3. Change the $TargetFolder variable to your directory
  4. Configure email settings and schedule as normal.

That’s it. Now you have a test that notifies you of all files that have not been modified for the past 31 days.


0 replies

Be the first to reply!

Reply