Forum Discussion
matjung
Dec 22, 2022Copper Contributor
Best Practice - how to get file age and size distribution in a folder
Hello Community I am seeking a little help to improve my PS script. It works, but I feel it contains some overhead code and I might head into the wrong direction. For the moment I am not worried a...
matjung
Dec 23, 2022Copper Contributor
Thanks @Joachim group-object helped a lot.
matjung
Dec 23, 2022Copper Contributor
Here is my current Output + Solution
The approach with Expression feels weird.
I will have a closer look into Measure with Script Blocks.
PS G:\formate\PowerShell> . 'g:\formate\PowerShell\ListModifiedDateInFolder.ps1' j:
j: Files 579.00 Size 3’648.17 MB
Name Count Age% Filesize (MB) Volume (%)
---- ----- ---- ------------- ----------
22.09.2022 36 6.22% 222.05 6.09%
23.09.2022 57 9.84% 355.84 9.75%
24.09.2022 94 16.23% 592.00 16.23%
25.09.2022 120 20.73% 754.52 20.68%
26.09.2022 72 12.44% 460.43 12.62%
27.09.2022 121 20.90% 766.22 21.00%
28.09.2022 79 13.64% 497.10 13.63%
param([string]$Path = (Get-Location).Path)
$totalStats = Get-ChildItem -File -Path $Path| Measure-Object -Property length -Sum
"{0} Files {1:N} Size {2:N} MB" -f $Path, $totalStats.Count, ($totalStats.Sum / 1MB)
Get-ChildItem -file -Path $Path |
Select-Object Name,FullName, Length, @{Name="LastModifiedTime"; Expression={$_.LastWriteTime.ToString("dd.MM.yyyy")}} |
group-object -Property LastModifiedTime |
Select-Object Name,
Count,
@{Name="Age%"; Expression= { ($_.Count / $totalStats.Count).ToString("P")}},
@{Name="Filesize (MB)"; Expression = { (($_.Group | Measure-Object -Property Length -Sum).Sum) / 1MB}},
@{Name="Volume (%)"; Expression = { ((($_.Group | Measure-Object -Property Length -Sum).Sum) / $totalStats.Sum).ToString("P") }}
| Sort Name | Format-Table