Windows Services Stopping starting by using PowerShell script

Hi all,

Would you help me to develop a bot for Starting and Stopping a particular windows Services(like windows updation or fax etc.) using powersehll script. Already we have the activities for this apart from that I need to do using the powershell.

Thanks and regards.
Baisil G

Hi @baisilgee,

Why dont’t use cmd command like this:

>SC [start/stop/query] [service_name]

@riefnaibaho thank you for your replay,

but here particularly I need to use Powershell script

i think it is easier using cmd command, if you have list of services to control, you can put it into .bat script and call the script using “start process” activity.

But since you only want Powershell script, i suggest you to read these documentations:

Maybe a bit late, but you can try this commands:

#To STOP UiPath
Stop-Process -processname “UiPath.Service.UserHost” -Force
Stop-Process -processname “UiPath.Executor” -Force

#To Start the robot
start-process “<YourRobotpath.exe>”

# Prompt user for service name and action (start or stop)
$serviceName = Read-Host "Enter the name of the service"
$action = Read-Host "Enter 'start' or 'stop' to perform the action"

# Get the service object
$service = Get-Service -Name $serviceName

# Check if the service exists
if (!$service) {
    Write-Host "The service $serviceName does not exist"
    Exit
}

# Perform the specified action
if ($action -eq "start") {
    Start-Service $service
    Write-Host "The $serviceName service has been started"
} elseif ($action -eq "stop") {
    Stop-Service $service
    Write-Host "The $serviceName service has been stopped"
} else {
    Write-Host "Invalid action specified. Please enter 'start' or 'stop'"
}

You can save this script as a .ps1 file and run it from a PowerShell console or run it as a PowerShell command from a batch file or other script.

Note that you may need to run the script with elevated privileges (i.e., as an administrator) depending on the service you are trying to start or stop. Also, be sure to replace “Enter the name of the service” and “Enter ‘start’ or ‘stop’ to perform the action” with appropriate prompts for your use case.

If we put this lines of code form powershell and try to run the script from UiPath. The UiPath executor is not able to run this as elevated access is required what kind of user we need for this as even after having admin access to start and stop service manually windows services are not starting with script, only if I run the command prompt in run as admin mode it work which I am not able to do form UiPath as it says “access denied” form Studio in debug mode. Will this work once this bot is in production?

Is there any work around/ some feature/ a utility can be created using which we can run this with low level of accesses?

Try to add it at beginning of your PowerShell script

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process

The Set-ExecutionPolicy cmdlet in PowerShell is used to configure the execution policy for PowerShell scripts.

The -ExecutionPolicy parameter specifies the execution policy that you want to set, and the -Scope parameter specifies the scope of the policy. In this case, the scope is set to “Process” which means that the policy will only be effective for the current PowerShell session.

The execution policy determines whether PowerShell scripts can be run on a system and what kind of scripts are allowed to run. The available execution policies are:

  • Restricted: No scripts can be run. This is the default setting.
  • AllSigned: Only scripts signed by a trusted publisher can be run.
  • RemoteSigned: Scripts downloaded from the internet must be signed by a trusted publisher before they can be run.
  • Unrestricted: All scripts can be run.

Setting the execution policy to Unrestricted can potentially expose your system to security risks, as any script can be run without any restrictions. It is recommended to use a more restrictive policy such as RemoteSigned or AllSigned to help prevent malicious scripts from running.