Mr. Roboto

Showstopper: Round Two

These routines take last month's tool a few steps further.

Last month, faithful reader Tom wanted to identify all the services on a remote computer that are configured to start automatically, but for one reason or another had stopped running.

Why not let PowerShell do it? Take advantage of the PowerShell pipeline and construct an expression like this to find services that should be running but aren't, and then attempt to start them:

PS C:\> Get-WmiObject 
win32_service -filter 
"startmode='auto' AND state
 !='running'"" -computer "APP
 SRV23" | foreach {$_.StartService()
  | Select ReturnValue}

I'm piping the expression to the ForEach cmdlet, which acts on each object it receives. In this case, it calls the StartService() method. The $_ represents the current object in the pipeline, which in my example is the Spooler service. This command will write another object to the pipeline, but all we need is the ReturnValue property. If the command was successful, you'll see a ReturnValue of 0. You may have to scroll to the right of your PowerShell window to see the value.

What if there are multiple services that must be started? How does this work when run against a list of servers? We're not using a cmdlet to start the service, so we don't have common parameters such as -Confirm, but we can achieve the same result. Here's a step in that direction:

Get-WmiObject win32_service -filter 
"startmode='auto' AND state !='running'"
 -computer $c | foreach { 
$confirm=Read-Host "Do you want to 
start" $_.Name service "on $c [YN]" 
 If ($confirm -eq "Y") {
   Write-Host "Starting" $_.Name 
service "on $c"
    if ($r -eq 0) {
      Write-Host "Successfully started" 
   $_.Name service "on $c." `
      -ForegroundColor Green
    else {
      Write-Host "Failed to start" 
   $_.Name service "on $c. Return
   Value=$r" `
      -ForegroundColor Red

Now for every stopped service, you'll be asked if you want to restart the service using the Read-Host cmdlet. Answer "Y" to do so. When the service starts, you'll get a confirmation message in green-if it doesn't, you'll get a red failure message.

Stop the Presses
Last month, I also showed you a filtering function called Show-Stopped that you could use like this:

PS C:\> 
"serverA","serverB","serverC" | show-stopped | format-table 
Pathname,StartName -autosize

Originally, I thought I could take the above code and write a second function to take the output object of Show-Stopped and start each service. Instead, I blended the two functions into one and called it Get-StoppedService. Because of some limitations with PowerShell version 1.0 with Windows Management Instrumentation (WMI), you can't use alternate credentials with this function. You'll need to make sure you're running it with full administrative rights.

You can pipe one or more computer names to the function without any parameters to get the same result you got with Show-Stopped:

PS C:\> 
"serverA","serverB","serverC" | get-
stoppedservice | format-table Sys-
Pathname,StartName -autosize

Perhaps you'd like to export the information for offline analysis:

PS C:\> get-content servers.txt | 
get-stoppedservice | export-clixml 

Now you can specify -start as a parameter and you'll be prompted to see if you want to start each stopped service:

PS C:\> get-content servers.txt | 
get-stoppedservice -start

The script files won't do anything alone. You'll need to copy and paste the functions into your PowerShell session, dot source the scripts to load the functions, or create a new script that includes these functions. Now you and Tom have a choice of tools and I hope you continue to see the benefit of PowerShell.

Download Mr. Roboto's Show-Stopped function at

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.


  • How To Configure Windows 10 for Intel Optane Memory

    Intel's Optane memory technology can significantly improve the performance of your Windows 10 system -- provided you enable it correctly. A single mistake can render the system unbootable. Here's how to do it the right way.

  • Microsoft and SAP Enhance Partnership with Teams Integration

    Microsoft and SAP this week described continuing partnership efforts on Microsoft Azure, while also planning a Microsoft Teams integration with SAP's enterprise resource planning product and other solutions.

  • Blue Squares Graphic

    Microsoft Previews Azure IoT Edge for Linux on Windows

    Microsoft announced a preview of Azure IoT Edge for Linux on Windows, which lets organizations tap Linux virtual machine processes that also work with Windows- and Azure-based processes and services.

  • How To Automate Tasks in Azure SQL Database

    Knowing how to automate tasks in the cloud will make you a more productive DBA. Here are the key concepts to understand about cloud scripting and a rundown of the best tools for automating code in Azure.

comments powered by Disqus