{"id":69203,"date":"2005-08-10T18:04:00","date_gmt":"2005-08-10T18:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/08\/10\/how-can-i-tell-if-the-screen-saver-is-active\/"},"modified":"2005-08-10T18:04:00","modified_gmt":"2005-08-10T18:04:00","slug":"how-can-i-tell-if-the-screen-saver-is-active","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-the-screen-saver-is-active\/","title":{"rendered":"How Can I Tell if the Screen Saver is Active?"},"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 if the screen saver is active and, if so, how long it has been running?<BR><BR>&#8212; MS<\/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, MS. Interesting question; we don\u2019t think we\u2019ve ever been asked that before. And at first it had us stumped; after all, there aren\u2019t any WMI classes or COM objects for managing the screen saver, and we didn\u2019t know of any event that was fired each time a screen saver kicked in, at least not an event we could get to using a script. This had all the makings of a complex and complicated problem that would likely take us years to solve.<\/P>\n<P>Or, as it turned out, it was something we could solve in just a couple minutes:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-dc-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colProcesses = objWMIService.ExecQuery(&#8220;Select * from Win32_Process&#8221;)<\/p>\n<p>For Each objProcess in colProcesses\n    If Right(objProcess.Name, 4) = &#8220;.scr&#8221; Then\n        Wscript.Echo &#8220;The screen saver &#8221; &amp; objProcess.Name &amp; &#8221; is running.&#8221;\n        Wscript.Echo &#8220;Screen saver start time: &#8221; &amp; objProcess.CreationDate\n        Wscript.Quit\n    End If\nNext<\/p>\n<p>Wscript.Echo &#8220;The screen saver is not running.&#8221;\n<\/PRE>\n<P>To be honest, we were originally making a mountain out of a molehill. Granted, there are no COM objects for managing the screen saver and there are no events fired when the screen saver kicks in. But that\u2019s OK. When the screen saver runs, it typically runs a file with a .scr file extension. If we want to know whether or not the screen saver is running all we have to do is check and see if there are any processes running that have an executable name ending in <B>.scr<\/B>. If there are, that probably means the screen saver is running.<\/P>\n<TABLE class=\"dataTable\" id=\"E6C\" 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>. We\u2019re aware that this isn\u2019t foolproof and there might be third-party screen savers that use a different file extension. But this approach should work for any of the screen savers that ship with the operating system.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now that we understand the strategy let\u2019s take a look at the script. For one thing, you might notice that the first line of code is a little different from most of our sample WMI scripts:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-dc-01&#8221;\n<\/PRE>\n<P>Typically our first line of code sets the value of the variable strComputer to a dot (.); we do that because the dot represents the local computer, and setting strComputer to a dot gives us a script that will run on the local computer regardless of the name of that computer. We\u2019re assuming, however, that you want to run this script against a remote machine; there are probably easier ways to tell whether or not the screen saver is running on your local machine without going to the trouble of writing a script. Therefore we set the value of strComputer to the name of a remote computer, in this case atl-dc-01.<\/P>\n<P>Next we connect to the <B>Win32_Process<\/B> class and use the <B>ExecQuery<\/B> method to retrieve a collection of all the processes running on the computer atl-dc-01. We then set up a For Each loop to cycle through that collection. Inside the loop we check the value of the <B>Name<\/B> property for each process (this will be equivalent to the file name for the application). If the Name ends in <B>.scr<\/B> we assume that a screen saver is running; we then echo the executable file name for the screen saver as well as the time that the screen saver started running. (We can retrieve that value from the <B>CreationDate<\/B> property.) Having found the screen saver, we then call the <B>Wscript.Quit<\/B> method to exit the script.<\/P>\n<P>How do we know if the executable file name ends in .scr? We simply use the VBScript <B>Right<\/B> function, and tell it to check the last four characters in the string:<\/P><PRE class=\"codeSample\">If Right(objProcess.Name, 4) = &#8220;.scr&#8221; Then\n<\/PRE>\n<P>But what if we <I>don\u2019t<\/I> find a process whose Name ends in .scr? After checking each and every process, we exit the loop and then echo a message stating that the screen saver is not running. <\/P>\n<P>When we run the script, we get back output similar to this:<\/P><PRE class=\"codeSample\">The screen saver logon.scr is running.\nScreen saver start time: 20050725153639.115344-420\n<\/PRE>\n<P>Well, yes, that\u2019s a good point: the start time comes back in UTC (Universal Time Coordinate) format, which isn\u2019t the most intuitive date-time format ever created. Therefore, let\u2019s modify our script to include a function that convert this UTC date-time into something a bit more readable:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-dc-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colProcesses = objWMIService.ExecQuery(&#8220;Select * from Win32_Process&#8221;)<\/p>\n<p>For Each objProcess in colProcesses\n    If Right(objProcess.Name, 4) = &#8220;.scr&#8221; Then\n        Wscript.Echo &#8220;The screen saver &#8221; &amp; objProcess.Name &amp; &#8221; is running.&#8221;\n        dtmStartTime = objProcess.CreationDate\n        dtmScreensaverStart = WMIDateStringToDate(dtmStartTime)\n        Wscript.Echo &#8220;Screen saver start time: &#8221; &amp; dtmScreensaverStart\n        Wscript.Quit\n    End If\nNext<\/p>\n<p>Wscript.Echo &#8220;The screen saver is not running.&#8221;<\/p>\n<p>Function WMIDateStringToDate(dtmBootup)\n    WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) &amp; &#8220;\/&#8221; &amp; _\n        Mid(dtmBootup, 7, 2) &amp; &#8220;\/&#8221; &amp; Left(dtmBootup, 4) _\n            &amp; &#8221; &#8221; &amp; Mid (dtmBootup, 9, 2) &amp; &#8220;:&#8221; &amp; _\n                Mid(dtmBootup, 11, 2) &amp; &#8220;:&#8221; &amp; Mid(dtmBootup,13, 2))\nEnd Function\n<\/PRE>\n<P>When we run <I>this<\/I> script we get back output that\u2019s a little easier to parse:<\/P><PRE class=\"codeSample\">The screen saver logon.scr is running.\nScreen saver start time: 7\/25\/2005 3:36:39 PM\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EWE\" 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>. We won\u2019t discuss the function for converting a UTC date to a regular date, but you can read about the process in <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wmi_yakv.mspx\" target=\"_blank\"><B>this section<\/B><\/A> of the <I>Microsoft Windows 2000 Scripting Guide<\/I>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Like we said, determining whether or not the screen saver is running turned out to be fairly easy. Which makes us wonder whether people have been making this cold fusion thing much tougher than it should be\u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell if the screen saver is active and, if so, how long it has been running?&#8212; MS Hey, MS. Interesting question; we don\u2019t think we\u2019ve ever been asked that before. And at first it had us stumped; after all, there aren\u2019t any WMI classes or COM objects for managing [&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":[237,16,3,5],"class_list":["post-69203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-basic-computer-information","tag-desktop-management","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell if the screen saver is active and, if so, how long it has been running?&#8212; MS Hey, MS. Interesting question; we don\u2019t think we\u2019ve ever been asked that before. And at first it had us stumped; after all, there aren\u2019t any WMI classes or COM objects for managing [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69203","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=69203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69203\/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=69203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}