Powershell Script To Find Last Modified Files

Recently, I had a Windows Server whose E drive was close to filling up.
The problem is that we couldn’t find which files were filling up the drive. It wasn’t the database or any other known program yet there were 80GB unaccounted for.

In an attempt to determine which files have been filling up the drive, I created a powershell script to find the files which have been either modified or created within the last 24 hours:

The default drive being searched is the E drive and the default location of the output file is C:\MODIFIED_FILES_INFO.txt.

You can replace the defaults based on your own prefrences.

#inspect folders and subfolders for file modified during the last 24 hours
#list the files and their last write time
#replace the drive letter with the drive you want to scan
$drive = 'E'

#replace the output file which stores the name and directory of the modified file
$outputfile = 'C:\MODIFIED_FILES_INFO.txt'

$RECYCLE = '$RECYCLE'
$bin = -join($drive, ":\$RECYCLE.BIN")

$folders = Get-ChildItem -Path ($drive + ":\") -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName

foreach($subfolder in $folders){
    $foldername = $subfolder.FullName
     
    if(($foldername -notlike $bin) -and ($foldername -ne ($drive + ":\System Volume Information")) -and ($foldername -notlike ($bin + "\*"))){
        
        $currenttime = get-date
            
        Get-Item $foldername | Foreach {
            $updatetime=$_.LastWriteTime
            
            $files = Get-ChildItem $_.FullName

            foreach($file in $files){
                if($file.LastWriteTimeUtc.Date -gt ($currenttime.Date.AddHours(-24))){
                    Write-Host $file " => " $file.LastWriteTimeUtc.Date
                    $file | out-file $outputfile -append
                    $file.LastWriteTimeUtc | out-file $outputfile -append
                }
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *