Skip to content

Instantly share code, notes, and snippets.

@markwragg
Last active October 5, 2016 13:02
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 markwragg/694cb79513c947a9a1cab0faf3ddf04f to your computer and use it in GitHub Desktop.
Save markwragg/694cb79513c947a9a1cab0faf3ddf04f to your computer and use it in GitHub Desktop.
Powershell script to get the total size of a list of folders and the last modified date of the newest file in each folder. The script takes a CSV input file which needs to be a list of paths with a header of "path".
[CmdletBinding()]
param (
$Paths = (Import-CSV "Paths.csv")
)
$i = 0
$Total = 0
$Paths | ForEach-Object {
Write-Progress -Activity "Getting folder size for $($_.Path)" -PercentComplete (($i / $Paths.Count) *100) -Status ("Total size -- " + "{0:N2}" -f $Total + "MB")
$subFolderItems = (Get-ChildItem $_.path -Recurse)
$FolderSize = ($subFolderItems | Measure-Object -property length -sum | Select Sum -ExpandProperty Sum) / 1MB
$NewestFile = $subFolderItems | sort LastWriteTime | select -last 1
$Result = @{
Path = $_.path
SizeMB = $FolderSize
LastWritten = $NewestFile.LastWriteTime
}
New-Object PSObject -Property $Result
Write-Verbose ("$($_.path) -- " + "{0:N2}" -f $FolderSize + "MB")
$Total += $FolderSize
$i++
} | Export-CSV "Paths-Sizes.csv"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment