Last active
May 27, 2021 05:45
-
-
Save DanAtkinson/ed47f7c769c3536ba3557289d517b923 to your computer and use it in GitHub Desktop.
Simple Powershell script to get files older than 30 minutes. Obviously adaptable as needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$fullPath = "D:\path\to\directory" | |
$numDays = 0 | |
$numHours = 0 | |
$numMins = 30 | |
function getOldFiles($path, $maxDays, $maxHours, $maxMins) { | |
$currDate = Get-Date | |
#Get all children in the path where the last write time is greater than 30 minutes. psIsContainer checks whether the object is a folder. | |
$oldFiles = @(Get-ChildItem $path -include *.* -recurse | where {($_.LastWriteTime -lt $currDate.AddDays(-$maxDays).AddHours(-$maxHours).AddMinutes(-$maxMins)) -and ($_.psIsContainer -eq $false)}) | |
if ($oldFiles -ne $NULL) { | |
$oldFileCount = 0 | |
for ($i = 0; $i -lt $oldFiles.Length; $i++) { | |
$oldFileCount += 1 | |
$thisFile = $oldFiles[$i] | |
Write-Host ("This file is old '" + $thisFile.Name + "' - " + $thisFile.LastWriteTime) | |
} | |
Write-Host ("A total of " + $oldFileCount + " old files were found.") | |
} | |
} | |
getOldFiles $fullPath $numDays $numHours $numMins |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice script - thanks! :)