I need to start a windows service via Powershell with a '1' as a parameter, like below:
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?
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.
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.
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.
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With