{"id":54963,"date":"2008-10-30T11:15:00","date_gmt":"2008-10-30T11:15:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/10\/30\/hey-scripting-guy-how-can-i-determine-folder-size\/"},"modified":"2008-10-30T11:15:00","modified_gmt":"2008-10-30T11:15:00","slug":"hey-scripting-guy-how-can-i-determine-folder-size","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-determine-folder-size\/","title":{"rendered":"Hey, Scripting Guy! How Can I Determine Folder Size?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! We are getting ready for a migration, and I am trying to find an easy way to determine the size of the Documents folder. I would like to use Windows PowerShell to do this, but when I use the <B>Get-Item<\/B> cmdlet to retrieve the properties of a folder, I do not see the size. I really do not want to have to iterate through the entire folder, and add up the size of all the files just to get the folder size. Do you have a magic solution you would be willing to share? <BR><BR>&#8211; LM<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hi LM,<\/P>\n<P>I do have a magical solution, which involves eye of newt (not the politician). However, I can&#8217;t share the solution with you because of the <A href=\"http:\/\/en.wikipedia.org\/wiki\/Magic_(illusion)#Secrecy\">Magician&#8217;s Oath<\/A>: <\/P>\n<P>&#8220;As a magician I promise never to reveal the secret of any illusion to a nonmagician, unless that one swears to uphold the Magician&#8217;s Oath in turn. I promise never to perform any illusion for any nonmagician without first practicing the effect until I can perform it well enough to maintain the illusion of magic.&#8221;<\/P>\n<P>So, uh, sorry about that. Remind me to show you the hidden jar of mustard trick sometime. <\/P>\n<P>What&#8217;s that? Oh, right, the script. One truly amazing feat is the way that Windows PowerShell can simplify seemingly mundane tasks. As seen in this image, folder size values are clearly displayed in Windows Explorer: <\/P><IMG height=\"400\" alt=\"Image of folder size values in a property sheet\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1030\/hsg_foldersize1.jpg\" width=\"299\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>For a VBScript version of this task, you may want to refer to <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/mar05\/hey0331.mspx\">this article<\/A>. Though the two scripts are different, they take a similar approach. Here is the&nbsp;script: <\/P><PRE class=\"codeSample\">$DocEnum = 0x5\n$shell = New-Object -ComObject shell.application\n$myDocs = $shell.NameSpace($DocEnum)\n$folderitem = $myDocs.self\n$path = $folderitem.path\n $totalSize = Get-ChildItem -path $path -recurse -errorAction &#8220;SilentlyContinue&#8221; |\n Measure-Object -property length -sum\n IF($totalSIze.Sum -ge 1073741824)\n   {\n      &#8220;{0:n2}&#8221; -f  ($totalSize.sum \/ 1GB) + &#8221; GigaBytes&#8221;\n   }\n ELSEIF($totalSize.sum -ge 1024)\n    {\n      &#8220;{0:n2}&#8221; -f  ($totalSize.sum \/ 1MB) + &#8221; MegaBytes&#8221;\n    }\n ELSE\n    {\n      &#8220;{0:n2}&#8221; -f  ($totalSize.sum \/ 1KB) + &#8221; KiloBytes&#8221;\n    }<\/PRE>\n<P>This script begins with assigning the hexadecimal value of 0x5 to the <B>$docEnum<\/B> variable. The number 0x5 is a magic number, but I will share its meaning with you any way (I am not really a magician, but don&#8217;t pass that around). The number 0x5 is what is called a shell special folder constant. It is documented in MSDN, but I decided to clean up the listing for you, and include the values in Table 1. The cool thing is that you can use any of the hexadecimal values from Table 1 in the script, and it will tell you the size of the folder. You do not even have to use a coded value; you can just include a path to a folder, which I will show you later. Anyway, here is the code that assigns the value 0x5 to the <B>$DocEnum<\/B> variable (you could have figured this one out yourself): <\/P><PRE class=\"codeSample\">$DocEnum = 0x5<\/PRE>\n<P><B>Table 1 Shell Special Folder Values<\/B><\/P>\n<TABLE class=\"dataTable\" id=\"E1D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD>\n<TR class=\"stdHeader\" vAlign=\"top\">\n<TD class=\"\" id=\"colE3D\">Special folder name<\/TD>\n<TD class=\"\" id=\"colEAE\">Hexadecimal value<\/TD><\/TR><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">ALTSTARTUP<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x1d<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">APPDATA<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x1a<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">BITBUCKET<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0xa<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">COMMONALTSTARTUP<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x1e<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">COMMONAPPDATA<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x23<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">COMMONDESKTOPDIR <\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x19<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">COMMONFAVORITES<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x1f<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">COMMONPROGRAMS<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x17<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">COMMONSTARTMENU<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x16<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">COMMONSTARTUP<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x18<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">CONTROLS<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x3<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">COOKIES<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x21<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">DESKTOP<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x0<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">DESKTOPDIRECTORY<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x10<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">DRIVES<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x11<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">FAVORITES<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x6<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">FONTS<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x14<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">HISTORY<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x22<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">INTERNETCACHE<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x20<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">LOCALAPPDATA<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x1c<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">MYPICTURES<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x27<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">NETHOOD<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x13<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">NETWORK<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x12<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">PERSONAL<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x5<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">PRINTERS<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x4<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">PRINTHOOD<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x1b<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">PROFILE<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x28<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">PROGRAMFILES<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x26<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">PROGRAMFILESx86<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x30<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">PROGRAMS<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x2<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">RECENT<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x8<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">SENDTO<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x9<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">STARTMENU<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0xb<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">STARTUP<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x7<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">SYSTEM<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x25<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">SYSTEMx86<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x29<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">TEMPLATES<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x15<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">WINDOWS<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0x24<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We now need to create an instance of the <B>shell.application<\/B> object. Actually, we do not need to do this, but I thought I would do this, because it gives us access to all those special folders in Table 1. We could have used the <B>WshShell<\/B> object as seen&nbsp;here: <\/P><PRE class=\"codeSample\">$wshshell = New-Object -ComObject wscript.shell\n$wshshell.SpecialFolders.Item(&#8220;mydocuments&#8221;)<\/PRE>\n<P>But this method does not provide access to all the folders in Table 1. It only provides access to 18 folders. This is <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/0ea7b5xe.aspx\">documented on MSDN<\/A>.<\/P>\n<P>The <B>shell.application<\/B> object is a COM object, and is created by using the <B>New-Object<\/B> cmdlet. We store the resulting object in the <B>$shell<\/B> variable. We then use the <B>NameSpace<\/B> method from the shell object to create a folder object that references the folder stored in the <B>$DocEnum<\/B> variable. When we have the folder object, we use the <B>self<\/B> property to refer to the specific folder, and store this in the <B>$folderItem<\/B> variable. We now query the <B>path<\/B> property and store it in the <B>$path<\/B> variable. This section of code is seen here. This code is very similar to the way we would have done things in&nbsp;VBScript: <\/P><PRE class=\"codeSample\">$shell = New-Object -ComObject shell.application\n$myDocs = $shell.NameSpace($DocEnum)\n$folderitem = $myDocs.self\n$path = $folderitem.path<\/PRE>\n<P>Now we are ready to use the <B>Get-ChildItem<\/B> cmdlet to get us a listing of all the files and folders in the target path. We use the <B>-recurse<\/B> parameter to allow the command to burrow down through any child folders. The <B>-errorAction<\/B> parameter tells the script to not report any errors from this command. This will suppress any &#8220;Access denied&#8221; errors you may get when targeting folders you might now have permission to: <\/P><PRE class=\"codeSample\">$totalSize = Get-ChildItem -path $path -recurse -errorAction &#8220;SilentlyContinue&#8221;<\/PRE>\n<P>We pipeline the results of the previous <B>Get-ChildItem<\/B> cmdlet to the <B>Measure-Object<\/B> cmdlet and tell it to sum the length of each file. This is shown here: <\/P><PRE class=\"codeSample\">Measure-Object -property length -sum<\/PRE>\n<P>Now we want to translate the results into something easier to read. The result from the previous command returned <B>GenericMeasureInfo<\/B> object which has a <B>sum<\/B> property. We want to translate that value into gigabytes or megabytes, if it makes sense. To do this we use a series of <B>if<\/B> statements. If the sum is bigger than 1,073,741,824, we will translate the value into gigabytes. To do this, we divide the <B>$totalSize.Sum<\/B> value by the 1GB admin constant. We use the <B>-f<\/B> format operator to display only two decimal places. This is seen&nbsp;here: <\/P><PRE class=\"codeSample\">IF($totalSIze.Sum -ge 1073741824)\n   {\n      &#8220;{0:n2}&#8221; -f  ($totalSize.sum \/ 1GB) + &#8221; GigaBytes&#8221;\n   }<\/PRE>\n<P>If the size is bigger than 1,024, we will display the value in megabytes (MB). We use the <B>ELSEIF<\/B> statement to get to this section of thecode: <\/P><PRE class=\"codeSample\">ELSEIF($totalSize.sum -ge 1024)\n    {\n      &#8220;{0:n2}&#8221; -f  ($totalSize.sum \/ 1MB) + &#8221; MegaBytes&#8221;\n    }<\/PRE>\n<P>However, if the size is less than 1,024, we display the value in kilobytes ((KB): <\/P><PRE class=\"codeSample\">ELSE\n    {\n      &#8220;{0:n2}&#8221; -f  ($totalSize.sum \/ 1KB) + &#8221; KiloBytes&#8221;\n    }<\/PRE>\n<P>When we run the script, the folder size is returned:<\/P><IMG height=\"410\" alt=\"Image of the folder size as returned by the script\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1030\/hsg_foldersize2.jpg\" width=\"550\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>So, LM, I hope this investigation into folder size magic does not cause me to lose my magician&#8217;s union membership (Local 42), but I just had to share this trick with you. David Blaine says hi.<\/P>\n<P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><SPAN class=\"Apple-style-span\"><B><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/B><\/SPAN><\/FONT><\/P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><B><\/B><\/FONT><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We are getting ready for a migration, and I am trying to find an easy way to determine the size of the Documents folder. I would like to use Windows PowerShell to do this, but when I use the Get-Item cmdlet to retrieve the properties of a folder, I do not see [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11,3,12,45],"class_list":["post-54963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We are getting ready for a migration, and I am trying to find an easy way to determine the size of the Documents folder. I would like to use Windows PowerShell to do this, but when I use the Get-Item cmdlet to retrieve the properties of a folder, I do not see [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54963","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=54963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54963\/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=54963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}