PowerShell Set-Service And New-Service Cmdlets
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Get-Service cmdlet in PowerShell.
https://cloudaffaire.com/powershell-get-service-cmdlet/
In this blog post, we will discuss Set-Service and New-Service cmdlets in PowerShell. You can use Set-Service cmdlet to modify properties of a service such as Status, Description, DisplayName, StartupType, etc. Using Set-Service cmdlet, you can start, stop, suspend, or pause a service in both your local system as well as your remote system. You need permission to manage windows service to use Set-Service cmdlet. Set-Service cmdlet can also be used to manage the service property of a remote computer using ‘ComputerName’ parameter.
You can use New-Service cmdlet to create a new windows service. New-Service cmdlet creates a new windows service in the registry and in the service database. To create a new service, you need to have manage service permission and an executable file for your service. New-Service cmdlet provides different parameters using which you can define service property line service name, service dependencies, service description, service start-up mode, etc.
Set-Service Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 |
## Set-Service ## [-ComputerName ## [-Name] ## [-DisplayName ## [-Description ## [-StartupType ## [-Status ## [-PassThru] ## [-WhatIf] ## [-Confirm] ## [ |
New-Service Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 |
## New-Service ## [-Name] ## [-BinaryPathName] ## [-DisplayName ## [-Description ## [-StartupType ## [-Credential ## [-DependsOn ## [-WhatIf] ## [-Confirm] ## [ |
Set-Service Cmdlet Argument List:
- –ComputerName: Specifies one or more computers. For remote computers, type the NetBIOS name, an IP address, or a fully qualified domain name.
- –Confirm: Prompts you for confirmation before running Set-Service.
- –Description: Specifies a new description for the service.
- –DisplayName: Specifies, as a string array, the display names of services to be modified. Wildcards are permitted.
- –InputObject: Specifies a ServiceController object that represents the service to change.
- –Name: Specifies the service name of the service to be changed. Wildcard characters aren’t permitted.
- –PassThru: Returns a ServiceController object that represents the services that were changed.
- –StartupType: 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.
- Disabled: The service is disabled and cannot be started by a user or application.
- Manual: The service is started only manually, by a user, using the Service Control Manager, or by an application.
- Boot: Indicates that the service is a device driver started by the system loader.
- System: Indicates that the service is a device driver started by the ‘IOInitSystem()’ function.
- –Status: Specifies the status for the service. The acceptable values for this parameter are as follows:
- Paused: Suspends the service.
- Running: Starts the service.
- Stopped: Stops the service.
- –WhatIf: Shows what would happen if Set-Service runs. The cmdlet isn’t run.
New-Service Cmdlet Argument List:
- –BinaryPathName: Specifies the path of the executable file for the service. This parameter is required.
- –Confirm: Prompts you for confirmation before running the cmdlet.
- –Credential: Specifies the account used by the service as the Service Logon Account.
- –DependsOn: Specifies the names of other services upon which the new service depends.
- –Description: Specifies a description of the service.
- –DisplayName: Specifies a display name for the service.
- –Name: Specifies the name of the service. This parameter is required.
- –StartupType: 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.
- Disabled: The service is disabled and cannot be started by a user or application.
- Manual: The service is started only manually, by a user, using the Service Control Manager, or by an application.
- Boot: Indicates that the service is a device driver started by the system loader.
- System: Indicates that the service is a device driver started by the ‘IOInitSystem()’ function.
- –WhatIf: Shows what would happen if the cmdlet runs. The cmdlet is not run.
PowerShell Set-Service And New-Service Cmdlet:
Create A New Windows Service In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
##################################################### ## PowerShell | Cmdlet | Set-Service | New-Service ## ##################################################### ## PowerShell Latest Version (5) ## open PowerShell as admin ## create a new service for this demo $params = @{ Name = "MyService" BinaryPathName = "C:\Program Files\VideoLAN\VLC\vlc.exe" DependsOn = "Winmgmt" DisplayName = "My Test Service" StartupType = "Manual" Description = "Test Service Created For PowerShell Demo" } New-Service @params ## get the service details Get-Service -Name "MyService" | Select-Object * |
Change Windows Service Display Name In PowerShell:
1 2 3 4 5 |
## change windows service display name in PowerShell Set-Service -Name "MyService" -DisplayName "New Display Name" Get-Service -Name "MyService" | Select-Object * |
Change Windows Service Description In PowerShell:
1 2 3 4 5 |
## change windows service description in PowerShell Set-Service -Name "MyService" -Description "New Description" Get-WmiObject win32_service | Where-Object { $_.Name -eq "MyService" } | Select-Object * |
Change Windows Service Start-Up Mode In PowerShell:
1 2 3 4 5 |
## change windows service start up mode in PowerShell Set-Service -Name "MyService" -StartupType Automatic Get-Service -Name "MyService" | Select-Object * |
Change Windows Service Status In PowerShell:
1 2 3 4 5 6 7 8 9 |
## change windows service status in PowerShell Set-Service -Name "TermService" -Status Running Get-Service -Name "TermService" | Select-Object * ## delete MyService service sc.exe delete MyService |
Hope you have enjoyed this article. In the next blog post, we will discuss other service-related cmdlets in PowerShell.
To Set more details on PowerShell, kindly follow below official documentation