The last two lines are inelegant, because the results header isn't the same format as the PSCustomObject. Otherwise you could just export a CSV file using "$List | Export-Csv", but that only keeps the file list.
Code:
$Path = 'C:\Users\GARLIN\Downloads\Multi-boot'
$Days = 180
$CSV_File = 'Report.csv'
$Today = Get-Date
$CutoffDate = $Today.AddDays(-$Days)
$List = @(Get-ChildItem $Path -File -Recurse | Where-Object { $_.LastWriteTime -lt $CutoffDate } | foreach {
[PSCustomObject]@{
Days = (New-TimeSpan -Start $_.LastWriteTime -End $Today).Days
Bytes = $_.Length
Filename = $_.FullName
}
$TotalSize += $_.Length
})
switch ($TotalSize) {
{ $TotalSize -ge 1GB } { $ReportedSize = ('{0} GB' -f ($TotalSize/1GB).tostring("#.##")) }
{ $TotalSize -ge 1MB } { $ReportedSize = ('{0} MB' -f ($TotalSize/1MB).tostring("#.##")) }
{ $TotalSize -ge 1024 } { $ReportedSize = ('{0} KB' -f ($TotalSize/1024).tostring("#.#")) }
default { $ReportedSize = ('{0} bytes' -f $TotalSize) }
}
@("""{0} Files"",""{1} total"",`r`n,," -f $List.Count, $ReportedSize
$List | ConvertTo-Csv -NoTypeInformation) | Set-Content $CSV_File
Code:
"5 Files","7.2 KB total",
,,
"Days","Bytes","Filename"
"725","5857","C:\Users\GARLIN\Downloads\Multi-boot\autounattend.xml"
"725","268","C:\Users\GARLIN\Downloads\Multi-boot\create-MBR.txt"
"725","371","C:\Users\GARLIN\Downloads\Multi-boot\create-UEFI.txt"
"725","659","C:\Users\GARLIN\Downloads\Multi-boot\startnet-ORIG.cmd"
"216","195","C:\Users\GARLIN\Downloads\Multi-boot\unattend.xml"