{"id":15411,"date":"2011-03-05T00:01:00","date_gmt":"2011-03-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/03\/05\/use-powershell-to-explore-disk-utilization-on-your-computer\/"},"modified":"2011-03-05T00:01:00","modified_gmt":"2011-03-05T00:01:00","slug":"use-powershell-to-explore-disk-utilization-on-your-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-explore-disk-utilization-on-your-computer\/","title":{"rendered":"Use PowerShell to Explore Disk Utilization on Your Computer"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to discover what files are using up all the disk space on your computer.<\/p>\n<h3>Weekend Scripter <\/h3>\n<p>Microsoft Scripting Guy, Ed Wilson, here. I do not know about you, but it seems as if there is a disk space-eating gremlin on my computer. One day I have several hundred gigabytes of free disk space on my computer, and the next day I am getting low disk space warnings. If I were downloading and installing stuff all the time on my computer, I could sort of understand it, but I do not do those sort of things. I do take pictures when Dr. Scripto, the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting+wife\/\">Scripting Wife<\/a>, and I head out for a road trip, but I know where those pictures go&mdash;I store them on my storage area network (SAN), so that should not adversely impact the storage space on my computer. <\/p>\n<p>The other day, when I was working on the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/02\/13\/add-code-snippets-to-your-script-by-using-the-powershell-ise.aspx\">code snippet project<\/a>, I happened to notice that my profile was consuming a rather large amount of disk space. I was surprised that this was the case, because I configure Microsoft Outlook to store my .ost file in a data folder for ease of backup. In addition, I have Internet Explorer configured with a rather small temporary cache, and it is also set to delete browsing history on exit. With all of my user data going to a different folder, there should not be hardly anything in my user profile other than a few <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/office.mspx\">Microsoft Office<\/a> <a target=\"_blank\" href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee176977.aspx\">scripting text files<\/a>. <\/p>\n<p><a target=\"_blank\" href=\"http:\/\/en.wikipedia.org\/wiki\/Dude\">Dude<\/a> (or Dudette), how much disk space does a simple .ini file require? In my case, it was over 20 gigabytes of space. If one has a laptop with a 100 gigabyte hard disk drive, that is a huge hit (not to mention the Windows Directory with its 10 gigabyte WinSxs folder. Add in space for virtual memory, Windows XP mode, and perhaps a hiberfile, and soon you are toast. <\/p>\n<p>I decided to write a quick script to explore space-consuming files that are hiding in my profile. Interestingly enough, one reason that I attempt to keep everything in my \\\\data folder is to avoid this exact scenario. In addition to making it easy to access files from the command line, and to backup all my crucial data, it also helps me monitor file usage. Unfortunately, most applications store stuff in my home directory, and if I am not careful, stuff gets dumped and forgotten. <\/p>\n<p>The complete Get-FileSizes.ps1 script appears here. <\/p>\n<blockquote>\n<p>Get-FileSizes.ps1<\/p>\n<p>Get-ChildItem -path $home -ErrorAction silentlycontinue -Recurse | <\/p>\n<p>Sort-Object -Property length -Descending | <\/p>\n<p>Format-Table -autosize -wrap -property `<\/p>\n<p>@{Label=&#8221;Last access&#8221;;Expression={($_.lastwritetime).ToshortDateString()}},<\/p>\n<p>@{label=&#8221;size in megabytes&#8221;;Expression={&#8220;{0:N2}&#8221; -f ($_.Length \/ 1MB)}}, <\/p>\n<p>fullname<\/p>\n<\/blockquote>\n<p>The first thing I do is use the <a target=\"_blank\" href=\"http:\/\/www.bing.com\/visualsearch?g=powershell_cmdlets&amp;FORM=Z9GE22\">Get-ChildItem cmdlet<\/a> to retrieve a recursive listing from my <b>$home<\/b><i> <\/i>location. In Windows XP and <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=10ee29af-7c3a-4057-8367-c9c1dab6e2bf\">Windows Server 2003<\/a>, this location is the user&rsquo;s folder in the <b>Documents and Settings<\/b> directory. Beginning with Windows Vista, the <b>$home<\/b> variable refers to the user&rsquo;s personal folder in the <b>Users<\/b> directory. <\/p>\n<p>Because there are some things, even in your personal profile folder, that you do not have access rights to, I use the <i>SilentlyContinue<\/i> error action to cause the script to continue past any access denied errors. The <i>Recurse<\/i> parameter is required to cause the script to traverse the deeply nested subfolder structure. This line of the script is shown here.<\/p>\n<blockquote>\n<p>Get-ChildItem -path $home -ErrorAction silentlycontinue -Recurse |<\/p>\n<\/blockquote>\n<p>I <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\">pipe<\/a> the resulting <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.fileinfo(VS.80).aspx\">system.io.fileinfo<\/a> objects to the <b>Sort-Object<\/b> cmdlet, and I select the <b>Length<\/b> property from the piped <b>FileInfo<\/b> objects to use to perform a descending sort. This line of code is shown here.<\/p>\n<blockquote>\n<p>Sort-Object -Property length -Descending |<\/p>\n<\/blockquote>\n<p>The last thing I do is create a custom table to display the gathered information. Unfortunately, this is where things begin to get rather complicated. Creating a customized field by using the <b>Format-Table<\/b> cmdlet requires a hash table. The hash table contains two elements: <b>Label<\/b> and <b>Expression<\/b>. <b>Label<\/b> accepts a string that becomes the custom column header, and <b>Expression<\/b> accepts a script block to perform any custom calculations that may be required. <\/p>\n<p>For a great introduction to using the <b>Format-Table<\/b> cmdlet see the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/02\/23\/format-powershell-output-with-an-easy-to-use-table.aspx\">Format PowerShell Output with an Easy-to-Use Table<\/a> Scripting Wife blog. For more information, see all of the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/displaying+output\/scripting+wife\/\">Scripting Wife displaying output articles<\/a> and all of the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting+wife\/\">Scripting Wife beginner articles<\/a>. <\/p>\n<p>In my <b>Format-Table<\/b> command, I use the <i>AutoSize<\/i> parameter, the <i>Wrap<\/i> parameter, and calculate two custom columns. I also use the standard <b>FullName<\/b> property from the <b>FileInfo<\/b> object. This portion of the script is shown here.<\/p>\n<blockquote>\n<p>Format-Table -autosize -wrap -property `<\/p>\n<p>@{Label=&#8221;Last access&#8221;;Expression={($_.lastwritetime).ToshortDateString()}},<\/p>\n<p>@{label=&#8221;size in megabytes&#8221;;Expression={&#8220;{0:N2}&#8221; -f ($_.Length \/ 1MB)}}, <\/p>\n<p>Fullname<\/p>\n<\/blockquote>\n<p>The output from the Get-FileSizes.ps1 script is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4118.hsg-3-5-11-1_432F7C45.jpg\"><img decoding=\"async\" height=\"456\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4812.hsg-3-5-11-1_thumb_65C394F3.jpg\" alt=\"Image of script\" border=\"0\" title=\"Image of script\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>That about wraps it up for today. I think I am going to play around with my Exchange Server 2010 machine for a while. Join me tomorrow for some more Windows PowerShell fun. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to discover what files are using up all the disk space on your computer. Weekend Scripter Microsoft Scripting Guy, Ed Wilson, here. I do not know about you, but it seems as if there is a disk space-eating gremlin on my computer. One day I have several hundred [&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":[25,38,3,4,12,61,45],"class_list":["post-15411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-displaying-output","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to discover what files are using up all the disk space on your computer. Weekend Scripter Microsoft Scripting Guy, Ed Wilson, here. I do not know about you, but it seems as if there is a disk space-eating gremlin on my computer. One day I have several hundred [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15411","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=15411"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15411\/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=15411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}