Venkateshwar Diddi
Venkateshwar Diddi

Reputation: 15

How to get files count using creation date time older then 90 days in Windows PowerShell script?

I have requirement to get files count older then 90days from entire directory which contains multiple sub-folders. I tried below script but no luck.

dir Z:\EDI\ |
    ? {$_.CreationTime -ge (Get-Date).AddDays(-90) -and !$_.PsIsContainer} |
    group {$_.CreationTime.ToShortDateString()} |
    select Name, Count

Upvotes: 1

Views: 2250

Answers (1)

Lee_Dailey
Lee_Dailey

Reputation: 7489

here is a somewhat different way to get the info. [grin]

it uses date math to get the age in days and compares that to the max allowed. then it puts that in a custom object that can be used later for getting details.

$MaxAgeInDays = 90
$Today = (Get-Date).Date
$SourceDir = $env:TEMP

$TooOldFileList = Get-ChildItem -LiteralPath $SourceDir -File -Recurse |
    Where-Object {($Today - $_.CreationTime).Days -ge $MaxAgeInDays} |
    Sort-Object CreationTime |
    ForEach-Object {
        [PSCustomObject]@{
            AgeInDays = ($Today - $_.CreationTime).Days
            FileName = $_.FullName
            }
        }

$OldestFile = $TooOldFileList |
    Select-Object -First 1

''
'Max allowed age in days = {0, 5}' -f $MaxAgeInDays
'Number of too-old files = {0, 5}' -f $TooOldFileList.Count
'Oldest file age in days = {0, 5}' -f $OldestFile.AgeInDays
'Oldest file name        = {0}' -f $OldestFile.FileName

output ...

Max allowed age in days =    90
Number of too-old files =   296
Oldest file age in days =  1136
Oldest file name        = C:\Temp\FXSAPIDebugLogFile.txt

Upvotes: 1

Related Questions