Mr. Roboto


It's Windows PowerShell to the rescue, as Mr. Roboto looks for a way to see what services need restarting.

Sometimes services fail and sometimes another administrator may have stopped a service for maintenance or troubleshooting, and just forgot to restart. Reader Tom K. was looking for a way to go through a list of servers and find all services that were configured to run automatically, but had stopped.

My first thought was to write an HTML application using VBScript and Windows Management Instrumentation (WMI). Then I realized this is a perfect situation for Windows PowerShell. Once you have some PowerShell fundamentals under your belt, you can solve this challenge without any scripting. You don't have to install PowerShell on your servers -- you can accomplish everything from your Windows XP or Vista desktop.

I'll start with a basic expression and end with a more complete solution. To understand the basics, open a PowerShell session and try this single-line command:

PS C:\> get-wmiobject -query 
"Select * from win32_service 
where startmode='auto' AND state 

If you have any services set to run automatically, but they aren't, you should get something back. I want to reiterate that this isn't scripting: You're using PowerShell as an interactive console. To connect to a remote machine use this:

PS C:\> get-wmiobject -query 
"Select * from win32_service 
where startmode='auto' AND 
state !='running'" -computer "Server01"

To process the list of servers, we'll leverage the PowerShell pipeline, get the content of a text file and pass the contents to our Get-WMIObject cmdlet:

PS C:\> Get-content servers.txt | 
forEach { get-wmiobject -query 
"Select * from win32_service 
where startmode='auto' AND
 state !='running'" -computer $_}

You need administrative permission on the remote servers, but you can specify alternate credentials. The recommended approach is to save the alternate credential as a PowerShell variable and pass it on when needed like this:

PS C:\> $cred=get-credential 

Enter the password in the dialog box. The credential is stored securely within your PowerShell session and erased when it ends. To use the credential, modify the previous expression:

PS C:\> Get-content servers.txt | 
forEach { get-wmiobject -query 
"Select * from win32_service 
where startmode='auto' AND state
 !='running'" -computer $_ 
 -credential $cred}

Remember, you can't use alternate credentials for the local system. If you get several results, you probably can't tell which service goes with which server. All we have to do is give PowerShell some formatting instructions on what to display:

PS C:\> Get-content servers.txt | 
forEach { get-wmiobject -query 
"Select * from win32_service 
where startmode='auto' AND 
state !='running'" -computer $_} | 

While this should solve Tom's initial request, I've gone ahead and created a filtering function called Show-Stopped. You can run this function on the local machine like this:

PS C:\> show-stopped
ExitCode  : 0
Name      : LVSrvLauncher
ProcessId : 0
StartMode : Auto
State     : Stopped
Status    : OK

Because the function writes a Win32_Service object to the pipeline, you can access any of its properties. This example will give you a nicely formatted report. This function can also use an alternate credential, although you'll have to create it ahead of time, and it will use it for every server it processes:

PS C:\> cred=get-
credential "domain\admin"
PS C:\> get-content servers.txt | 
show-stopped $cred | Format-List 

Each server in servers.txt is piped to the function and makes a WMI connection with the alternate credential. The output is piped to Format-List. Because objects are coming out of the pipeline, you can write them to a file, send them a printer, format as HTML, e-mail or export.

Once you spend a little time with PowerShell, you'll quickly realize its value. I certainly hope my new friend Tom does as well.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog, on Twitter at and on Google Plus (http:/

comments powered by Disqus

Reader Comments:

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.