VBScript: Start and Stop Windows Service

You can manage Windows Service through VBScript easily by using WMI Services. You can use the WMI class Win32_Service to get complete information of Windows Service. In this article, I am going write VBScript samples to Start, Stop, and Restart Windows Service.

Summary

VBScript to Stop Windows Service using WMI

1. Copy the below example vbscript code and paste it in notepad or a VBScript editor.
2. Change the value strService into your own windows service name.
3. If you want to stop a service in Remote machine, set the Remote computer name in the variable strComputer instead of  “.” (local machine).
4. Save the file with a .vbs extension, for example: StopService.vbs
5. Double-click the vbscript file (or Run this file from command window) to stop the given windows service.

' StopService.vbs
' Sample vbscript script to Stop Windows Service
' -------------------------------------------------------' 
Option Explicit
Dim objWMIService, objService
Dim strService,strComputer
strService="RemoteRegistry"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&strService&"'")
    objService.StopService
Next
WScript.Echo "Your "& strService & " service has stopped" 
WScript.Quit

Start Windows Service using VBScript and WMI

1. Copy the below example vbscript code and paste it in notepad or a VBScript editor.
2. Change the value strService into your own windows service name.
3. If you want to start a service in Remote machine, set the Remote computer name in the variable strComputer instead of “.” (local machine).
4. Save the file with a .vbs extension, for example: StarService.vbs
5. Double-click the vbscript file (or Run this file from command window) to start the given windows service.

' StartService.vbs
' Sample vbscript script to Start Windows Service
' -------------------------------------------------------' 
Option Explicit
Dim objWMIService, objService
Dim strService,strComputer
strService="RemoteRegistry"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&strService&"'")
    objService.StartService
Next
WScript.Echo "Your "& strService & " service has started" 
WScript.Quit

Restart Windows Service using VBScript and WMI

1. Copy the below example vbscript code and paste it in notepad or a VBScript editor.
2. Change the value strService into your own windows service name.
3. Change the value waitTime to increase time interval between Stop and Start the Windows Service.
4. If you want to restart a windows service in Remote machine, set the Remote computer name in the variable strComputer instead of “.” (localmachine).
5. Save the file with a .vbs extension, for example: StarService.vbs
6. Double-click the vbscript file (or Run this file from command window) to start the given windows service.

' RestartService.vbs
' Sample vbscript script to Restart Windows Service
' -------------------------------------------------------' 
Option Explicit
Dim objWMIService, objService
Dim strService,strComputer,waitTime
strService="RemoteRegistry"
strComputer = "."
waitTime=10000
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service Where Name = '"_
&strService&"'")
objService.StopService()
   WSCript.Sleep waitTime
objService.StartService()
Next
WScript.Echo "Your "& strService & " service has restarted" 
WScript.Quit

List all Windows Services and Running Status using VBScript

1. Copy the below example vbscript code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: ListService.vbs
3. Run this file from command window using cscript utility to list running status of all the windows services.

' ListServices.vbs
' Sample vbscript script to list all Windows Services
' -------------------------------------------------------' 
Option Explicit
Dim objWMIService, objService, strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
For Each objService In objWMIService.ExecQuery("Select * from Win32_Service")
Wscript.Echo objService.DisplayName  & " : "& objService.State
Next
WScript.Quit

CMD usage to run vbscript using cscript:

C:> cscript ListServices.vbs
Start, Stop and Restart Windows Service using VBScript
Advertisement

Leave a Comment