Ask a Question

I need to Start and Stop Window Services using Test complete J script?


I need to Start and Stop Window Services using Test complete J script?

Hello ,

I need to Start and Stop Window Services using TestComplete J Script?




Thanks and Regards,
Wittmann Battenfeld India Pvt. Ltd.,
Email :
Website :
Mobile: +91 8870219392 | Landline: +91 44 4207700
Esteemed Contributor

In that case, I'd suggest looking into the SC (service controller) command for this process.  You should run it with elevated privileges.


As for passing in credentials, not sure how to go about that off the top of my head but I found the SC command prompt stuff ( with a quick Google search.  There should be some way of doing that as well.  The key is how to run the command prompt via TestComplete... and WshShell is the way to do it.

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Valued Contributor


I just scripted that a few days ago :


procedure xxxx

  wmiService, service;
  wmiService:= WMI.Service;
  service:= wmiService.Get('Win32_Service.Name=''theServiceName''');


please note that "theServiceName" is to be changed by the service name as it appears in windows services


also, "service.StopService" stops the service


finally, both return an integer. 0 indicates that the operation has correctly been processed.


see this page for all return codes :

thank you trying it.


I found a dos command that can stop & start the service. However it doesn't do anythign when i run it from Testcomplete


belwo is what I did, but it didn't do anything.


comExecute = "psservice \\\\" + serverName + " -u " + userName + " -p " + pWord + " " + command + " " + String.fromCharCode(34) + txtService + String.fromCharCode(34)


Sys.OleObject("WScript.Shell").Exec("cmd /c " + comExecute)




Community Hero



> I found a dos command

psservice is not a dos command but a utility by Sysinternals that can be downloaded from Microsoft site.


If you need to pass credentials to control remote machine, I would recommend WMI (like in the example from @m_essaid) with additional SWbemLocator object:


  /Alex [Community Hero]
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.

Hi ALex,


Thank you. But I am using javascript, I found the sample jscript in TestComplete website but they don't work in javascript. That's why I switched to using the psservice approach. I don't mind which approach as long as it works in javascript. This issue is holding me up for weeks now 😞 I hope you can assist.


I need to start the service remotely with credentials using javascript.


I appreciate your help.


Thank you so much,



I tried adding - SWbemLocator.ConnectServer  but failed as well. Can you



SmartBear Alumni (Retired)



You can use this example, just replace this line

  wmiService = GetObject("WinMgmts:{impersonationLevel=impersonate}!\\\\" + 
                          computerName + "\\root\\cimv2");

with the JavaScript version:

  let Locator = getActiveXObject("WbemScripting.SWbemLocator");

  // the params are: computer name, namespace, username, password
  wmiService = Locator.ConnectServer(".", "\\root\\cimv2", "", "");

Helen Kosova
SmartBear Documentation Team Lead
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️



Thank you for your help, I replaced my code with your suggestion and i bypassed the error but !eObjectsList.atEnd() always return true though objectsList.Count returns 150. Thus my script still failed. It cannot find the service that I sent.


function fn_GetService(computerName, serviceName)
 var wmiService, objectsList, eObjectsList, item, sysID;

let Locator = Sys.OleObject("WbemScripting.SWbemLocator");

wmiService = Locator.ConnectServer(computerName, "\\root\\cimv2", "domain\\testUser", "testPass");
objectsList = wmiService.InstancesOf("Win32_Service")

if (objectsList.Count > 0)
eObjectsList = new Enumerator(objectsList);
for (;!eObjectsList.atEnd();eObjectsList.moveNext())
item = eObjectsList.item();
if (item.Name == serviceName)
return item;
return null;

SmartBear Alumni (Retired)



Try replacing

  if (objectsList.Count > 0) { 
    eObjectsList = new Enumerator(objectsList);
    for (; !eObjectsList.atEnd(); eObjectsList.moveNext()) {
      item = eObjectsList.item();
      if (item.Name == serviceName)
        return item;


  if (objectsList.Count > 0) {
    for (let i = 0; i < objectsList.Count; i++) {
      item = objectsList.ItemIndex(i);
      if (item.Name == serviceName)
        return item;

Does this work?

Helen Kosova
SmartBear Documentation Team Lead
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

Thank you Helen for your quick reply but it did not work 😞 did not return any value.

Showing results for 
Search instead for 
Did you mean: