{"id":51673,"date":"2009-12-31T00:01:00","date_gmt":"2009-12-31T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/12\/31\/hey-scripting-guy-how-can-i-create-a-disk-space-report-in-a-word-document\/"},"modified":"2009-12-31T00:01:00","modified_gmt":"2009-12-31T00:01:00","slug":"hey-scripting-guy-how-can-i-create-a-disk-space-report-in-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-create-a-disk-space-report-in-a-word-document\/","title":{"rendered":"Hey, Scripting Guy! How Can I Create a Disk Space Report in a Word Document?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><font size=\"2\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/font><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! We are having problems with several of our servers at work. Several of them were upgraded from Windows NT 4, and as a result, we are constantly running into disk space issues on the C: drive. I would rather manage the problem than take the servers down and rebuild them because we already have budget to replace them next quarter. Therefore, in the meantime, I have to baby-sit 30 servers. <\/p>\n<p class=\"MsoNormal\">This whole thing came to a head last week when one server blue-screened because of no disk space on the C: drive when the page file was unable to grow. Now the pointy-headed boss wants a daily disk space report for the 30 servers. I wrote a quick Windows PowerShell script that will report the disk space for the 30 servers, and gave him the script (hoping in vain that he would run it). But he wants an actual Microsoft Word document report. I found your <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2008\/11\/11\/how-can-i-create-a-microsoft-word-document-from-wmi-information.aspx\"><font face=\"Segoe\">How Can I Create a Microsoft Word Document from WMI Information?<\/font><\/a> article, but it does not really do what I need to accomplish. I would appreciate it if you would help me out. <\/p>\n<p class=\"MsoNormal\">&#8212; CA<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello CA, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I am listening to <a href=\"http:\/\/en.wikipedia.org\/wiki\/UFO_(band)\">UFO<\/a> on my Zune HD, and sipping a nice cup of Earl Grey tea with a cinnamon stick and a splash of milk in it. I am also munching on one of my carefully guarded ANZAC biscuits. Probably because of the cold weather, I have been reviewing some of the pictures I took during my last dive trip to Key Largo, Florida, with my scuba club. The following picture makes me want to grab my gear and dive in.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of a shark\" alt=\"Image of a shark\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1231\/hsg-12-31-09-01.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">CA, writing WMI information to a Microsoft Word document by using a Windows PowerShell script is easier than stepping off a dive boat and swimming with sharks.<\/p>\n<p class=\"MsoNormal\">The first thing we do in the WriteDiskSpaceToWord.ps1 script is assign a few strings to some variables. The <b>Win32_Volume<\/b> WMI class is used to obtain the free disk space from the C: drive. The <b>Win32_Volume<\/b> WMI class was first introduced in Windows Server 2003, and if you have to query Windows XP or earlier computers, you will need to use the <b>Win32_LogicalDisk<\/b> WMI class. The <b>$path<\/b> variable holds the location that will store the complete disk space report. You will need to edit the script to point to a location that is available to your computer, and you can name the report file anything that makes sense to you. The <b>$computers<\/b> variable stores an array of computer names and will need to be modified for your specific network. If you have more than ten computers, you will probably want to store your computer names in a text file and use the <b>Get-Content<\/b> cmdlet to read the file. You could still store the results in the <b>$comptuers<\/b> variable, as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$computers = Get-Content &ndash;path c:fsocomputerlist.txt<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The complete variable initialization section of the WriteDiskSpaceToWord.ps1 script is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$class = &#8220;Win32_Volume&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$path = &#8220;C:fsoVolume.docx&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$computers = &#8220;Win7-pc&#8221;,&#8221;MrEd1&#8243;,&#8221;HyperV&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To save the Microsoft Word document, use an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb238158.aspx\"><font face=\"Segoe\">wdSaveFormat enumeration<\/font><\/a>. The <b>wdSaveFormat<\/b> enumeration is documented on MSDN and is used to determine the type of document that will be saved. <\/p>\n<p class=\"Readeraidonly\">I used the <b>wdSaveFormat<\/b> enumeration to allow me to convert a folder full of Microsoft Word documents into PDF files in a <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2008\/11\/12\/how-can-i-convert-word-files-to-pdf-files.aspx\"><font face=\"Segoe\">recent Hey, Scripting Guy! post<\/font><\/a>. <\/p>\n<p class=\"MsoNormal\">After the <b>WdSaveFormat<\/b> enumeration is created, it is saved in the <b>$SaveFormat<\/b> reference variable. This is seen here. <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>[ref]$SaveFormat = &#8220;microsoft.office.interop.word.WdSaveFormat&#8221; -as [type]<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"Readeraidonly\"><span>For more information about using the <b>[ref]<\/b> type, refer to<\/span> <span><a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2008\/11\/11\/how-can-i-create-a-microsoft-word-document-from-wmi-information.aspx\"><font face=\"Segoe\">this Hey, Scripting Guy! post<\/font><\/a>. To create the new Microsoft Word document, you will need to first create the <b>Word.Application<\/b> object. To do this, use the <b>New-Object<\/b> cmdlet and specify the <b>&ndash;ComObject<\/b> parameter while using the <b>word.application<\/b> program ID. Store the resulting application object in the <b>$word<\/b> variable, as seen here. If you want to see the Word document while the WMI query is writing to it, you can set the <b>visible<\/b> property of the application object to <b>$true<\/b>. After you have the script working on your network, you will probably want to change this value to <b>$false<\/b>. <\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$word = New-Object -ComObject word.application<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$word.visible = $true<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">It is time create a new Microsoft Word document. To do this, use the <b>add<\/b> method from the <b>documents<\/b> collection. Store the resulting document object in the <b>$doc<\/b> variable. You also need to create a <b>selection<\/b> object by using the <b>selection<\/b> property from the <b>application<\/b> object. Store the resultant <b>selection<\/b> object in the <b>$selection<\/b> variable. This section of the script is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$doc = $word.documents.add()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$selection = $word.selection<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To walk through the array of computer names that is stored in the <b>$computers<\/b> variable, use the <b>Foreach<\/b> statement:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Foreach($computer in $Computers)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">{<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Use the <b>TypeText<\/b> method from the <b>selection<\/b> object to write the heading for each computer report. Each computer name is stored in the <b>$computer<\/b> variable. The <b>`r`n<\/b> special character sequence generates a carriage return and a line feed in the Microsoft Word document. The <b>`t<\/b> will tab over the line of text by one tab. This section of the script is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$selection.typeText(&#8220;Disk Space Report for $Computer `r`n `tDisk C gigabytes: &#8220;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Use the <b>Get-WmiObject<\/b> cmdlet to query the <b>Win32_Volume<\/b> WMI class. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dwhawy9k(VS.71).aspx\"><font face=\"Segoe\">.NET format specifier<\/font><\/a> command causes the resulting free disk space to be returned to two decimal places. The <b>GB<\/b> is an administrative constant that converts numbers to gigabytes. The resulting WMI object is piped to the <b>Out-String<\/b> cmdlet so that the results can be displayed as text. This command is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>&#8220;{0:n2}&#8221; -f ((Get-WmiObject -Class Win32_Volume -ComputerName $computer `<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp; <\/span>-Filter &#8220;DriveLetter = &#8216;c:'&#8221;).FreeSpace\/1GB) | <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>Out-String |<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To use a command in the middle of a pipeline, use the <b>ForEach-Object<\/b> cmdlet. This requirement exists even if there is only one item on the pipeline. The <b>typeText<\/b> method is used to write the current information on the pipeline. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb223147.aspx\"><font face=\"Segoe\">typeParagraph method from the selection object<\/font><\/a> writes a blank paragraph. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>ForEach-Object { $selection.typeText($_)}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$selection.TypeParagraph()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>saveas<\/b> method from the <b>document<\/b> object needs the path to the file and a <b>wdSaveFormat<\/b> enumeration value. In this example, the <b>wdFormatDocument<\/b> enumeration value is used, which will save to the default word format. For Microsoft Word 2007, this is a DOCX format file. When you are done, make sure you call the <b>quit<\/b> method from the <b>application<\/b> object to release the word application object from memory:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$doc.saveas($path, $saveFormat::wdFormatDocument)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$word.quit()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When the WriteDiskSpaceToWord.ps1 script runs, the Microsoft Word document shown in the following image is created. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Word document created when script is run\" alt=\"Image of Word document created when script is run\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1231\/hsg-12-31-09-02.jpg\" width=\"600\" height=\"419\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">CA, that is all there is to using Microsoft Word to store information you gathered from WMI. This concludes our Microsoft Word Week articles. Join us tomorrow for Quick-Hits Friday. <span>&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be covering tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/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.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! We are having problems with several of our servers at work. Several of them were upgraded from Windows NT 4, and as a result, we are constantly running into disk space issues on the C: drive. I would rather manage the problem than take the servers down and rebuild them [&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":[216,84,49,3,4,12,45,6],"class_list":["post-51673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-disk-drives-and-volumes","tag-microsoft-word","tag-office","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! We are having problems with several of our servers at work. Several of them were upgraded from Windows NT 4, and as a result, we are constantly running into disk space issues on the C: drive. I would rather manage the problem than take the servers down and rebuild them [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51673","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=51673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51673\/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=51673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}