Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Service management (Get/Start/Stop-Service)
📖 Scenario: You are managing services on a Windows computer. Sometimes services need to be checked if they are running, started if they are stopped, or stopped if they are running. This helps keep your computer working smoothly.
🎯 Goal: You will write a PowerShell script that checks the status of a specific service, starts it if it is stopped, and then stops it if it is running. This will teach you how to use basic service management commands.
📋 What You'll Learn
Use Get-Service to check the status of a service
Use Start-Service to start a stopped service
Use Stop-Service to stop a running service
Work with the service named Spooler
💡 Why This Matters
🌍 Real World
System administrators often need to manage Windows services to keep computers running smoothly. Automating service checks and controls saves time and avoids manual errors.
💼 Career
Knowing how to use PowerShell to manage services is a key skill for IT support, system administration, and automation roles.
Progress0 / 4 steps
1
Create a variable with the service name
Create a variable called serviceName and set it to the string "Spooler".
PowerShell
Hint
The service name is a string. Use double quotes around Spooler.
2
Get the service status
Use Get-Service with the variable serviceName and save the result in a variable called service.
PowerShell
Hint
Use Get-Service -Name $serviceName and assign it to $service.
3
Start the service if it is stopped
Write an if statement to check if $service.Status equals "Stopped". If yes, use Start-Service with $serviceName to start it.
PowerShell
Hint
Use if ($service.Status -eq "Stopped") { Start-Service -Name $serviceName }.
4
Stop the service if it is running and print status
Write an if statement to check if $service.Status equals "Running". If yes, use Stop-Service with $serviceName to stop it. Then print the message "Service stopped".
PowerShell
Hint
Use if ($service.Status -eq "Running") { Stop-Service -Name $serviceName; Write-Output "Service stopped" }.
Practice
(1/5)
1. What does the PowerShell command Get-Service do?
easy
A. It shows the status of services on the computer.
B. It starts a stopped service.
C. It stops a running service.
D. It deletes a service from the system.
Solution
Step 1: Understand the purpose of Get-Service
The Get-Service command lists services and their current status (running or stopped).
Step 2: Compare with other commands
Start-Service and Stop-Service change service states, but Get-Service only shows status.
Final Answer:
It shows the status of services on the computer. -> Option A
Quick 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
A. Start-Service -Name Spooler
B. Get-Service -Start Spooler
C. Stop-Service -Name Spooler
D. Start-Service Spooler -Force
Solution
Step 1: Identify the correct cmdlet for starting a service
Start-Service is used to start services, and it accepts the -Name parameter to specify the service.
Step 2: Check the syntax correctness
Start-Service -Name Spooler is 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 A
Quick 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
A. Starts the W32Time service.
B. Displays the status (Running or Stopped) of the W32Time service.
C. Stops the W32Time service.
D. Shows an error because Select-Object cannot be used here.
Solution
Step 1: Understand Get-Service with Select-Object
Get-Service -Name W32Time fetches the service object, and piping it to Select-Object -Property Status extracts 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 B
Quick 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:
Stop-Service Spooler
But it fails with an error. What is the likely cause?
medium
A. You need to specify the parameter name: -Name before the service name.
B. The service is already stopped, so Stop-Service cannot run.
C. Stop-Service cannot stop the Spooler service.
D. You must run PowerShell as Administrator to stop services.
Solution
Step 1: Check command syntax
Stop-Service Spooler is valid because the parameter -Name is 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 D
Quick 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
A.
Start-Service -Name W32Time -IfStopped
B.
if (Get-Service W32Time -Status 'Stopped') { Start-Service W32Time }