Я не знаю, что вы подразумеваете под «когда он был удален» ... он был удален при запуске скрипта. [ ухмылка ]
это примет список файлов, проанализирует расширение для даты, сравнит возраст в днях с возрастом отсечения, а затем удалит или не удалится согласно тесту. затем он показывает общее количество и удаленное количество.
# save the old InfoPref
$OldInfoPref = $InformationPreference
# enable Info output
$InformationPreference = 'Continue'
# fake reading in a list of files
# in real life, use Get-ChildItem
$FileList = @(
[System.IO.FileInfo]'One.log.20180101'
[System.IO.FileInfo]'Two.log.20180202'
[System.IO.FileInfo]'Three.log.20180303'
[System.IO.FileInfo]'Four.log.20180404'
[System.IO.FileInfo]'Five.log.20180505'
[System.IO.FileInfo]'Ten.log.20181010'
[System.IO.FileInfo]'Nine.log.20180909'
[System.IO.FileInfo]'Eight.log.20180808'
[System.IO.FileInfo]'Eleven.log.20181111'
)
$MaxDaysOld = 30
$Today = (Get-Date).Date
$DeletedFileList = foreach ($FL_Item in $FileList)
{
$DaysOld = ($Today - [datetime]::ParseExact($FL_Item.Extension.Trim('.'), 'yyyyMMdd', $Null)).Days
if ($DaysOld -gt $MaxDaysOld)
{
Write-Information ('The file named {0} is too old [{1} days].' -f $FL_Item.Name, $DaysOld)
Write-Information ' ----- The file would be deleted -----.'
Write-Information ''
# put your delete command on the next line
# send the deleted file name to the deleted file collection
$FL_Item
}
else
{
Write-Information ('The file named {0} is only {1} days old.' -f $FL_Item.Name, $DaysOld)
Write-Information ' The file would NOT be deleted.'
Write-Information ''
}
}
'=' * 50
'Total file count = {0}' -f $FileList.Count
'Deleted file count = {0}' -f $DeletedFileList.Count
# restore the old InfoPref
$InformationPreference = $OldInfoPref
вывод ...
The file named One.log.20180101 is too old [317 days].
----- The file would be deleted -----.
The file named Two.log.20180202 is too old [285 days].
----- The file would be deleted -----.
The file named Three.log.20180303 is too old [256 days].
----- The file would be deleted -----.
The file named Four.log.20180404 is too old [224 days].
----- The file would be deleted -----.
The file named Five.log.20180505 is too old [193 days].
----- The file would be deleted -----.
The file named Ten.log.20181010 is too old [35 days].
----- The file would be deleted -----.
The file named Nine.log.20180909 is too old [66 days].
----- The file would be deleted -----.
The file named Eight.log.20180808 is too old [98 days].
----- The file would be deleted -----.
The file named Eleven.log.20181111 is only 3 days old.
The file would NOT be deleted.
==================================================
Total file count = 9
Deleted file count = 8