- <#
- .SYNOPSIS
- This script displays the size of a folder
- .DESCRIPTION
- This script is a rewrite of an MSDN sample. It
- uses System.IO namespace to determine the size
- of a folder and its subfolders, then displays it.
- Note that the Get-DirSize function is recursive!
- .NOTES
- File Name : Get-FolderSize.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- This script posted to: http://pshscripts.blogspot.com/2009/10/get-foldersizeps1.html
- MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
-
- .EXAMPLE
- PSH [C:\foo]: .Get-FolderSize c:\foo
- The size of C:\Foo and its subdirectories is 17,577,318 bytes
- #>
-
- param (
- $folder = "C:\Foo"
- )
-
- ##
- # Helper Function Get-DirSize
- ##
- function Get-DirSize {
- param ([system.IO.DirectoryInfo] $d)
- [long] $Size = 0;
-
- # Add file sizes.
- $fis = $d.GetFiles();
- foreach ($fi in $fis){
- $size += $fi.Length;
- }
- # Add subdirectory sizes recursively.
- $dis = $d.GetDirectories()
- foreach ($di in $dis) {
- $Size += Get-DirSize($di)
- }
- return $Size
- }
-
- ## End of Get-DirSize helper function
-
- ##
- # Start of Script
- $d = New-Object system.IO.DirectoryInfo $folder
- $size= Get-Dirsize $d
- "The size of {0} and its subdirectories is {1} bytes" -f $d,$size.ToString("###,###")
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Monday, 5 October 2009
Get-FolderSize.ps1
Subscribe to:
Post Comments (Atom)
8 comments:
Note - declare $Size as long if >2GB of files per folder.
Garth - great catch - sample updated to reflect this!
Is line 31 correct? Currently
n] $Size = 0;
Larry - a typo that must have sneaked in after I created the sample. Thanks for the catch.
Hmm, did that 'correction' remove the [long] declaration for $Size that you added on October 6?
Garth - well caught. Not my day I guess. But thanks for the catch and this is fixed.
Hi Thomas,
How different is the below code from yours? I saw we could do recoursion through inbuilt PS command get-childitem rather than creating a function.
Param ($folder = "C:\test")
Function Get-Files()
{
param ([System.IO.DirectoryInfo] $d)
$files = dir $d -Recurse -Force | ?{$_.attributes -ne "Directory"}
return $files;
}
$xyz = New-Object System.IO.DirectoryInfo $folder
$files = Get-Files $xyz
[long]$size = 0;
foreach ($fi in $files) { $size += $fi.length}
"Total number of files are {0} and total size of the folder is {1} Bytes " -f $files.length, $size.tostring('##,##');
Thanks :-) this helped a lot :-)
I've tweaked it and the following would display the size of all profiles installed on a workstation:
param (
$folder = "C:\Documents and settings"
)
##
function Get-DirSize {
param ([system.IO.DirectoryInfo] $d)
[long] $Size = 0;
# Add file sizes.
$fis = $d.GetFiles();
foreach ($fi in $fis){
$size += $fi.Length;
}
# Add subdirectory sizes recursively.
$dis = $d.GetDirectories()
foreach ($di in $dis) {
$Size += Get-DirSize($di)
}
return $Size
}
# Start of Script
$a = New-Object system.IO.DirectoryInfo $folder
$aa = $a.GetDirectories()
foreach ($ab in $aa) {
$subf = $folder + "\" + $ab.Name
$d = New-Object system.IO.DirectoryInfo $subf
$size= Get-Dirsize $d
"{0} : {1:N2} " -f $d,($size / 1MB) + "MB"
}
Post a Comment