The cmdlet “Set-Service” starts, stops, or suspends service in PowerShell remotely and locally. Moreover, it also alters the properties of the specific service including “Description”, “Status”, or a “DisplayName”. A service object or a service name can be utilized to identify a service.
In this blog, we will explain the “Set-Service” cmdlet in detail.
How to use the Set-Service (Microsoft.PowerShell.Management)?
The “Set-Service” cmdlet is used to alter the state of a specific service, such as run or stop. Let’s explore some of the given examples.
Example 1: Use the “Set-Service” Cmdlet to Start a Service
Execute the below-given command to initiate a service in PowerShell:
In the below-given code:
-
- First, mention the “Set-Service” cmdlet.
- Then, specify the “-Name” parameter and assign it the service name.
- After that, add the “-Status” parameter having the “Running” value assigned to it.
- Lastly, specify the “-PassThru” parameter:
Example 2: Use the “Set-Service” Cmdlet to Set the Status of a Service to Running
To set the service to status to running, use the below-mentioned code:
Set-Service -InputObject $Service -Status Running
In the stated code above:
-
- First, initialize the variable “$Service” and assign it the “Get-Service” cmdlet alongside the “-Name” parameter with the service name assigned to it.
- Lastly, specify the “Set-Service” cmdlet along with the “-InputObject” and “-Status” parameters having the stated values assigned to them:
Example 3: Use the “Set-Service” Cmdlet to Change the Display Name of the Service
Execute the mention command to change the display name of the service:
Get-Service Spooler
According to the above-stated code:
-
- First, specify the “Set-Service” cmdlet alongside the “-Name” parameter and assign it the service name.
- After that, mention the “-DisplayName” parameter and specify a new service name to rename the service.
- Lastly, verify the change by executing the “Get-Service” cmdlet along with the service name:
Example 4: Use the “Set-Service” Cmdlet to Change the Startup Type of a Service
To alter the service startup type simply execute the below-given command:
Get-Service Spooler | Select-Object -Property Name, StartType
In the above-described code:
-
- First, specify the “Set-Service” cmdlet along with the “-Name” parameter and assign it the service name.
- Next, specify the “-StartupType” parameter and provide it the “Automatic” value.
- After that, write the “Get-Service” and provided the stated service name along with the “|” pipeline.
- Lastly, mention the “Select-Object” cmdlet along with the “-Property” having the two properties assigned to it separated by a comma:
That was all about the “Set-Service” cmdlet in PowerShell.
Conclusion
The “Set-Service” cmdlet is used to start, stop, or suspend service in PowerShell. It is also utilized to change the properties of a service. These properties include “Status”, “Description”, or a “DisplayName”. This post has discovered the “Set-Service” cmdlet with the help of several examples.