{"id":16161,"date":"2010-12-20T00:01:00","date_gmt":"2010-12-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/12\/20\/a-powershell-carol-in-which-ebenezer-script-learns-wmi\/"},"modified":"2010-12-20T00:01:00","modified_gmt":"2010-12-20T00:01:00","slug":"a-powershell-carol-in-which-ebenezer-script-learns-wmi","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/a-powershell-carol-in-which-ebenezer-script-learns-wmi\/","title":{"rendered":"A PowerShell Carol in which Ebenezer Script Learns WMI"},"content":{"rendered":"<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\"><strong>Summary:<\/strong> A Windows PowerShell Carol in which Ebenezer Script learns how easy it to use WMI to check disk space usage.<\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\"><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hey, Scripting Guy! I am wondering if Ebenezer Script will learn how easy it is to work with WMI from inside Windows PowerShell.<\/span>\n<span style=\"font-size:10.0pt\">&#8212; CS<\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\"><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hello CS, Microsoft Scripting Guy Ed Wilson here. It is almost the end of the year and we have decided to devote some posts to the holiday season. We even have guest bloggers from around the world to share some holiday spirit. This week we have Sean Kearney. You can learn more about Sean in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/12\/19\/weekend-scripter-holiday-powershell-carol-part-one.aspx\"><span>yesterday&rsquo;s blog introduction<\/span><\/a>. Be sure to check out <a href=\"https:\/\/www.youtube.com\/watch?v=v04BXhlPxyY\">Sean&rsquo;s theme song for this week&rsquo;s PowerShell Carol posts<\/a>.<\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\">And so it was on that cold winter evening, Ebenezer Script headed home to his hovel whilst hauling his old 286 with CGA monitor in a rusty Red Flyer wagon to his house.<\/span>\n<span style=\"font-size:10.0pt\">We did mention Ebenezer Script was not fond of anything new. Even a suitcase computer was sickening to his eyes.<\/span>\n<span style=\"font-size:10.0pt\">He trotted along up the stairs of his abode. He stared at the doorknocker. It shimmered.<\/span>\n<span style=\"font-size:10.0pt\">His eyes blinked as he reached for the doorknocker and it took on the shape of his former assistant Jacob Clippy. Big round eyes blinked away at him.<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size:10.0pt\">&ldquo;Don&rsquo;t run with scissors,&rdquo; he cheerfully suggested and shimmered away into nothingness.<\/span>\n<span style=\"font-size:10.0pt\">Old Ebenezer shook his head. Was he losing his mind?<\/span>\n<span style=\"font-size:10.0pt\">He opened the door up with the old ratty skeleton key, stepping into the hall. Staring at his floor littered with CP\/M 2.2 8&rdquo; floppies. He let out a sigh shaking his head. He grabbed a bottle from the shelf and poured himself a strong stiff drink for this warm night, a hot can of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Soft_drink\">soda pop<\/a>.<\/span>\n<span style=\"font-size:10.0pt\">(*BLEeAaaaCCccchHhHH!!*)<\/span>\n<span style=\"font-size:10.0pt\">Ebenezer&rsquo;s body responded cheerfully to his always excellent choice in pre-nighttime drinks. The boiled syrup reacted equally well with his lunch of &ldquo;Mr. Noodles.&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">(*Urrrrrrggggllllll*) <\/span>\n<span style=\"font-size:10.0pt\">His old tummy rumbled. Ebenezer&rsquo;s habits came from a long time ago when he was once happy.<\/span>\n<span style=\"font-size:10.0pt\">Yes, there was a day when old Ebenezer Script was the best of the best, and could out manage anybody with VBScript and run circles with Perl. He was the king with his old Office Assistant and sometimes business partner, the ever-irritating Jacob Clippy. Happy were they coding and scripting, scripting and coding; and sometimes dancing, especially with the pretty data entry clerks.<\/span>\n<span style=\"font-size:10.0pt\">But, he was always happy.<\/span>\n<span style=\"font-size:10.0pt\">Nevertheless, those days were gone. Off to bed Ebenezer went. The thought about &ldquo;Whatever happe&hellip;.&rdquo; quickly floated away. He was a miserable grouchy, grimy grump. Nothing could ever be perceived to change that.<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;Bah! Codebug!&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">Ebenezer turned on his old computer and fired up his script to check free drive space. He looked at once to remind himself about the hard work he had put in to create this work of art.<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Dim Result<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">&nbsp;<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Set ConnectoWMI = GetObject(&#8220;winmgmts:\\localhostrootcimv2&#8221;)<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Set Disks = objWMIService.ExecQuery(&#8220;Select * from Win32_LogicalDisk Where DriveType = 3&#8221;) <\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">For each Drive in Disks<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Result = &#8220;Drive &#8221; &amp; Drive.DeviceID &amp; &#8221; &#8211; Free Space &#8221; &amp; Drive.FreeSpace<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>wscript.echo Result<\/span><\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Next<\/span><\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\">He ran the script and proudly noted plenty of free space on his old computer. <\/span>\n<span style=\"font-size:10.0pt\">&ldquo;Good thing I ran DriveSpace on it,&rdquo; he chortled to himself.<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;Let&rsquo;s see Windows PowerShell do anything like that,&rdquo; cackled old Ebenezer sitting back on his haunches. <\/span>\n<span style=\"font-size:10.0pt\">He reached into his pocket to pull out his keys to lock the computer when he found a scrap of paper. Glancing down, he realized that it was a piece of paper he had grabbed off Bob Snipit&rsquo;s desk to write down a telephone number.<\/span>\n<span style=\"font-size:10.0pt\">He turned it over and noted it was a script Bob had written.<\/span>\n<b><i><span style=\"font-size:10.0pt\">Free Drive Space in Windows PowerShell &ndash; One liner<\/span><\/i><\/b><span style=\"font-size:10.0pt\">, it was titled.<\/span><\/p>\n<p class=\"CodeBlock\" style=\"margin-left:.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">GET-WMIOBJECT win32_logicaldisk | where { $_.DriveType -eq 3} | format-list<\/span><\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;BAH! There is no way it is that easy. What a fool Bob is! Codebug! Baaaaaaah!&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">It was at that time that a foul breeze blew out of the power supply of his old 286. Capacitors popped and the room filled with smoke.<\/span>\n<span style=\"font-size:10.0pt\">Coughing and hacking, Ebenezer looked up to put out the smoke only to see a shape.<\/span>\n<span style=\"font-size:10.0pt\">&ldquo;AIGHAIHGHAGHH!!!!&rdquo; Ebenezer Script leapt out of his old socks, &ldquo;It can&rsquo;t be you!&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">Sure enough, it was his old assistant \/ partner \/ iconic personality, Jacob Clippy. Thin as always with that same slick smug smile.<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size:10.0pt\">&ldquo;Well Ebenezer,&rdquo; the giant Bent Blinking Behemoth looked on, &ldquo;have you missed me?&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">*KLUNK!*, the old Admin dropped dead in a faint.<\/span>\n<span style=\"font-size:10.0pt\">Will Ebenezer recover from his fainting spell? Is Jacob Clippy compatible with Windows PowerShell? Will he irritate the readers endlessly with inane suggestions? Will we get hot <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hot_chocolate\">hot chocolate<\/a> after reading this? This and many more questions (some pointlessly odd) answered next time!<\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<span style=\"font-size:10.0pt\">CS, that is all there is to using Windows PowerShell and WMI. Holiday guest blogger week will continue tomorrow when Sean will continue &ldquo;A Windows PowerShell Carol.&rdquo;<\/span>\n<span style=\"font-size:10.0pt\">I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a target=\"_blank\" href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/span>\n<span style=\"font-size:10.0pt\">&nbsp;<\/span>\n<b><span style=\"font-size:10.0pt\">Ed Wilson, Microsoft Scripting Guy<\/span><\/b><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp; Summary: A Windows PowerShell Carol in which Ebenezer Script learns how easy it to use WMI to check disk space usage. &nbsp; Hey, Scripting Guy! I am wondering if Ebenezer Script will learn how easy it is to work with WMI from inside Windows PowerShell. &#8212; CS &nbsp; Hello CS, Microsoft Scripting Guy Ed [&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,56,3,4,154,12,100,6],"class_list":["post-16161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-disk-drives-and-volumes","tag-guest-blogger","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney","tag-storage","tag-windows-powershell-ise","tag-wmi"],"acf":[],"blog_post_summary":"<p>&nbsp;&nbsp; Summary: A Windows PowerShell Carol in which Ebenezer Script learns how easy it to use WMI to check disk space usage. &nbsp; Hey, Scripting Guy! I am wondering if Ebenezer Script will learn how easy it is to work with WMI from inside Windows PowerShell. &#8212; CS &nbsp; Hello CS, Microsoft Scripting Guy Ed [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16161","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=16161"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16161\/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=16161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}