Services: `Get-Service` and `Restart-Service`
A service is a program Windows runs with no window, such as the Print Spooler: Get-Service shows each one's Status and, with Select-Object, its StartType, all without changing anything, while Restart-Service, Stop-Service, Start-Service and Set-Service change them and need an administrator window, after a rehearsal with -WhatIf.
Journeyman · PowerShell · Operating System · Updated
After this lesson you can
- Read a Windows service's Status and StartType with Get-Service, by its Name or DisplayName, without changing anything
- Choose between Restart-Service, Stop-Service, Start-Service and Set-Service for a job, rehearsing with -WhatIf in an administrator window
Key takeaways
- A service is a program Windows runs in the background with no window, such as the Print Spooler or Windows Audio. Each has a short Name and a longer DisplayName.
- Get-Service shows each one's Status (Running or Stopped). Select-Object adds its StartType: Automatic, Manual or Disabled. Looking changes nothing.
- Restart-Service, Stop-Service and Start-Service change a service, and Set-Service -StartupType changes when it starts. They need a window running as administrator.
- Rehearse with -WhatIf first. Restart-Service -Name Spooler is what a help desk tries first when print jobs are stuck.
You send a page to the printer and nothing happens. The document sits in the queue, and the next one queues up behind it. Someone from IT comes over, types one command, and the queue starts moving. That command restarted the Print Spooler, the part of Windows that hands documents to printers. It has no window, no icon and no button: it's a service. Knowing how to look at services, and when to restart one, is a big part of every help-desk and Windows administrator job, and it fixes the family computer more often than you'd think.
Programs with no window
A building's heating runs all day inside the walls. Nobody opens it to use it; you notice it only when it stops. Windows has dozens of programs like that, each doing one job in the background: printing, sound, the clock, updates. A program Windows runs that way is a service. Get-Service lists them. Here are three every Windows PC has:
Get-Service -Name Spooler, Audiosrv, W32Time
Status Name DisplayName
------ ---- -----------
Running Audiosrv Windows Audio
Running Spooler Print Spooler
Running W32Time Windows Time
Each service has a short Name, the one commands use, and a longer DisplayName for people. Status says whether it's running right now. -DisplayName searches the other column, wildcards and all: Get-Service -DisplayName 'Print*' finds the Print Spooler by the name you know.
A service is a process too, so Get-Process finds it. The Print Spooler's program is spoolsv:
Get-Process -Name spoolsv
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
34 22.59 39.77 0.00 7068 0 spoolsv
Many smaller services share one svchost process, the Service Host from What's Running: Get-Process and Stop-Process. Session 0, in the SI column, is where Windows runs its services, apart from anyone's sign-in.
When does it start? StartType
Each service also has a startup type: when Windows starts it. It isn't in the table, so ask for it with Select-Object:
Get-Service -Name Spooler, Audiosrv, W32Time | Select-Object Name, Status, StartType
Name Status StartType
---- ------ ---------
Audiosrv Running Automatic
Spooler Running Automatic
W32Time Running Automatic
Automatic starts with Windows. Manual starts only when something asks for it, so a Stopped service set to Manual is usually fine. Disabled never starts. On your PC, Windows Time may be Manual: it starts when Windows wants to check the clock. A service that's Stopped but Automatic, after the PC has been on for a while, is worth a second look.
To see the whole PC at a glance, count services by status:
Get-Service | Group-Object Status -NoElement
Count Name
----- ----
177 Stopped
178 Running
Everything in this section only looks, so it's safe to run anywhere, as often as you like. The mouse's door is the Services app: run services.msc, or search for Services in the Start menu. Its Status and Startup Type columns are the same facts.
Restarting a service
Restarting a service stops it and starts it again, like turning a stuck machine off and on. It changes the computer, so it needs a PowerShell window running as administrator (Starting Programs: Start-Process showed how to open one). Rehearse first; -WhatIf changes nothing, so it runs in any window:
Restart-Service -Name Spooler -WhatIf
What if: Performing the operation "Restart-Service" on target "Print Spooler (Spooler)".
The target is the service you meant. Then, in an administrator window:
Restart-Service -Name Spooler
It prints nothing when it works. While a slow service is stopping, it prints a yellow warning that it's waiting, then carries on. In an ordinary window, it stops with an error saying the service can't be opened: that's Windows refusing a change without administrator rights, and the service keeps running. After a restart, Get-Service -Name Spooler shows it Running again.
Services lean on each other: Get-Service -Name Spooler -RequiredServices lists the ones the Print Spooler needs. Stopping a service can affect the ones that depend on it, and stopping one that Windows needs can make the PC misbehave until it starts again. Restart the one service you came for, with a reason.
The other three
| Command | What it does |
|---|---|
Stop-Service -Name Spooler |
stops it, and it stays stopped |
Start-Service -Name Spooler |
starts a stopped service |
Set-Service -Name Spooler -StartupType Automatic |
changes when it starts: Automatic, Manual or Disabled |
All three change the computer, all three need an administrator window, and all three take -WhatIf. Set-Service is the one to be most careful with, because a change lasts after a restart of the PC: write down the startup type you found before you change it.
On a Mac or Linux, Get-Service and the rest aren't there: services work differently on those systems. Windows PowerShell 5.1 has the same commands.
Check yourself
2 questions to try on the lesson page.
So what
Most of a Windows administrator's work with services is looking: Get-Service shows what's running and how it starts, and when one is stuck, one rehearsed Restart-Service in an administrator window puts it right.