{"id":4365,"date":"2013-01-05T00:01:00","date_gmt":"2013-01-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/01\/05\/weekend-scripter-sorting-folders-by-size\/"},"modified":"2013-01-05T00:01:00","modified_gmt":"2013-01-05T00:01:00","slug":"weekend-scripter-sorting-folders-by-size","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-sorting-folders-by-size\/","title":{"rendered":"Weekend Scripter: Sorting Folders by Size"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, uses Windows PowerShell&nbsp;3.0 to sort folders by size.\nMicrosoft Scripting Guy, Ed Wilson, is here. It is amazing how things continue to go in circles &hellip; I know I have written a script to sort folders by size many times in many different languages. I recently ran across <a href=\"http:\/\/jeffwouters.nl\/index.php\/2012\/04\/powershell-script-to-list-folders-and-sizes\/\" target=\"_blank\">a function<\/a> written by <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/11\/22\/the-basics-of-windows-powershell-workflow-when-to-use-it-and-why.aspx\" target=\"_blank\">Jeff Wouters<\/a> that caused me to decide to write this script yet again.\nIt may be partially because, hey, it is the weekend, and on the weekends, I like to play around with Windows PowerShell (my buddy Don Jones used to write a column called <em>PowerShell with a purpose&mdash;<\/em>well, when I am playing around on the weekend, I could call it <em>PowerShell without a purpose <\/em>&hellip; but then I would never purposefully infringe on anyone&rsquo;s title. (OK, so I have now given credit to Jeff and to Don &hellip; so now on with the blog post).<\/p>\n<h2>Use the FileSystem object to find folder sizes<\/h2>\n<p>I know that I knew this &hellip; but hey, I forget stuff after a while. I mean, <a href=\"http:\/\/www.amazon.com\/Ed-Wilson\/e\/B001ILFMZ8\/ref=sr_tc_2_0?qid=1357155643&amp;sr=1-2-ent\" target=\"_blank\">I wrote two books on VBScript (three if you count my WMI book as well<\/a>), so I am certain that I knew this. The thing is that I had forgotten about it until I saw Jeff&rsquo;s script on his blog.\nThe old-fashioned COM object, Scripting.FileSystemObject returns a folder size. With this in mind, I need to use the <strong>New-Object<\/strong> cmdlet with the <strong>&ndash;comobject<\/strong> parameter to create an instance of the filesystemobject, as shown here.<\/p>\n<p style=\"padding-left: 30px\">$fso = New-Object -ComObject scripting.filesystemobject\nThe next thing I need to do is to get a listing of the paths to the folders I am interested in exploring. I do this by using the Windows PowerShell&nbsp;3.0 syntax to return directories only. I am interested in the folders as well as the subfolders, so I use the <strong>&ndash;recurse<\/strong> parameter. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Foreach($folder in (Get-ChildItem $path -Directory -Recurse))\nI create a custom psobject that contains two properties. The first property is the path to the folder (I call it <strong>name<\/strong>) and the second property is the size of the folder in megabytes (stored in a property called <strong>size<\/strong>). The <strong>GetFolder<\/strong><em> <\/em>method from the <strong>FileSystemObject<\/strong> requires a string that is the path to the folder. I use the <strong>FullName<\/strong><em> <\/em>property obtained by the <strong>Get-ChildItem<\/strong> cmdlet and turn it into a string by calling the <strong>ToString<\/strong><em> <\/em>method.\nI then pick up the path itself from the folder object returned by the <strong>GetFolder<\/strong> method. I do essentially the same thing to get the folder size; that is, I pass the <strong>FullName<\/strong> as a string to the <strong>GetFolder<\/strong><em> <\/em>method. I pick the <strong>size<\/strong><em> <\/em>property from the returned <strong>folderobject<\/strong>. But I also turn it into megabytes by using the MB admin constant. The easy way to get whole numbers is to cast the returned size to an integer, and this is what I do with the [int] type accelerator. This code is shown here.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;New-Object -TypeName psobject -Property @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; name=$fso.GetFolder($folder.FullName.tostring()).path;<\/p>\n<p style=\"padding-left: 30px\">&nbsp; size=[int]($fso.GetFolder($folder.FullName.ToString()).size \/1MB)}&nbsp;&nbsp;\nThe last thing I do is sort the folders by size. I also filter the size so that only folders that are larger than 1000 MB return. This code is shown here.<\/p>\n<p style=\"padding-left: 30px\">$folders | sort size -Descending | ? size -gt 1000\nThe entire Get-FolderSizes.PS1 script is shown here.<\/p>\n<p style=\"padding-left: 30px\">#requires -version 3.0<\/p>\n<p style=\"padding-left: 30px\">$path = &#8220;c:data&#8221;<\/p>\n<p style=\"padding-left: 30px\">$fso = New-Object -ComObject scripting.filesystemobject<\/p>\n<p style=\"padding-left: 30px\">$folders = Foreach($folder in (Get-ChildItem $path -Directory -Recurse))<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;New-Object -TypeName psobject -Property @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; name=$fso.getFolder($folder.fullname.tostring()).path;<\/p>\n<p style=\"padding-left: 30px\">&nbsp; size=[int]($fso.GetFolder($folder.FullName.ToString()).size \/1MB)}&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp; }<\/p>\n<p style=\"padding-left: 30px\">$folders | sort size -Descending | ? size -gt 1000\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, uses Windows PowerShell&nbsp;3.0 to sort folders by size. Microsoft Scripting Guy, Ed Wilson, is here. It is amazing how things continue to go in circles &hellip; I know I have written a script to sort folders by size many times in many different languages. I recently ran across a [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[397,3,12,61,45],"class_list":["post-4365","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-directories","tag-scripting-guy","tag-storage","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, uses Windows PowerShell&nbsp;3.0 to sort folders by size. Microsoft Scripting Guy, Ed Wilson, is here. It is amazing how things continue to go in circles &hellip; I know I have written a script to sort folders by size many times in many different languages. I recently ran across a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4365"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4365\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}