Posts RSS Comments RSS 253 Posts and 411 Comments till now

Restart-Service (recursively)

I often need to restart services on multiple machines and one of the biggest issues is the dependent services that also need to be restarted. I finally found time to write a script that will recursively stop and start dependent services.


  • ServiceName: Name of the Service to restart (REQUIRED)
  • Server: Name of the Server to restart the service on (Default is local server)
  • Verbose: Enable verbose output

More Info:


Param($ServiceName = $(throw ‘$ServiceName is Required’),
      $Server = $Env:ComputerName,

$VerbosePreference = "Continue"

[system.Reflection.Assembly]::LoadWithPartialName("System.ServiceProcess") | out-Null

$ErrorActionPreference = "SilentlyContinue"

Write-Verbose " – ServiceName = $ServiceName"
Write-Verbose " – Server = $Server"

function Get-Dependents{
    Write-Verbose "   + Getting Dependent Services for $($MasterService.Name)"
    foreach($dependent in $MasterService.DependentServices)
        Write-Verbose "     – Found Dependent Service [$($dependent.Name)]"
        Get-Dependents $dependent

Write-Verbose " – Getting Service [$ServiceName]"
$Service = New-Object System.ServiceProcess.ServiceController($ServiceName,$Server)

Write-Verbose " – Getting Dependent Services"
$DependentServices = Get-Dependents $Service

Write-Verbose " + Stopping [$ServiceName] and dependent Services"

Write-Verbose "   – Waiting for Service to Stop"
$Service.WaitForStatus("Stopped",(new-object system.TimeSpan(0,0,20)))

Write-Verbose " + Starting [$ServiceName]"

Write-Verbose "   – Waiting for Service to Start"
$Service.WaitForStatus("Running",(new-object system.TimeSpan(0,0,20)))

foreach($dependent in $DependentServices )
    if($dependent.status -eq "Stopped")
        $dependent.WaitForStatus("Running",(new-object system.TimeSpan(0,0,5)))

Trackback this post | Feed on Comments to this post

Leave a Reply

You must be logged in to post a comment.