Managing services lets you control programs running in the background on your computer. You can check if a service is running, start it, or stop it easily.
Service management (Get/Start/Stop-Service) in PowerShell
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PowerShell
Get-Service [-Name] <string> Start-Service [-Name] <string> Stop-Service [-Name] <string>
Get-Service shows the status of a service.
Start-Service turns on a stopped service.
Stop-Service turns off a running service.
Examples
PowerShell
Get-Service -Name "wuauserv"PowerShell
Start-Service -Name "Spooler"PowerShell
Stop-Service -Name "Spooler"Sample Program
This script checks if the Print Spooler service is running. If it is stopped, it starts it. If it is running, it stops it. Then it shows the final status.
PowerShell
Write-Host "Checking Print Spooler service status..." $service = Get-Service -Name "Spooler" Write-Host "Status: $($service.Status)" if ($service.Status -eq 'Stopped') { Write-Host "Starting Print Spooler service..." Start-Service -Name "Spooler" Write-Host "Service started." } else { Write-Host "Stopping Print Spooler service..." Stop-Service -Name "Spooler" Write-Host "Service stopped." } $service = Get-Service -Name "Spooler" Write-Host "Final Status: $($service.Status)"
Important Notes
You need to run PowerShell as Administrator to start or stop some services.
Service names are not case sensitive but must be exact.
Use Get-Service without parameters to list all services.
Summary
Use Get-Service to see if a service is running or stopped.
Use Start-Service to turn on a stopped service.
Use Stop-Service to turn off a running service.
Practice
1. What does the PowerShell command
Get-Service do?easy
Solution
Step 1: Understand the purpose of Get-Service
TheGet-Servicecommand lists services and their current status (running or stopped).Step 2: Compare with other commands
Start-ServiceandStop-Servicechange service states, butGet-Serviceonly shows status.Final Answer:
It shows the status of services on the computer. -> Option AQuick Check:
Get-Service = Show service status [OK]
Hint: Get-Service always lists service status, not start or stop [OK]
Common Mistakes:
- Confusing Get-Service with Start-Service
- Thinking Get-Service stops services
- Assuming Get-Service deletes services
2. Which of the following is the correct syntax to start a service named 'Spooler' in PowerShell?
easy
Solution
Step 1: Identify the correct cmdlet for starting a service
Start-Serviceis used to start services, and it accepts the-Nameparameter to specify the service.Step 2: Check the syntax correctness
Start-Service -Name Spooleris the correct syntax. Get-Service -Start Spooler uses wrong cmdlet and parameter, Stop-Service -Name Spooler stops service, and Start-Service Spooler -Force uses an invalid parameter-Force.Final Answer:
Start-Service -Name Spooler -> Option AQuick Check:
Start-Service -Name ServiceName = Start service [OK]
Hint: Use Start-Service with -Name to start a service [OK]
Common Mistakes:
- Using Get-Service to start a service
- Using Stop-Service instead of Start-Service
- Adding invalid parameters like -Force
3. What will be the output of this PowerShell command?
Get-Service -Name W32Time | Select-Object -Property Status
medium
Solution
Step 1: Understand Get-Service with Select-Object
Get-Service -Name W32Timefetches the service object, and piping it toSelect-Object -Property Statusextracts only the status property.Step 2: Determine the output
The output will be a simple display showing the status of the W32Time service, such as Running or Stopped.Final Answer:
Displays the status (Running or Stopped) of the W32Time service. -> Option BQuick Check:
Get-Service + Select-Object = Show service status [OK]
Hint: Get-Service piped to Select-Object shows specific properties [OK]
Common Mistakes:
- Thinking it starts or stops the service
- Believing Select-Object causes an error here
- Confusing service name with command parameters
4. You run this command to stop the 'Spooler' service:
But it fails with an error. What is the likely cause?
Stop-Service Spooler
But it fails with an error. What is the likely cause?
medium
Solution
Step 1: Check command syntax
Stop-Service Spooleris valid because the parameter-Nameis positional and can be omitted.Step 2: Consider permissions
Stopping services usually requires administrator rights. Without running PowerShell as admin, the command fails with an error.Final Answer:
You must run PowerShell as Administrator to stop services. -> Option DQuick Check:
Stopping services needs admin rights [OK]
Hint: Run PowerShell as admin to stop services [OK]
Common Mistakes:
- Thinking -Name parameter is mandatory
- Assuming service must be running to stop
- Believing Spooler service cannot be stopped
5. You want to write a script that checks if the 'W32Time' service is stopped, and if so, starts it. Which script snippet correctly does this?
hard
Solution
Step 1: Retrieve the service object and check status
Using$svc = Get-Service -Name W32Timestores the service object. Then$svc.Status -eq 'Stopped'correctly compares the status.Step 2: Start the service if stopped
If the status is 'Stopped',Start-Service -Name W32Timestarts the service.Final Answer:
$svc = Get-Service -Name W32Time if ($svc.Status -eq 'Stopped') { Start-Service -Name W32Time } -> Option CQuick Check:
Check status with -eq, then start service [OK]
Hint: Use -eq to compare status, then Start-Service if stopped [OK]
Common Mistakes:
- Using single = instead of -eq for comparison
- Assuming Start-Service has -IfStopped parameter
- Trying to filter Get-Service with -Status parameter
