如何在Windows PowerShell脚本中使用创建日期时间早于90天的文件进行计数?

时间:2018-12-14 10:23:02

标签: powershell

我要求从包含多个子文件夹的整个目录中获取比90天更旧的文件。 我尝试了以下脚本,但没有运气。

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

1 个答案:

答案 0 :(得分:0)

这是获取信息的某种不同方式。 [咧嘴]

它使用日期数学来获取以天为单位的年龄并将其与允许的最大值进行比较。然后将其放在自定义对象中,以后可用于获取详细信息。

$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

输出...

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