Skip to content

Instantly share code, notes, and snippets.

@azam
Created September 7, 2023 21:25
Show Gist options
  • Save azam/62d16988699af4a22db5dae627a09ea9 to your computer and use it in GitHub Desktop.
Save azam/62d16988699af4a22db5dae627a09ea9 to your computer and use it in GitHub Desktop.
Powershell function to print recent files recursively, defaults to last 1 day
function Print-RecentFiles {
Param (
[Parameter()][string]$Path,
[Parameter()][switch]$NoRecurse,
[Parameter()][int]$Days = 1,
[Parameter()][int]$Hours = 0,
[Parameter()][int]$Minutes = 0,
[Parameter()][int]$Seconds = 0
)
if([string]::IsNullOrWhiteSpace($Path)){
$Path = $PWD
}
$Recurse = ! $NoRecurse.IsPresent
$Info = (Get-Item $Path)
$From = Get-Date
$From = $From.AddDays(-$Days)
$From = $From.AddHours(-$Hours)
$From = $From.AddMinutes(-$Minutes)
$From = $From.AddSeconds(-$Seconds)
if ($Info -is [System.IO.DirectoryInfo]) {
$Items = (Get-ChildItem -File -Path $Path -Recurse:$Recurse -Include * | Where-Object {$_.LastWriteTime -gt $From})
foreach ($Item in $Items) {
Write-Host $Item.LastWriteTime $Item
}
} elseif ($Info -is [System.IO.FileInfo]) {
if ($Info.LastWriteTime -gt $From) {
Write-Host $Info.LastWriteTime $Info
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment