Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Start Windows service with a parameter

I need to start a windows service via Powershell with a '1' as a parameter, like below:

enter image description here

So basically I want to do something like this with powershell:

Start-Service _MyService 1 <- won't work

Googling has produced nothing of note on this, perhaps I'm looking for the wrong thing, but I can't believe it's not possible. Clues anyone?

like image 968
Ben Power Avatar asked May 20 '16 03:05

Ben Power


People also ask

How do I Start a Windows service in PowerShell?

To start or stop a service through PowerShell, you can use the Start-Service or the Stop Service cmdlet, followed by the name of the service that you want to start or stop. For instance, you might enter Stop-Service DHCP or Start-Service DHCP.

What is param () in PowerShell?

Parameters can be created for scripts and functions and are always enclosed in a param block defined with the param keyword, followed by opening and closing parentheses. param() Inside of that param block contains one or more parameters defined by -- at their most basic -- a single variable as shown below.

How do I Start a remote service in PowerShell?

Method 3: Using PowerShellGet-Service -ComputerName computername -Name servicename | Restart-Service -Force. Get-Service -ComputerName computername -Name servicename | Stop-Service -Force. Get-Service -ComputerName computername -Name servicename | Start-Service.


1 Answers

An alternative is to use the Get-Service cmdlet to obtain a service controller, and then invoke its Start() method.

# "ServiceName" != "Display Name"
$yourService = Get-Service "ServiceName" 
$yourService.Start(1)

If you need to supply multiple arguments (credit to @Mark):

$yourService.Start(@('arg1','arg2'))
like image 96
Tung Avatar answered Nov 14 '22 00:11

Tung