Skip to content

Instantly share code, notes, and snippets.

@Boggin
Created March 9, 2021 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Boggin/a62f328a722ccb32c18ca2e5ca21f121 to your computer and use it in GitHub Desktop.
Save Boggin/a62f328a722ccb32c18ca2e5ca21f121 to your computer and use it in GitHub Desktop.
Get the monthly average folder size
<#
Given the list of folders and the dates over which to check then this script
will group the files by month for each folder between the given dates and get
the sum of the file sizes. So you will get a calculated property of 'folder',
'month', 'size' (where file size is in MB to two decimal places). Then that
calculated property is grouped by 'folder' and the cumulative file size over the
selected months is averaged. So you will have a calculated property of 'folder',
'monthly' (where 'monthly' is a monthly average in MB).
#>
$archives = @{
FileLocation = '\\server\archive'
ArchiveLocation = '\\another\archive'
}# Uses the last three complete months.
$start = Get-Date -Year 2020 -Month 12 -Day 01
$end = Get-Date -Year 2021 -Month 02 -Day 28
$archives.Values `
| ForEach-Object { `
$folder = $_;
Get-ChildItem $_ `
| Where-Object { !$_.PSIsContainer } `
| Where-Object { $_.LastWriteTime -le $end -and $_.LastWriteTime -ge $start } `
| Group-Object { $_.LastWriteTime.Date.Month } `
| Select-Object `
@{ n = 'Size'; e = { "{0:N2}" -f (($_.Group | Measure-Object Length -Sum).Sum / 1mb) } }, `
@{ n = 'Month'; e = { $_ | Select-Object -Expand Name } }, `
@{ n = 'Folder'; e = { $folder } } } `
| Group-Object Folder `
| Select-Object `
@{ n = 'Monthly'; e = { "{0:N2}" -f (($_.Group | Measure-Object Size -Average).Average) } }, `
@{ n = 'Folder'; e = { $_.Group | Select-Object -Expand Folder -First 1 } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment