How to Restart a Windows Service using PowerShell?
As a system administrator, I have come across instances when a service on a computer or server would stop working, resulting in the system being unavailable. In such situations, restarting the service can be a quick fix. However, manually restarting services on multiple systems can be time-consuming and tedious. This is where PowerShell comes in handy. The PowerShell cmdlet Restart-Service provides a simple way to stop and start a service on a local or remote computer. In this article, we will cover the basics of using Restart-Service to restart services in PowerShell. You’ll learn the basic syntax, how to target local or remote services, restart multiple services at once, and even how to script and schedule automated restarts.
Table of contents
- Why You Should Use PowerShell to Restart Services?
- Understanding the PowerShell Restart-Service cmdlet
- Restarting a Service via the Services Console
- How to Restart a Service using PowerShell?
- Stop and Start Windows Service using Powershell
- Restarting Services with PowerShell on Remote Computer
- PowerShell Script to Restart Services on Multiple Computers
- Restarting IIS Services with PowerShell
- How to Restart Multiple Services using PowerShell?
- How to Schedule Restarting Services?
- Best Practices for Restarting Services with PowerShell
- Troubleshooting Common Issues with Service Restart
Why You Should Use PowerShell to Restart Services?
There are several reasons why you might need to restart a service on a computer or server. Restarting services is necessary when troubleshooting issues, applying updates, or restarting processes. Doing this manually through the Windows Services console can be time-consuming, especially if you need to restart multiple services across many computers. Some of these reasons include:
1. Service Failure
Services can fail for various reasons, including hardware failure, software bugs, and configuration errors. When a service fails, the system can become unavailable or unstable. Restarting the service can sometimes fix the problem.
2. Software Updates
When you install software updates on a computer or server, you might need to restart services to ensure that the changes take effect.
3. Performance Issues
Services can sometimes consume too much memory or CPU, causing performance issues. Restarting the service can help free up resources and improve system performance.
Understanding the PowerShell Restart-Service cmdlet
The Restart-Service
cmdlet is used to restart services in PowerShell. This command can be used to restart services on the local computer or remote computers. Here are the important parameters of the Restart-Service command:
Parameter | Description | Example Usage |
---|---|---|
-Name | Specifies the name of the service you want to restart. | Restart-Service -Name "ServiceName" |
-DisplayName | Allows you to specify the service by its display name instead of its service name. | Restart-Service -DisplayName "Display Name" |
-Force | Restarts the service and any dependent services. | Restart-Service -Name "ServiceName" -Force |
-Exclude | Excludes specified services from being restarted, useful when using wildcard characters. | Restart-Service -Name * -Exclude "ServiceToExclude" |
-Include | The -Include parameter of the Restart-Service cmdlet is used to specify an array of service names to include in the operation. | Restart-Service -Name 'win*' -Include 'WinRM','Winmgmt' |
More info on the syntax and parameters for Restart-Service is here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-service?view=powershell-7.3
Restarting a Service via the Services Console
To restart a service via the Services Console, follow these steps:
- Open the Services console by searching for “Services” in the Windows start menu. You can do this by pressing the Windows key + R and typing “services.msc” in the Run dialog box.
- Locate the service you want to restart in the list of services.
- Right-click on the service and select “Restart” from the context menu. The service status will change to “Stopping” and then to “Starting” as the service restarts.
- Alternatively, you can stop the service by selecting “Stop” from the context menu, and then start it again by selecting “Start”.
When the restart is complete, the service status will display as “Running”. If you prefer to use the command line, you can use the “net stop” and “net start” commands to stop and start a service, respectively. You can also restart a service through Task Manager >> Services.
How to Restart a Service using PowerShell?
PowerShell makes restarting services quick and easy. The Restart-Service cmdlet allows you to restart services by name, display name, or object reference. To restart a service on the local computer, open PowerShell and type the following syntax:
Restart-Service -Name <ServiceName>
Replace <ServiceName>
with the name of the service you want to restart. For example, to restart the Print Spooler service, type:
Restart-Service -Name Spooler -PassThru
This will restart the Print Spooler service on the local computer. Here, -Name
specifies the name of the service that you want to restart. The service name is different from the display name, which is generally what you see in the services manager. To specify a service by its display name, use the -displayname
parameter. The -PassThru
parameter waits until the service is restarted and displays its running status.
Alternatively, You can use Get-Service to find the service name and pipeline the service object to the Restart-Service cmdlet.
Get-Service Spooler | Restart-Service
The Restart-Service automatically determines dependent services and restarts them as well. You need administrator access to be able to restart services, BTW! Otherwise, you’ll face: “Restart-Service : Service ‘Service Name’ cannot be stopped due to the following error: Cannot open <Service-Name> on computer ‘.’.” error.
Please note, The services can be started only if the start type is set to automatic, manual, and automatic (delayed start). You can’t start the service if it’s disabled. You can change the startup type of the service using:
Set-Service -Name "Service Name" -StartupType Automatic
Use -Force Parameter to Immediately Restart a Service
By default, Restart-Service
sends a stop message and waits for the service to enter the stopped state before restarting. This helps ensure proper restart but can take more time. You can use the -Force
parameter to immediately restart the service after sending a stop message:
Restart-Service -Name Spooler -Force
This bypasses the waiting and restarts the service as soon as possible. Use -Force
when you need to quickly restart a service.
How to Identify the Name of the Service You Want to Restart?
Not sure about the name of the service you need to restart? Use the Get-Service
cmdlet to find it. This will return an object representing each service, helping you identify them by their service name or display name of the services. Just open the PowerShell console and type Get-Service to list all services:
Apart from the Name column from the above cmdlet, You can also get the service name from the services console:
You can use wildcards to get multiple services. Say, for example, Let’s find all the services on the computer (starting with “Samsung Mobile”) and restart any that are stopped:
Get-service -DisplayName "Samsung*" | where {$_.Status -eq "Stopped"} | Restart-Service
You can also filter services by its other properties using the Where-object cmdlet:
Get-Service | Where-Object {$_.Name -like "*print*"} | Restart-Service
Stop and Start Windows Service using Powershell
Here is how to stop and start Windows services using PowerShell:
Stop a Windows Service
To stop a service, you use the Stop-Service cmdlet. Specify the service name or display name using the -Name parameter:
Stop-Service -Name "Spooler"
This sends a stop message to the Windows Service controller. You can also pipe the Get-Service to stop or start a service. E.g.,
Get-Service -Name 'ServiceName' | Stop-Service
Alternatively, You can use the InputObject parameter as:
#Get the specific service
$Service = Get-service -DisplayName "Print Spooler"
#Stop the service
Stop-Service -InputObject $Service -Verbose
Start a Windows Service
To start a service, you use the Start-Service cmdlet, again specifying the service name with -Name:
Start-Service -Name "Spooler"
This sends a start message to restart the service. You can check the status using Get-Service to confirm it stopped:
Get-Service -Name Spooler
Stop/Start Multiple Services
You can target multiple services using an array:
$services = @("Spooler", "Wsearch")
Restart-Service -Name $services
This provides an easy way to stop and start multiple Windows services with PowerShell.
Restarting Services with PowerShell on Remote Computer
To restart a service on a remote computer, use the -ComputerName parameter. Here is an example:
#Get the service in Remote computer
$Service = Get-service -DisplayName "Print Spooler" -ComputerName "FileServer"
#Restart the service
Restart-Service -InputObject $Service -Verbose
Alternatively, You can remotely restart a service with Invoke-Command
cmdlet. For example, to restart the Print Spooler service on a remote computer named RemoteComputer
, type the following command:
#Enter into a new session
$Session = New-PSsession -Computername "RemoteComputer"
#Execute PowerShell script remotely
Invoke-Command -Session $Session -ScriptBlock {Restart-Service "webClient"}
#Disconnect the Session
Remove-PSSession $Session
You will need administrative credentials on the remote computer to restart services.
Remote PowerShell Execution – Requirements
Please note, that the PowerShell remoting has below requirements:
- WinRM: The Windows Remote Management (WinRM) service must be running on the remote computer and properly configured to allow remote connections. You can configure it by running
winrm quickconfig
on the remote computer. - Firewall: Make sure the firewall allows for WinRM traffic (usually HTTP over port 5985 or HTTPS over port 5986).
- Permissions: Make sure you have the necessary permission to run commands on the remote computer. You might need administrative privileges, depending on what you’re trying to do.
- Network: Both the local and remote computers must be reachable over the network.
PowerShell Script to Restart Services on Multiple Computers
You can also use a PowerShell script to restart services on multiple computers. The script can be used to restart services on a list of computers or on all computers in a domain. Here is an example script that restarts the Print Spooler service on a list of computers:
$Computers = Get-Content "C:\temp\computers.txt"
$ServiceName = "Spooler"
foreach ($Computer in $Computers)
{
Write-Host "Restarting service on $Computer"
Invoke-Command -ComputerName 'RemoteServerName' -ScriptBlock {
Restart-Service -Name $ServiceName -Force
}
}
In this script, the Get-Content
cmdlet reads a list of computer names from a file, and the foreach
loop restarts the Print Spooler service on each computer.
Restarting IIS Services with PowerShell
In addition to Windows services, you can also use PowerShell to restart IIS services. Restarting IIS (Internet Information Services) services can be a common administrative task when managing a web server. You can use PowerShell to accomplish this task.
To restart the World Wide Web Publishing Service (W3SVC) on the local computer, type the following command:
Restart-Service -Name W3SVC
To restart the W3SVC service on a remote computer, You can use Invoke-Command to run a script block on a remote computer. Here’s how you can restart the IIS services:
Invoke-Command -ComputerName 'RemoteServerName' -ScriptBlock {
iisreset
} -Credential (Get-Credential)
Instead of IISReset in the script block, you can also use: “Restart-Service ‘W3SVC'”. You can specify the credentials to connect by adding the -Credential parameter to Invoke-Command or Enter-PSSession.
Restarting a Specific Website Remotely
If you want to restart a specific website on a remote computer, you can use Invoke-Command to run the necessary cmdlets.
Invoke-Command -ComputerName 'RemoteServerName' -ScriptBlock {
Import-Module WebAdministration
Stop-WebSite -Name 'Default Web Site'
Start-WebSite -Name 'Default Web Site'
}
How to Restart Multiple Services using PowerShell?
While individual cmdlets are powerful, a PowerShell script can help you perform more complex tasks like restarting multiple services in a specific order. You can create a script to restart services based on custom conditions and even apply it to multiple computers.
Having to restart services one by one can become tedious. Luckily, Restart-Service makes it easy to restart multiple services at the same time.
You have a few different options for restarting multiple services:
- Use wildcards: The
-Name
parameter accepts wildcard characters to target multiple services. For example:
Restart-Service -Name *print*
Input object: You can pipe a collection of services from Get-Service
to target multiple services:
Get-Service | Where-Object {$_.Name -like "*print*"} | Restart-Service
Specify service names: You can provide a comma-separated list of services:
Restart-Service Spooler,Wsearch
This provides flexibility for restarting groups of related services.
PowerShell to Restart multiple services if they are in the stopped state
To restart multiple services only if they are stopped, you can use PowerShell to first check the status of each service. If a service is stopped, you can restart it. Here’s how you can do this:
# Define an array of service names you want to restart
$serviceNames = @('seclogon', 'WAS', 'TermService')
# Loop through each service name in the array
Try {
ForEach ($ServiceName in $serviceNames) {
# Retrieve the current status of the service
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
# Check if the service exists
if ($service -ne $null) {
# Check if the service is stopped
if ($service.Status -eq 'Stopped') {
# Restart the service
Restart-Service -Name $serviceName -ErrorAction Stop
Write-Host -f Yellow "Restared the service $serviceName because it was stopped"
} else {
# Output the current status of the service
Write-Host -f Green "Service $serviceName is currently $($service.Status)"
}
} else {
Write-Host -f Yellow "Service $ServiceName does not exist!"
}
}
}
catch {
Write-Host -f Red "An error occurred: $_"
}
How to Schedule Restarting Services?
You can easily create PowerShell scripts to automate restarting Windows services. This allows you to schedule restart tasks using tools like Windows Task Scheduler.
Here is an example script that restarts two services every day at 1 AM:
# Daily service restart script
$services = @("Spooler","Wsearch")
foreach ($service in $services) {
Restart-Service -Name $service -Force
}
Save this script as Restart-Services.ps1
and call it from Task Scheduler. This automates the task!
Best Practices for Restarting Services with PowerShell
When restarting services with PowerShell, it is important to follow best practices to ensure a smooth and successful process. Here are some tips:
1. Verify Service Name and Status
Before restarting a service, verify the service name to ensure that you are restarting the correct service. Check the current status of the service using Get-Service
. Make sure the service is not already stopped before trying to restart it.
2. Stop Dependent Services
If a service has dependent services, stop the dependent services before restarting the main service.
3. Check the Service Status
After restarting a service, check the service status to ensure that it is running.
4. Test Service Functionality
Test the service functionality after restarting to ensure that it is working as expected.
5. Error Handling
In production scripts, it’s good practice to include error handling, such as try/catch blocks, especially when performing actions that could affect system stability or availability. E.g.
try {
Restart-Service -Name 'ServiceName' -ErrorAction Stop
}
catch {
Write-Host "An error occurred: $_"
}
Troubleshooting Common Issues with Service Restart
Sometimes, restarting services with PowerShell can encounter errors. Here are some common issues and their solutions:
- Access Denied – If you get an access denied error when restarting a service, ensure you have administrative credentials on the local or remote computer.
- Service Not Found: If you get a service not found error, verify that you have entered the correct service name.
- Service Dependencies: If a service has dependent services, stop the dependent services before restarting the main service.
- PowerShell Version: Ensure you are using the latest version of PowerShell to avoid compatibility issues.
Tail: How to Create a New Service and delete existing services using PowerShell?
PowerShell provides some more cmdlets for efficient service management. You can provision new services and remove existing services through PowerShell. Here is a quick example:
#Create a New service to Execute PowerShell script
New-Service -name SiteBackupScript -binaryPathName "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -File C:\Scripts\BackupScript.ps1"
#Delete existing service
(Get-WmiObject win32_service -Filter "name='SiteBackupScript'").delete()
#From PowerShell 6 onwards, You can use:
Remove-Service -Name "SiteBackupScript"
Conclusion and Next Steps
Restarting Windows services is a common task required for managing servers and workstations. However, doing this manually through the Services console is slow and tedious. The Restart-Service cmdlet in PowerShell provides a quick, flexible method for restarting services. In this comprehensive guide, we have explored how to restart services with PowerShell. We have covered various scenarios, including restarting services on the local and remote computers, using scripts to restart services, and restarting IIS services. We have also discussed best practices and troubleshooting common issues. By following these guidelines, you can master the art of service restarting with PowerShell and save time and effort in managing your systems.
Now that you have learned how to restart services with PowerShell. Next time, you need to restart a service, turn to PowerShell, and Restart-Service to save time and effort. Happy Scripting!