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

Created by: Kristinn Magnusson

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
  3. # 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);
    }

     

  4. Change the $TargetFolder variable to your directory
  5. 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.

Note: If you require assistance with this, please create a support ticket

Kristinn is the author of this solution article.