Create windows service using powershell

Shamelessly pilfered from The Powershell Guy.  I’ve used it before with no issue, but that doesn’t mean you won’t have one.  Use at your own risk.

$computer = "." # this computer
$class = "Win32_Service"
$method = "Create"
$mc = [wmiclass]"\\$computer\ROOT\CIMV2:$class"
$inparams = $mc.PSBase.GetMethodParameters($method)
$inparams.DesktopInteract = $false
$inparams.DisplayName = "WebDirs Trigger Command"
$inparams.ErrorControl = 0
$inparams.LoadOrderGroup = $null
$inparams.LoadOrderGroupDependencies = $null
$inparams.Name = "Service Name"  #
$inparams.PathName = "Path to .exe"
$inparams.ServiceDependencies = $null
$inparams.ServiceType = 16
$inparams.StartMode = "Automatic"
$inparams.StartName = $null # will start as localsystem builtin if null
$inparams.StartPassword = $null

$result = $mc.PSBase.InvokeMethod($method,$inparams,$null)
$result | Format-List
Advertisements

2 Comments on “Create windows service using powershell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: