- New-Service
- Syntax
- Description
- Examples
- Example 1: Create a service
- Example 2: Create a service that includes description, startup type, and display name
- Example 3: View the new service
- Example 4: Set the SecurityDescriptor of a service when creating.
- Parameters
- Inputs
- Outputs
- Notes
- Start or stop Windows service from command line (CMD)
- Start-Service
- Syntax
- Description
- Examples
- Example 1: Start a service by using its name
- Example 2: Display information without starting a service
- Example 3: Start a service and record the action in a text file
- Example 4: Start a disabled service
- Parameters
- Inputs
- Outputs
- Notes
New-Service
Creates a new Windows service.
Syntax
Description
The New-Service cmdlet creates a new entry for a Windows service in the registry and in the service database. A new service requires an executable file that runs during the service.
The parameters of this cmdlet let you set the display name, description, startup type, and dependencies of the service.
Examples
Example 1: Create a service
This command creates a service named TestService.
Example 2: Create a service that includes description, startup type, and display name
This command creates a service named TestService. It uses the parameters of New-Service to specify a description, startup type, and display name for the new service.
Example 3: View the new service
This command uses Get-CimInstance to get the Win32_Service object for the new service. This object includes the start mode and the service description.
Example 4: Set the SecurityDescriptor of a service when creating.
This example adds the SecurityDescriptor of the service being created.
The SecurityDescriptor is stored in the $SDDLToSet variable. The SecurityDescriptorSddl parameter uses $SDDL to set the SecurityDescriptor of the new service.
Parameters
Specifies the path of the executable file for the service. This parameter is required.
The fully qualified path to the service binary file. If the path contains a space, it must be quoted so that it is correctly interpreted. For example, d:\my share\myservice.exe should be specified as ‘»d:\my share\myservice.exe»‘ .
The path can also include arguments for an auto-start service. For example, ‘»d:\myshare\myservice.exe arg1 arg2″‘ . These arguments are passed to the service entry point.
For more information, see the lpBinaryPathName parameter of CreateServiceW API.
Type: | String |
Aliases: | Path |
Position: | 1 |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Prompts you for confirmation before running the cmdlet.
Type: | SwitchParameter |
Aliases: | cf |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the account used by the service as the Service Logon Account.
Type a user name, such as User01 or Domain01\User01, or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, this cmdlet prompts you for a password.
Credentials are stored in a PSCredential object and the password is stored as a SecureString.
For more information about SecureString data protection, see How secure is SecureString?.
Type: | PSCredential |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the names of other services upon which the new service depends. To enter multiple service names, use a comma to separate the names.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies a description of the service.
Type: | String |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies a display name for the service.
Type: | String |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the name of the service. This parameter is required.
Type: | String |
Aliases: | ServiceName |
Position: | 0 |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the SecurityDescriptor for the service in Sddl format.
Type: | String |
Aliases: | sd |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Sets the startup type of the service. The acceptable values for this parameter are:
- Automatic — The service is started or was started by the operating system, at system start-up. If an automatically started service depends on a manually started service, the manually started service is also started automatically at system startup.
- AutomaticDelayedStart — Starts shortly after the system boots.
- Disabled — The service is disabled and cannot be started by a user or application.
- InvalidValue — This value is not supported. Using this value results in an error.
- Manual — The service is started only manually, by a user, using the Service Control Manager, or by an application.
The default value is Automatic.
Type: | Microsoft.PowerShell.Commands.ServiceStartupType |
Accepted values: | Automatic, Manual, Disabled, AutomaticDelayedStart, InvalidValue |
Position: | Named |
Default value: | Automatic |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Shows what would happen if the cmdlet runs. The cmdlet is not run.
Type: | SwitchParameter |
Aliases: | wi |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
None
You cannot pipe input to this cmdlet.
Outputs
This cmdlet returns an object that represents the new service.
Notes
This cmdlet is only available on Windows platforms.
To run this cmdlet, start PowerShell by using the Run as administrator option.
Start or stop Windows service from command line (CMD)
We normally use Services.msc to start or stop or disable or enable any service. We can do the same from windows command line also using net and sc utilities. Below are commands for controlling the operation of a service.
Command to stop a service:
To start a service:
You need to have administrator privileges to run net start/stop commands. If you are just a normal user on the computer, you would get an error like below.
To disable a service:
To enable a service:
To make a service start automatically with system boot:
Note: Space is mandatory after ‘=’ in the above sc commands.
This SC command works on a Windows 7 machine and also on the down-level editions of Windows i.e Windows XP/2003 and Windows Vista. Again, if you do not have administrator previliges you would get the below error.
Note that the service name is not the display name of a service. Each service is given a unique identification name which can be used with net or sc commands. For example, Remote procedure call (RPC) is the display name of the service. But the service name we need to use in the above commands is RpcSs.
So to start Remote procedure call service the command is:
These service names are listed below for each service. The first column shows the display name of a service and the second column shows the service name that should be used in net start or net stop or sc config commands.
Start-Service
Starts one or more stopped services.
Syntax
Description
The Start-Service cmdlet sends a start message to the Windows Service Controller for each of the specified services. If a service is already running, the message is ignored without error. You can specify the services by their service names or display names, or you can use the InputObject parameter to supply a service object that represents the services that you want to start.
Examples
Example 1: Start a service by using its name
This example starts the EventLog service on the local computer. The Name parameter identifies the service by its service name.
Example 2: Display information without starting a service
This example shows what would occur if you started the services that have a display name that includes «remote».
The DisplayName parameter identifies the services by their display name instead of their service name. The WhatIf parameter causes the cmdlet to display what would happen when you run the command but does not make changes.
Example 3: Start a service and record the action in a text file
This example starts the Windows Management Instrumentation (WMI) service on the computer and adds a record of the action to the services.txt file.
First we use Get-Service to get an object that represent the WMI service and store it in the $s variable. Next, we start the service. Without the PassThru parameter, Start-Service does not create any output. The pipeline operator (|) passes the object output by Start-Service to the Format-List cmdlet to format the object as a list of its properties. The append redirection operator (>>) redirects the output to the services.txt file. The output is added to the end of the existing file.
Example 4: Start a disabled service
This example shows how to start a service when the start type of the service is Disabled.
The first attempt to start the Telnet service (tlntsvr) fails. The Get-CimInstance command shows that the StartMode property of the Tlntsvr service is Disabled. The Set-Service cmdlet changes the start type to Manual. Now, we can resubmit the Start-Service command. This time, the command succeeds. To verify that the command succeeded, run Get-Service .
Parameters
Prompts you for confirmation before running the cmdlet.
Type: | SwitchParameter |
Aliases: | cf |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the display names of the services to start. Wildcard characters are permitted.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
Specifies services that this cmdlet omits. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as s* . Wildcard characters are permitted.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
Specifies services that this cmdlet starts. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as s* . Wildcard characters are permitted.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | True |
Specifies ServiceController objects representing the services to be started. Enter a variable that contains the objects, or type a command or expression that gets the objects.
Type: | ServiceController [ ] |
Position: | 0 |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies the service names for the service to be started.
The parameter name is optional. You can use Name or its alias, ServiceName, or you can omit the parameter name.
Type: | String [ ] |
Aliases: | ServiceName |
Position: | 0 |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Returns an object that represents the service. By default, this cmdlet does not generate any output.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Shows what would happen if the cmdlet runs. The cmdlet is not run.
Type: | SwitchParameter |
Aliases: | wi |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
System.ServiceProcess.ServiceController, System.String
You can pipe objects that represent the services or strings that contain the service names to this cmdlet.
Outputs
None, System.ServiceProcess.ServiceController
This cmdlet generates a System.ServiceProcess.ServiceController object that represents the service, if you specify PassThru. Otherwise, this cmdlet does not generate any output.
Notes
This cmdlet is only available on Windows platforms.
- You can also refer to Start-Service by its built-in alias, sasv . For more information, see about_Aliases.
- Start-Service can control services only if the current user has permission to do this. If a command does not work correctly, you might not have the required permissions.
- To find the service names and display names of the services on your system, type Get-Service . The service names appear in the Name column, and the display names appear in the DisplayName column.
- You can start only the services that have a start type of Manual, Automatic, or Automatic (Delayed Start). You cannot start the services that have a start type of Disabled. If a Start-Service command fails with the message Cannot start service \ on computer , use Get-CimInstance to find the start type of the service and, if you have to, use the Set-Service cmdlet to change the start type of the service.
- Some services, such as Performance Logs and Alerts (SysmonLog) stop automatically if they have no work to do. When PowerShell starts a service that stops itself almost immediately, it displays the following message: Service \ start failed.