{"id":67873,"date":"2006-02-27T11:33:00","date_gmt":"2006-02-27T11:33:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/27\/how-can-i-disable-a-service\/"},"modified":"2006-02-27T11:33:00","modified_gmt":"2006-02-27T11:33:00","slug":"how-can-i-disable-a-service","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-disable-a-service\/","title":{"rendered":"How Can I Disable a Service?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" 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\"> \n<P>Hey, Scripting Guy! How can I disable a service?<BR><BR>&#8212; DS<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" 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\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DS. You know, the Scripting Guys seem to have stumbled upon a great idea. In <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/feb06\/hey0224.mspx\"><B>our last column<\/B><\/A>, we showed people how to disable the LMHosts file. Today, we\u2019re going to show everyone how to disable a service. If we continue down this path, we\u2019ll soon have shown you how to disable <I>everything<\/I> on a computer. Just think of that: no more problems with the network, no more help desk calls, no more users overwriting files that shouldn\u2019t have been overwritten. All we have to do is disable everything and all those problems will disappear! This could be our ticket to the big time.<\/P>\n<P>Admittedly, disabling everything on all your computers might introduce some <I>other<\/I> problems in your organization. But that\u2019s really an internal thing you guys will have to deal with. <\/P>\n<P>Besides, we\u2019ll probably be too busy counting our money to help you with that.<\/P>\n<P>Of course, until that money starts to roll in we still have to write this column and we still have to pay the bills. With that in mind, here\u2019s a script that disables the Alerter service on a computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colServiceList = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Service where Name = &#8216;Alerter'&#8221;)<\/p>\n<p>For Each objService in colServiceList\n    If objService.State = &#8220;Running&#8221; Then\n        objService.StopService()\n        Wscript.Sleep 5000\n    End If\n    errReturnCode = objService.ChangeStartMode(&#8220;Disabled&#8221;)   \nNext\n<\/PRE>\n<P>As you can see, it doesn\u2019t take much code to disable a service, and the little bit of code that <I>is<\/I> required is pretty basic. There is one tricky part, however, which is why we\u2019ll take a few minutes to walk you through the script. <\/P>\n<P>Besides, the Rolls Royce dealership doesn\u2019t open for another hour anyway.<\/P>\n<P>There\u2019s nothing too special about the way the script starts: we simply connect to the WMI service on the local computer (although we could also disable a service on a remote computer). We then issue this query, which returns a collection consisting of just one item: the Alerter service:<\/P><PRE class=\"codeSample\">Set colServiceList = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Service where Name = &#8216;Alerter'&#8221;)\n<\/PRE>\n<P>Incidentally, make sure you include a <B>Where<\/B> clause similar to the one above. Why? Well, suppose you leave the Where clause off. In that case, your collection will consist of all the services installed on the computer, and your script will then dutifully try to disable every single service you have.<\/P>\n<P>You\u2019re right: that\u2019s usually not a good thing. Besides, the Scripting Guys would have to sue you; after all, disabling everything on a computer is <I>our<\/I> idea!<\/P>\n<P>After we get back our collection, we set up a For Each loop to walk through all the items in that collection. (And yes, we know: there\u2019s only one item in the collection. But we still need to use a For Each loop.) Here\u2019s the tricky part. You <I>can<\/I> disable a service if it\u2019s running; however, the service will not actually <I>be<\/I> disabled until it stops. If you disable a running service, the service will continue running the same as ever; the change doesn\u2019t take effect until the service stops.<\/P>\n<P>Because of that, we use this line of code to check and see if the Alerter service is running:<\/P><PRE class=\"codeSample\">If objService.State = &#8220;Running&#8221; Then\n<\/PRE>\n<P>If the service <I>is<\/I> running, we then call the <B>StopService()<\/B> method in order to stop the thing. After that we pause for 5 seconds (5,000 milliseconds); that gives the service time to actually stop before we go ahead and disable it. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">objService.StopService()\nWscript.Sleep 5000\n<\/PRE>\n<TABLE id=\"EPE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. In the interests of keeping this sample script as short as possible, we\u2019re cheating a little bit. We\u2019re assuming that your service will either be Running or Stopped. In reality, a service can have other states, most notably Paused or Continuing. To make a more bulletproof script, you should probably check for those conditions (and take the appropriate action) as well. But because 99.9% of the time services <I>are<\/I> either running or stopped, you can usually get by with the sample script we showed you.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Once the service has stopped we then use this line of code to disable it:<\/P><PRE class=\"codeSample\">errReturnCode = objService.ChangeStartMode(&#8220;Disabled&#8221;)\n<\/PRE>\n<P>As you can see, we\u2019re simply using the <B>ChangeStartMode<\/B> method to change the value of the <B>StartMode<\/B> property. We want to disable the service so we pass the parameter \u201cDisabled\u201d to ChangeStartMode. We could also set the StartMode to \u201cManual\u201d or \u201cAutomatic\u201d; that would, well, set the StartMode to either Manual or Automatic.<\/P>\n<P>Remember, once a service has been stopped and disabled it can no longer be started; the only way to restart the service is to re-enable it (by changing the StartMode to Manual or Automatic). But we\u2019ll wait until we\u2019ve talked everyone into disabling everything on their computers before we tell you how to re-enable stuff. After all, we can probably charge double for <I>those<\/I> scripts. Do you think it would look weird if all of us got the same diamond-studded Rolex? <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I disable a service?&#8212; DS Hey, DS. You know, the Scripting Guys seem to have stumbled upon a great idea. In our last column, we showed people how to disable the LMHosts file. Today, we\u2019re going to show everyone how to disable a service. If we continue down this path, [&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":[31,3,39,5],"class_list":["post-67873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scripting-guy","tag-services","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I disable a service?&#8212; DS Hey, DS. You know, the Scripting Guys seem to have stumbled upon a great idea. In our last column, we showed people how to disable the LMHosts file. Today, we\u2019re going to show everyone how to disable a service. If we continue down this path, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67873","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=67873"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67873\/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=67873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}