Start stopped Windows services via PowerShell

This script demonstrates how to start specified Windows services if they are not running. The parameter of the function in this script is services name separated by comma. After the services run, their names and statuses will show up later.

In production server, not all startup types of Windows services are automated. Some of them are manual for the sake of performance. But after reboot, those running services with manual startup types will not start again.

IT manager can maintain a list of manual startup services name and pass them to this script. Then this script will help to start them.

Follow the steps below:

  • Run PowerShell as administrator
  • Go to the directory that containing the script
  • Import module of the script
    Import-Module .\StartServicesNotRunning.psml
  • Run the cmdlet of the script
    Start-ServicesNotRunning dhcpserver,dns
  • In the code above the script will check if the dhcpserver and dns is started, if not then the script will start the services

Here are some code snippets for your references (source: TechNet).

#Example: Start-ServicesNotRunning -ServerNames dhcpserver,dns
Function Start-ServicesNotRunning(){
 param
 (
 [string[]]$ServerNames =$(throw "Parameter missing: -ServerNames ServerName1,ServerName2") #get multiple services
 )
 Write-Host -ForegroundColor Yellow "Get Services State..."
 Get-Service -Name $ServerNames | Sort-Object Status,Displayname | 
 ForEach-Object {
 $Displayname = $_.Displayname
 If($_.Status -eq "Running")
 { 
 Write-Host $Displayname $_.Status -ForegroundColor Green
 } Else
 {
 Write-Host $Displayname $_.Status $_.StartType -ForegroundColor Red
 Try
 {
 #server startup status is disabled.
 If($_.StartType -eq "Disabled")
 { 
 Write-Host -ForegroundColor Yellow $Displayname "Startup Status To Manual..."
 Set-Service -Name $_.Name -StartupType Manual -ErrorAction Stop
 } 
 #start it if not running
 Write-Host -ForegroundColor Yellow $Displayname "Starting..."
 
 Start-Service -InputObject $_ -PassThru -ErrorAction Stop
 $_.Refresh()
 Write-Host $Displayname $_.Status -ForegroundColor Green
 }
 Catch
 {
 Write-Host -ForegroundColor Red $Displayname $_.Exception.Message
 }
 }
 }
}

Export-ModuleMember -Function Start-ServicesNotRunning

Download: StartServicesNotRunning PowerShell Module

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s