Bat or Exe file to restart Computer Browser Service


  1. Posts : 6
    Windows 10
       #1

    Bat or Exe file to restart Computer Browser Service


    About 1 out of every 6 times I turn my laptop on I have to restart the computer browser service to be able to connect to networks. Is there a bat or exe file I can create that I can just click on and run to restart the service instead of having to go into the services/computer browser area to restart each time this happens? Thanks.
      My Computer


  2. Posts : 93
    Windows 10
       #2

    You should really get to the root of the problem, but since this is a good opportunity to brush up on my scripting I’m going to assume you’ve verified the service’s settings are regular and that you’ve exhausted your options, so here are your suggestions in fruition.

    Batch (.bat)

    Code:
    @echo off
    
    set "service=MyServiceName"
    
    >nul sc query "%SERVICE:"=%"
    if errorlevel 1 (
        if errorlevel 1060 (
            >&2 echo Cannot find any service with service name '"%SERVICE:"=%"'
        ) else (
            >&2 echo Error
        )
        exit /b 1
    )
    
    sc query "%SERVICE:"=%" | find "STATE" | findstr "2 4" >nul
    if errorlevel 1 (
        >nul sc start "%SERVICE:"=%"
        if errorlevel 1 (
            >&2 echo Error starting service
            exit /b 1
        )
    ) else (
        >nul sc stop "%SERVICE:"=%"
        if errorlevel 1 (
            >&2 echo Error stopping service
            exit /b 1
        )
        >nul pathping -n -p 220 -q 1 127.0.0.1
        >nul sc start "%SERVICE:"=%"
    )

    C# (.exe)

    PHP Code:
    using System;
    using System.Windows.Forms;
    using System.ServiceProcess;

    class 
    Program
    {
        static 
    void Main(string[] args)
        {
            
    ServiceController service = new ServiceController("MyServiceName");

            if ((
    service.Status & (ServiceControllerStatus.Running ServiceControllerStatus.StartPending)) == 0)
            {
                try
                {
                    
    service.Start();
                }
                catch (
    Exception e)
                {
                    
    MessageBox.Show(e.Message);
                }
            }
            else
            {
                try
                {
                    
    service.Stop();
                    
    service.WaitForStatus(ServiceControllerStatus.Stopped);
                    
    service.Start();
                }
                catch (
    Exception e)
                {
                    
    MessageBox.Show(e.Message);
                }
            }
        }


    VBScript (.vbs)

    Code:
    strService = "MyServiceName"
    
    Set objSWbemServices = GetObject("winmgmts:\\.\root\cimv2")
    
    Set colServices = objSWbemServices.ExecQuery("SELECT * FROM Win32_Service WHERE Name = '" & strService & "'")
    
    If colServices.Count = 0 Then
        WScript.Echo "Cannot find any service with service name '" & strService & "'"
    End If
    
    On Error Resume Next
    For Each svc In colServices
        If svc.State = "Running" Or svc.State = "Start Pending" Then
            result = svc.StopService
    
            If result <> 0 Then
                If result = 2 Then
                    WScript.Echo "Error stopping service: Access is denied"
                    WScript.Quit 1
                End If
    
                WScript.Echo "Error stopping service: " & Err.Description
                WScript.Quit 1
            End If
    
            WScript.Sleep 200
            svc.StartService
        Else
            svc.StartService
    
            If result <> 0 Then
                WScript.Echo "Error starting service: " & Err.Description
                WScript.Quit 1
            End If
        End If
    Next

    You should replace ‘MyServiceName’ with the name of your service (not to be confused with the service display name, e.g., ‘w32time’ instead of ‘Windows Time’).

    Many services can be started but cannot be stopped without administrative privileges. If you know or suspect this is the case with your service, you may need to run the program as administrator each time.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 19:46.
Find Us




Windows 10 Forums