Here's my script of the day. I wanted to write a Powershell script which could retrieve the files and folders size for a specified list of volumes. It was important for me to include also empty directories and zero size files, which is not so easy to get with the standard 'Get-ChildItem' method.
The script will return for each volume:
- the name of the subfolder
- the total size of the subfolder
- the number of files in the subfolder (including zero size files)
- the average file size
Here's the code.
I haven't had the time to optimize it, but it does work and does what it says it does.
Feel free to use it if you need it and to leave a comment if you want to suggest an improvement or a correction.
- clear-host
- $volumes = "c:\","e:\","f:\","x:\"
- foreach ($startFolder in $volumes){
- $colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
- foreach ($i in $colItems)
- {
- $subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object -property length -sum)
- $count = @((Get-ChildItem $i.FullName -force -recurse | where {$_.length -ge 0} )).Count
- if($count -eq 0)
- {
- if($subFolderItems.sum -lt 1)
- {
- $avgsize = 0
- $totalsize = 0
- }
- else
- {
- $avgsize = "Not possible"
- $totalsize = "Not possible"
- }
- }
- else
- {
- $avgsize = [math]::round($subFolderItems.sum/$count / 1MB,2)
- $totalsize = [math]::round($subFolderItems.sum / 1MB,2)
- }
- $output_final = $startFolder + ",",$i.FullName + ",",$totalsize + " MB,",$count + ",",$avgsize + " MB"
- $output_final | Out-File -Append 'folder-size-log.csv' -Encoding ASCII
- }
- }
Nice - exactly what I was looking for and does exactly what it says!
ReplyDeleteHow to get information about only directory, not volume?
ReplyDelete