{"id":54773,"date":"2008-12-02T11:30:00","date_gmt":"2008-12-02T11:30:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/12\/02\/hey-scripting-guy-how-can-i-easily-reconfigure-services\/"},"modified":"2008-12-02T11:30:00","modified_gmt":"2008-12-02T11:30:00","slug":"hey-scripting-guy-how-can-i-easily-reconfigure-services","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-easily-reconfigure-services\/","title":{"rendered":"Hey, Scripting Guy! How Can I Easily Reconfigure Services?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>I have a problem. The pointy-headed boss (PHB) loves to play with his laptop. The problem is he does not know anything. He routinely hoses his computer, and then expects me to drop everything and come fix it. I am not making this up; we had an Exchange server crash one time, he knew we were trying to get the thing back up, and he comes into the server room whining about his laptop, expecting me to come look at it. And this is in the middle of an outage! Sorry, I digress. Where was I? Oh, yeah. His latest toy to play with is the services control tool. He thinks he knows better than Microsoft what needs to be running to make Windows Vista work. Now before you say it, I cannot take administrator rights away from him. The PHB insists on being a domain administrator. Is there a way I can easily reconfigure the services after he has screwed up everything?<BR><BR>&#8211; SV<\/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\"> \n<P>Hi SV,<\/P>\n<P>Don\u2019t you just love it when the PHB makes extra work for you? Consider it a learning experience, an opportunity to grow and to mature in your profession (cough, cough). Interestingly enough, I may have just the thing you are looking for. It is a script that I wrote for myself several years ago, and I used it when I used to fly every week. When I am using my laptop on an airplane, there are a number of services that I really do not need running. If I can easily stop a bunch of services I am not using, I can free up memory and often increase my battery life as well. The trick is to have a file that documents what your desired configuration is, and go through the services to ensure that your computer meets that configuration. In my case, what I did was have a list of services that I wanted to stop, read that file, and then use the <B>Stop-Service<\/B> cmdlet to perform the configuration change. Here is an example:<\/P><PRE class=\"codeSample\">Get-Content -Path C:\\fso\\runningServices.txt | Stop-Service<\/PRE>\n<P>That\u2019s it. One line of code. It does not even have to be put in a script, if you do not want to do so. The text file is not anything special\u2014it can be simply a list of service names:<\/P><IMG height=\"435\" alt=\"Image of the text file of service names\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1202\/HSG_ServiceConf01.jpg\" width=\"400\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>If I want to start the services in the text file, I use the same text file and the same syntax. However, instead of using the <B>Stop-Service<\/B> cmdlet, I use the <B>Start-Service<\/B> cmdlet. This is seen here:<\/P><PRE class=\"codeSample\">Get-Content -Path C:\\fso\\runningServices.txt | Start-Service<\/PRE>\n<P>To verify that I have not missed any of the services and that none of the services hung on start, I use the <B>Get-Service<\/B> cmdlet instead of <B>Start-Service<\/B> or <B>Stop-Service<\/B> cmdlet:<\/P><PRE class=\"codeSample\">Get-Content -Path C:\\fso\\runningServices.txt | Get-Service<\/PRE>\n<P>Here is an image of the output from the <B>Get-Service<\/B> command as seen in Windows PowerShell:<\/P><IMG height=\"323\" alt=\"Image of the output from the Get-Service command\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1202\/HSG_ServiceConf02.jpg\" width=\"550\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>What does this all have to do with the PHB? Perhaps nothing, but then again, perhaps everything. All you need to do is to get a baseline snapshot of the services you want to have running, store them in a text file, and then use the above three commands to manage the PHB. The hard part, it would seem, is creating a text file that lists all the services you want to have running on the computer and then cleaning up that text file. This script will create the text file for you:<\/P><PRE class=\"codeSample\">Get-Service |\nWhere-Object {$_.status -eq &#8216;running&#8217;} | \nFormat-Table name -HideTableHeaders | \nOut-File \u2013FilePath C:\\fso\\runningServices.txt\n<\/PRE>\n<P>This script could be a command that was typed on a single line. In fact, most of the time this is how I use it. The script begins by using <B>Get-Service<\/B> to obtain a listing of services. It then pipes the result to the <B>Where-Object<\/B> where the status of each service is checked. If the service is running, the service is passed to <B>Format-Table<\/B> where the name of the service is chosen and the table headers are hidden. From there, it is passed to <B>Out-File<\/B>, which creates the text file. <\/P>\n<P>If we were to use the text file directly, it would not work. This is because of blank lines and spaces that keep <B>Get-Service<\/B> from working. The errors are shown in this image:<\/P><IMG height=\"288\" alt=\"Image of the errors returned because of blank lines and spaces in the text file\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1202\/HSG_ServiceConf03.jpg\" width=\"600\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>In the past, I used to open the text file in Notepad and manually clean up the file. This is fine, but begins to get old after a while. The answer, of course, is to write a script. The resulting script is seen&nbsp;here:<\/P><PRE class=\"codeSample\">$serviceFile = &#8220;C:\\fso\\runningServices.txt&#8221;\n$tempFile = [io.path]::getTempFIleName()\n$content = Get-Content -path $serviceFile\n$content | \nForeach-object `\n{\n  if($_ -notmatch &#8220;^$&#8221;)\n    {\n      $_.trim() &gt;&gt; $tempFile\n    } #end if\n} #end foreach<\/p>\n<p>Remove-item $serviceFile\nCopy-Item $tempFile $serviceFile\n<\/PRE>\n<P>The script begins by assigning the path to the service file to the <B>$serviceFile<\/B> variable. The <B>runningServices.txt<\/B> file is the file we created earlier that contains white space and blank lines in it:<\/P><PRE class=\"codeSample\">$serviceFile = &#8220;C:\\fso\\runningServices.txt&#8221;<\/PRE>\n<P>Now for some cool stuff. The <B>io.path<\/B> .NET Framework class has the <B>GetTempFileName<\/B> static method. This will create a temporary file in the temporary directory. This is a great thing to use when you have a task that requires temporary storage. In Windows PowerShell, when you need to use a static method from a .NET Framework class, you put the class in square brackets and use a double-dotted notation. We store the temporary file name and path in the <B>$tempFile<\/B> variable (for a VBScript example of this technique, see the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/default.mspx?mfr=true\" target=\"_blank\">text files repository<\/A>): <\/P><PRE class=\"codeSample\">$tempFile = [io.path]::getTempFileName()<\/PRE>\n<P>To read the content of a text file, you use the <B>Get-Content<\/B> cmdlet. We give it the path to the text file we wish to read, and store the results in the <B>$content<\/B> variable. This is shown here: <\/P><PRE class=\"codeSample\">$content = Get-Content -path $serviceFile<\/PRE>\n<P>Now we want to groom our data. We can&#8217;t have a bunch of hairy strings hanging out in our nice clean text file. The first thing we do is take the content of the text file we stored in the <B>$content<\/B> variable and pipeline it to the <B>Foreach-Object<\/B> cmdlet. This will allow us to work with each line in the text file and is similar to using <B>readline<\/B> in VBScript. There are a couple examples of doing this with VBScript in the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/default.mspx?mfr=true\" target=\"_blank\">text files repository<\/A> as well. After we have each object, we use a regular expression pattern match that basically says the beginning of the line (<B>^<\/B>) followed by the end of the line (<B>$<\/B>)&#8230;but hold our horses, we don\u2019t want it to match, so we use <B>notmatch<\/B>. This gives a line with stuff in it and will remove blank lines. Now we use the <B>trim<\/B> method from the <B>string<\/B> class to get rid of blank spaces and the like from the beginning and the end of the line. We send this line to the temporary file by using the redirect and append operator, <B>&gt;&gt;<\/B><B>: <\/B><\/P><PRE class=\"codeSample\"><PRE class=\"codeSample\">$content | \nForeach-object `\n{\n  if($_ -notmatch &#8220;^$&#8221;)\n    {\n      $_.trim() &gt;&gt; $tempFile\n    } #end if\n} #end foreach\n<\/PRE><P>&nbsp;<\/P><P>The rest of the script deletes the original file by using <B>Remove-Item<\/B>, and copies the temporary file to the original file location. This is shown here:<\/P><PRE class=\"codeSample\">&nbsp;<\/PRE><PRE class=\"codeSample\">Remove-Item $serviceFile\nCopy-Item $tempFile $serviceFile\n<\/PRE><P>&nbsp;<\/P><P>Well, SV, this should help you to remove at least one dollop of annoyance delivered daily by your pointy-headed boss. Script you tomorrow.<\/P><\/PRE>\n<P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><SPAN class=\"Apple-style-span\"><B><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/B><\/SPAN><\/FONT><\/P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><B><\/B><\/FONT><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a problem. The pointy-headed boss (PHB) loves to play with his laptop. The problem is he does not know anything. He routinely hoses his computer, and then expects me to drop everything and come fix it. I am not making this up; we had an Exchange server crash one time, he knew we [&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,45],"class_list":["post-54773","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scripting-guy","tag-services","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>I have a problem. The pointy-headed boss (PHB) loves to play with his laptop. The problem is he does not know anything. He routinely hoses his computer, and then expects me to drop everything and come fix it. I am not making this up; we had an Exchange server crash one time, he knew we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54773","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=54773"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54773\/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=54773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}