{"id":65123,"date":"2007-04-09T21:01:00","date_gmt":"2007-04-09T21:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/09\/how-can-i-tell-whether-a-laptop-computer-is-running-off-batteries\/"},"modified":"2007-04-09T21:01:00","modified_gmt":"2007-04-09T21:01:00","slug":"how-can-i-tell-whether-a-laptop-computer-is-running-off-batteries","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-whether-a-laptop-computer-is-running-off-batteries\/","title":{"rendered":"How Can I Tell Whether a Laptop Computer is Running Off Batteries?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! How can I tell whether a laptop computer is running off batteries?<BR><BR>&#8212; TJ<\/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\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, TJ. Ah, yes: batteries. Back in December the Scripting House lost power for six days. Was the Scripting Guy who writes this column concerned? Of course not. After all, he was fully prepared for an emergency like that: he had four (count \u2018em: four) flashlights, each one fully-stocked with batteries. Granted, he had no way to heat the house, and after a day without power all the food (or at least the food in the refrigerator) had to be thrown out. But he <I>did<\/I> have four flashlights, fully-stocked with batteries.<\/P>\n<P>About the only thing he <I>wasn\u2019t<\/I> prepared for was the fact that batteries don\u2019t maintain their charge forever. (Hey, who knew?) Within a day two of his four flashlights had already died; shortly thereafter the other two began to sputter and cough. At that point the Scripting Guy who writes this column sat down on the floor, disassembled all the flashlights, and started mixing and matching batteries in the hope of finding some combination that would enable at least <I>one<\/I> of the flashlights to continue working.<\/P>\n<P>What\u2019s that? Just walk down to the store and buy some <I>new<\/I> batteries? Actually the Scripting Family did walk down to the store one day, mainly because there wasn\u2019t anything to do at home. Although most businesses were closed (they didn\u2019t have power, either) one of the grocery stores was sort of open; as you entered the store they gave you a lantern so you could make your way through the darkened aisles, just in case someone overlooked a box of crackers or something. The Scripting Guy who writes this column wandered into the store just to see if they happened to have any batteries. Not only was every single battery of every single size long-since gone, but the battery area looked like a war zone: apparently people had gone into the store, torn the batteries from the package, and then (we assume) took them without even paying for them. <\/P>\n<P>But who can blame them? After all, two days without a cappuccino will drive anyone insane.<\/P>\n<P>At any rate, trying to find some combination of batteries that would make a flashlight work was an interesting exercise, an exercise very similar to your question, TJ: trying to determine whether a laptop computer is running off of batteries. As it turns out, there are millions of WMI classes in the cimv2 namespace that <I>say<\/I> they will return information about batteries; however, those classes proved to be a bit disappointing, to say the least. For example, on the Scripting Test Machine the Win32_PortableBattery class returned no information whatsoever. By contrast, the Win32_Battery class <I>did<\/I> return information \u2026 sort of. To note two instances, the BatteryStatus and Availability classes seemed to randomly return one of two values: Unknown or Other.<\/P>\n<TABLE class=\"dataTable\" id=\"EVD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. What\u2019s the difference between Unknown and Other? Good question. As near as we can tell, Unknown means, \u201cWe have no idea what the battery status is.\u201d By contrast, Other apparently means, \u201cWe know what the battery status is. But we aren\u2019t going to tell <I>you<\/I>.\u201d Either way the information wasn\u2019t particularly helpful.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>It was at this point that the Scripting Guy who writes this column started poking through the <B>root\\wmi<\/B> namespace. Poking through the root\\wmi namespace is a lot like going to a second-hand store: most of what you find is of no use to you whatsoever. (The majority of classes stashed in root\\wmi are valid classes, but they don\u2019t return any data.) Every now and then, however, you stumble upon something a little more valuable. At a second-hand store this might be a long-lost copy of the <A href=\"http:\/\/www.msnbc.msn.com\/id\/17761718\/\" target=\"_blank\"><B>Declaration of Independence<\/B><\/A>; in the root\\wmi namespace, this might be the <B>BatteryStatus<\/B> class:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\wmi&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * From BatteryStatus Where Voltage &gt; 0&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo &#8220;Battery: &#8221; &amp; objItem.InstanceName\n    Wscript.Echo &#8220;On AC Power: &#8221; &amp; objItem.PowerOnline\n    Wscript.Echo &#8220;Battery is Discharging: &#8221; &amp; objItem.Discharging\n    Wscript.Echo &#8220;Battery is Charging: &#8221; &amp; objItem.Charging\n    Wscript.Echo &#8220;Remaining capacity: &#8221; &amp; objItem.RemainingCapacity\nNext\n<\/PRE>\n<P>One caveat before we explain how this script works. As far as we know this particular class has never been documented; we searched everywhere for additional information on the BatteryStatus class and couldn\u2019t find much of anything. However, as near as we can tell the script works just fine on laptops running either Windows XP or Windows Vista, so, with any luck, this will get you started, TJ.<\/P>\n<TABLE class=\"dataTable\" id=\"ETE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Does the script run on other versions of Windows? To tell you the truth, we don\u2019t know; that\u2019s because we don\u2019t have any laptops running, say, Windows 2000 or Windows Server 2003.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As for the script itself, we begin by binding to the WMI service on the local computer. Fortunately this script also works remotely; after all, if you want to know if the local computer is running off batteries it\u2019s probably quicker and easier just to see whether or not the power cord is plugged into the wall. So then how <I>do<\/I> you modify this script to run against a remote machine? That\u2019s easy; just assign the name of the remote computer (e.g., atl-ws-01) to the variable strComputer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;\n<\/PRE>\n<P>After making the connection (note that we bind to the <B>root\\wmi<\/B> namespace) we then use this line of code to retrieve information about all the batteries with a <B>Voltage<\/B> greater than 0:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery(&#8220;Select * From BatteryStatus Where Voltage &gt; 0&#8221;)\n<\/PRE>\n<P>Why did we add the stipulation about batteries with a voltage greater than 0? Well, to tell you the truth, we aren\u2019t sure if that\u2019s required or not. In our case the Scripting Test Machine has two separate battery bays; being a cheapskate, however, the Scripting Guy who writes this column only has one battery installed in the computer. Nevertheless the empty bay gets reported back as a battery, albeit a battery with a voltage of 0. That seemed silly to us, so we added the Where clause to filter out non-existent batteries.<\/P>\n<TABLE class=\"dataTable\" id=\"EOF\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, when trying to determine which flashlight batteries worked and which ones didn\u2019t the Scripting Guy who writes this column should have done something similar, carefully setting aside batteries that he knew did <I>not<\/I> work. The one thing he <I>shouldn\u2019t<\/I> have done is test a battery and then simply toss it back on the pile, mixing it in with the batteries that he hadn\u2019t tested. So is this what he did? Well, you know, that was a long time ago, and it\u2019s hard to remember what he did or didn\u2019t do \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After executing our query we then set up a For Each loop to report back information for each battery in the collection. We chose to echo back values for the following properties:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>InstanceName<\/B>. Identifying information for the battery.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>PowerOnline<\/B>. The key to this entire script. If PowerOnline is True that means that the AC adapter is plugged in and the computer is not running off the battery. If PowerOnline is False that means that the computer is running off the battery.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Discharging<\/B>. Indicates whether the battery is in use and therefore discharging.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>Charging<\/B>. Indicates whether the battery is currently charging.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>RemainingCapacity<\/B>. Lets you know the estimated battery life. We\u2019re not 100% sure what this number means. We found some information that suggests the value is reported back as milliwatt-hours, but we don\u2019t know that for sure; for that matter, we don\u2019t even know what a millwatt-hour is. However, we do know this: on our test computer the Windows Power Meter would report an estimated battery life like 2 hours and 54 minutes (that is, 174 minutes). Meanwhile, the value of the RemainingCapacity property would come back as 43734. If we divided 43734 by 174 minutes, we got back a value of 252. For us 252 seemed to be the magic number: any time we divided RemainingCapacity by 252 we got back a value, in minutes, that matched up quite nicely with the estimated battery life reported back by the Power Meter. Again, we can\u2019t guarantee that 252 is the magic number but, again, it gives you a place to start.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>So what happens when we unplug the test machine (causing it to run off the battery) and then fire up the script? Why, we get back information similar to this:<\/P><PRE class=\"codeSample\">Battery: ACPI\\PNP0C0A\\1_0\nOn AC Power: False\nBattery is Discharging: True\nBattery is Charging: False\nRemaining capacity: 41769\n<\/PRE>\n<P>And if we then plug the computer in and try the script again we get back information similar to <I>this<\/I>:<\/P><PRE class=\"codeSample\">Battery: ACPI\\PNP0C0A\\1_0\nOn AC Power: True\nBattery is Discharging: False\nBattery is Charging: True\nRemaining capacity: 42273\n<\/PRE>\n<P>In other words, and unlike the typical Scripting Guy flashlight, the script appears to work just fine.<\/P>\n<P>Speaking of flashlights, once the stores were reopened and the shelves restocked did the Scripting Guy who writes this column go out and replenish his supply of batteries? Let\u2019s put it this way: remember the fable about <A href=\"http:\/\/en.wikipedia.org\/wiki\/The_Ant_and_the_Grasshopper\" target=\"_blank\"><B>the <\/B><B>grasshopper and the ant<\/B><\/A>, the story where the ant worked hard to store up food for the winter while the grasshopper spent <I>his<\/I> time singing and dancing? To be honest, we don\u2019t know much about this particular fable; we were too busy singing and dancing to finish reading it. But we\u2019re sure that, somehow, the grasshopper magically ended up with all the food and batteries he needed to get him through the winter. <\/P>\n<P>At least we hope he did.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell whether a laptop computer is running off batteries?&#8212; TJ Hey, TJ. Ah, yes: batteries. Back in December the Scripting House lost power for six days. Was the Scripting Guy who writes this column concerned? Of course not. After all, he was fully prepared for an emergency like that: [&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":[90,34,2,3,5],"class_list":["post-65123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-batteries-and-power-supplies","tag-hardware","tag-running","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell whether a laptop computer is running off batteries?&#8212; TJ Hey, TJ. Ah, yes: batteries. Back in December the Scripting House lost power for six days. Was the Scripting Guy who writes this column concerned? Of course not. After all, he was fully prepared for an emergency like that: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65123","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=65123"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65123\/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=65123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}