Skip to content

Instantly share code, notes, and snippets.

@DanAtkinson
Last active May 27, 2021 05:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanAtkinson/ed47f7c769c3536ba3557289d517b923 to your computer and use it in GitHub Desktop.
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.
$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
@chrisreeves-
Copy link

Nice script - thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment