Create

Creates a new service.

Creates a new service.

Example

Do not run below example code just to see what happens next. Many methods seriously affect your system. Always make sure you actually understand what the method and the code do.

# define the arguments you want to submit to the method
# remove values that you do not want to submit
# make sure you replace values with meaningful content before running the code
# see section "Parameters" below for a description of each argument.
$arguments = @{
    DesktopInteract = [Boolean](12345)  # replace 12345 with a meaningful value
    DisplayName     = 'someText'  # replace 'someText' with meaningful text
    ErrorControl    = [UInt8](12345)  # replace 12345 with a meaningful value
    LoadOrderGroup  = 'someText'  # replace 'someText' with meaningful text
    LoadOrderGroupDependencies = 'someText'  # replace 'someText' with meaningful text
    Name            = 'someText'  # replace 'someText' with meaningful text
    PathName        = 'someText'  # replace 'someText' with meaningful text
    ServiceDependencies = 'someText'  # replace 'someText' with meaningful text
    ServiceType     = [UInt8](12345)  # replace 12345 with a meaningful value
    StartMode       = 'someText'  # replace 'someText' with meaningful text
    StartName       = 'someText'  # replace 'someText' with meaningful text
    StartPassword   = 'someText'  # replace 'someText' with meaningful text
}


Invoke-CimMethod -ClassName Win32_BaseService -Namespace Root/CIMV2 -MethodName Create -Arguments $arguments |
Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
  switch ([int]$this.ReturnValue)
  {
        0        {'Success'}
        1        {'Not Supported'}
        2        {'Access Denied'}
        3        {'Dependent Services Running'}
        4        {'Invalid Service Control'}
        5        {'Service Cannot Accept Control'}
        6        {'Service Not Active'}
        7        {'Service Request Timeout'}
        8        {'Unknown Failure'}
        9        {'Path Not Found'}
        10       {'Service Already Running'}
        11       {'Service Database Locked'}
        12       {'Service Dependency Deleted'}
        13       {'Service Dependency Failure'}
        14       {'Service Disabled'}
        15       {'Service Logon Failed'}
        16       {'Service Marked For Deletion'}
        17       {'Service No Thread'}
        18       {'Status Circular Dependency'}
        19       {'Status Duplicate Name'}
        20       {'Status Invalid Name'}
        21       {'Status Invalid Parameter'}
        22       {'Status Invalid Service Account'}
        23       {'Status Service Exists'}
        24       {'Service Already Paused'}
        default  {'Unknown Error '}
    }
}

To run this method on one or more remote systems, use New-CimSession:

$ComputerName = 'server12','server14'  # adjust to your server names
$Credential   = Get-Credential         # submit a user account with proper permissions

# define the arguments you want to submit to the method
# remove values that you do not want to submit
# make sure you replace values with meaningful content before running the code
# see section "Parameters" below for a description of each argument.
$arguments = @{
    DesktopInteract = [Boolean](12345)  # replace 12345 with a meaningful value
    DisplayName     = 'someText'  # replace 'someText' with meaningful text
    ErrorControl    = [UInt8](12345)  # replace 12345 with a meaningful value
    LoadOrderGroup  = 'someText'  # replace 'someText' with meaningful text
    LoadOrderGroupDependencies = 'someText'  # replace 'someText' with meaningful text
    Name            = 'someText'  # replace 'someText' with meaningful text
    PathName        = 'someText'  # replace 'someText' with meaningful text
    ServiceDependencies = 'someText'  # replace 'someText' with meaningful text
    ServiceType     = [UInt8](12345)  # replace 12345 with a meaningful value
    StartMode       = 'someText'  # replace 'someText' with meaningful text
    StartName       = 'someText'  # replace 'someText' with meaningful text
    StartPassword   = 'someText'  # replace 'someText' with meaningful text
}


$session = New-CimSession -ComputerName $ComputerName -Credential $Credential

Invoke-CimMethod -ClassName Win32_BaseService -Namespace Root/CIMV2 -MethodName Create -Arguments $arguments -CimSession $session |
Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
  switch ([int]$this.ReturnValue)
  {
        0        {'Success'}
        1        {'Not Supported'}
        2        {'Access Denied'}
        3        {'Dependent Services Running'}
        4        {'Invalid Service Control'}
        5        {'Service Cannot Accept Control'}
        6        {'Service Not Active'}
        7        {'Service Request Timeout'}
        8        {'Unknown Failure'}
        9        {'Path Not Found'}
        10       {'Service Already Running'}
        11       {'Service Database Locked'}
        12       {'Service Dependency Deleted'}
        13       {'Service Dependency Failure'}
        14       {'Service Disabled'}
        15       {'Service Logon Failed'}
        16       {'Service Marked For Deletion'}
        17       {'Service No Thread'}
        18       {'Status Circular Dependency'}
        19       {'Status Duplicate Name'}
        20       {'Status Invalid Name'}
        21       {'Status Invalid Parameter'}
        22       {'Status Invalid Service Account'}
        23       {'Status Service Exists'}
        24       {'Service Already Paused'}
        default  {'Unknown Error '}
    }
}


Remove-CimSession -CimSession $session

Learn more about Invoke-CimMethod and invoking WMI methods.

Syntax

uint32 Create(
  [in] string  Name,
  [in] string  DisplayName,
  [in] string  PathName,
  [in] uint8   ServiceType,
  [in] uint8   ErrorControl,
  [in] string  StartMode,
  [in] boolean DesktopInteract,
  [in] string  StartName,
  [in] string  StartPassword,
  [in] string  LoadOrderGroup,
  [in] string  LoadOrderGroupDependencies[],
  [in] string  ServiceDependencies[]
);

Parameters

Name Type Description
DesktopInteract Boolean The DesktopInteract parameter passes the value that indicates whether the service can create or communicate with windows on the desktop.
Values: TRUE or FALSE. A value of TRUE indicates the service can create or communicate with windows on the desktop.
DisplayName String The DisplayName parameter passes the display name of the service. This string has a maximum length of 256 characters. The name is case-preserved in the service control manager. DisplayName comparisons are always case-insensitive.
Constraints: Accepts the same value as the Name parameter.
Example: Atdisk.
ErrorControl UInt8 If the Create method fails to start, the ErrorControl parameter passes the severity of the error. The value indicates the action taken by the startup program if failure occurs. All errors are logged by the system. The system does not notify the user of “Ignore” errors. With “Normal” errors the user is notified. With “Severe” errors, the system is restarted with the last-known-good configuration. Finally, on “Critical” errors, the system attempts to restart with a good configuration.
LoadOrderGroup String The LoadOrderGroup parameter passes the group name associated with the new service. Load order groups are contained in the registry, and determine the sequence in which services are loaded into the operating system. If the pointer is NULL or if it points to an empty string, the service does not belong to a group. Dependencies between groups should be listed in the LoadOrderGroupDependencies parameter. Services in the load-ordering group list are started first, followed by services in groups not in the load-ordering group list, followed by services that do not belong to a group. The registry has a list of load ordering groups located at:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ServiceGroupOrder.
LoadOrderGroupDependencies String The LoadOrderGroupDependencies parameter passes a list of load ordering groups that must start before this service. The array is doubly null-terminated. If the pointer is NULL or if it points to an empty string, the service has no dependencies. Group names must be prefixed by the SC_GROUP_IDENTIFIER (defined in the WINSVC.H file) character to differentiate it from a service name, because services and service groups share the same name space. Dependency on a group means that this service can run if at least one member of the group is running after an attempt to start all members of the group.
Name String The Name parameter passes the name of the service to install to the Create method. The maximum string length is 256 characters. The service control manager database preserves the case of the characters, but service name comparisons are always case insensitive. Forward-slashes (/) and double back-slashes () are invalid service name characters.
PathName String The PathName parameter passes the fully qualified path to the executable file that implements the service.
Example: \SystemRoot\System32\drivers\afd.sys
ServiceDependencies String The ServiceDependencies parameter passes a list containing names of services that must start before this service starts. The array is doubly null-terminated. If the pointer is NULL, or if it points to an empty string, the service has no dependencies. Dependency on a service means that this service can only run if the service it depends on is running.
ServiceType UInt8 The ServiceType parameter passes the type of services provided to processes that call them.
StartMode String The StartMode parameter passes the start mode of the Win32 base service. “Boot” specifies a device driver started by the operating system loader. This value is valid only for driver services. “System” specifies a device driver started by the IoInitSystem function. This value is valid only for driver services. “Automatic” specifies a service to be started automatically by the service control manager during system startup. “Manual” specifies a service to be started by the service control manager when a process calls the StartService function. “Disabled” specifies a service that can no longer be started.
StartName String The StartName parameter passes the account name the service runs under. Depending on the service type, the account name may be in the form of “DomainName\Username”.The service process will be logged using one of these two forms when it runs. If the account belongs to the built-in domain, “.\Username” can be specified. If NULL is specified, the service will be logged on as the LocalSystem account. For a kernel or system-level drivers, StartName contains the driver object name (that is, \FileSystem\Rdr or \Driver\Xns) which the input and output (I/O) system uses to load the device driver. If NULL is specified, the driver runs with a default object name created by the I/O system based on the service name.
Example: DWDOM\Admin.
StartPassword String The StartPassword parameter passes the password to the account name specified by the StartName parameter. Specify NULL if you are not changing the password. Specify an empty string if the service has no password.

Return Value

Returns a value of type UInt32. Return values:

$returnValues = @{
    0    = 'Success'
    1    = 'Not Supported'
    2    = 'Access Denied'
    3    = 'Dependent Services Running'
    4    = 'Invalid Service Control'
    5    = 'Service Cannot Accept Control'
    6    = 'Service Not Active'
    7    = 'Service Request Timeout'
    8    = 'Unknown Failure'
    9    = 'Path Not Found'
    10   = 'Service Already Running'
    11   = 'Service Database Locked'
    12   = 'Service Dependency Deleted'
    13   = 'Service Dependency Failure'
    14   = 'Service Disabled'
    15   = 'Service Logon Failed'
    16   = 'Service Marked For Deletion'
    17   = 'Service No Thread'
    18   = 'Status Circular Dependency'
    19   = 'Status Duplicate Name'
    20   = 'Status Invalid Name'
    21   = 'Status Invalid Parameter'
    22   = 'Status Invalid Service Account'
    23   = 'Status Service Exists'
    24   = 'Service Already Paused'
}

Requirements

To use Win32_BaseService, the following requirements apply:

PowerShell

Get-CimInstance was introduced with PowerShell Version 3.0, which in turn was introduced on clients with Windows 8 and on servers with Windows Server 2012.

If necessary, update Windows PowerShell to Windows PowerShell 5.1, or install PowerShell 7 side-by-side.

Operating System

Win32_BaseService was introduced on clients with Windows Vista and on servers with Windows Server 2008.

Namespace

Win32_BaseService lives in the Namespace Root/CIMV2. This is the default namespace. There is no need to use the -Namespace parameter in Get-CimInstance.

Implementation

Win32_BaseService is implemented in CIMWin32.dll and defined in CIMWin32.mof. Both files are located in the folder C:\Windows\system32\wbem:

explorer $env:windir\system32\wbem
notepad $env:windir\system32\wbem\CIMWin32.mof