Get Folder Size PowerShell Script

Posted: 02/05/2011 in File Server Administration, PowerShell

One of my clients was experiencing slow logon times after enabling roaming profiles and this was tracked down to large profiles. I needed to write a quick script that would tell me the folder size across a bunch of file servers.

I decided to create a function for this task named Get-FolderSize and this is the code:

function Get-FolderSize {
param ( [Parameter(Mandatory=$true)] [System.String]${Path}	)

$objFSO = New-Object -com  Scripting.FileSystemObject
$folders = (dir $path | ? {$_.PSIsContainer -eq $True})
foreach ($folder in $folders)
    {
    $folder | Add-Member -MemberType NoteProperty -Name "SizeMB" -Value (($objFSO.GetFolder($folder.FullName).Size) / 1MB) -PassThru
    }

}
Get-FolderSize $args[0]

To use this code on a local machine simply execute the above function and call it, for example Get-FolderSize D:\FileData. Now as previously mentioned I wanted to be able to check a large bunch of File Servers and to do that I would first need to create a variable to hold my servers, for example $allServers, then save the above code to the executing server for example C:\Scripts\Get-FolderSize.ps1. This finally allowed me to run the following command:

$folders = icm $allServers -FilePath C:\Scripts\Get-FolderSize.ps1 -ArgumentList "E:\Profiles"
 

Now to see the top 10 worst offenders with a filter to show only two decimal places:

 $folders | sort -Property SizeMB -Descending | select fullname,@{n='SizeMBN2';e={"{0:N2}" -f $_.SizeMB}} | select -First 10
 

Well as you can see from the below screen my client has some profiles to clean up but at least now they know which ones.

Thanks for reading and I hope you find this useful.

Regards,

jfrmilner

Advertisements
Comments
  1. […] https://jfrmilner.wordpress.com/2011/05/02/get-folder-size-powershell-script/ LD_AddCustomAttr("AdOpt", "1"); LD_AddCustomAttr("Origin", "other"); LD_AddCustomAttr("theme_bg", "ffffff"); LD_AddCustomAttr("theme_text", "000000"); LD_AddCustomAttr("theme_link", "b54141"); LD_AddCustomAttr("theme_border", "eeeeee"); LD_AddCustomAttr("theme_url", "b54141"); LD_AddCustomAttr("LangId", "1"); LD_AddCustomAttr("Autotag", "technology"); LD_AddCustomAttr("Tag", "ps-or-cmd"); LD_AddSlot("wpcom_below_post"); LD_GetBids(); Rate this: Share this:FacebookTwitterLike this:LikeBe the first to like this post. Leave a Comment […]

  2. JP says:

    Hey, thanks for posting this. How would you list only those folders where the size is greater than XXXX MB?

  3. Mayur says:

    thx a lot…i have similar requirement….i have many roaming profiles on one the dfs share…can i get list folder utilizing above 300mb? off all

  4. Mayur says:

    thx a lot Jfrmiler i need your more help since i am new to scripting….we have user profiles on netapp filers which we access it from \\domainname\dfs-emea\ldn001\profile name… i want to get all details of folders on 001 with most used size at top 10….in csv output can you help me on that pl.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s