Bird
Raised Fist0
PowerShellscripting~20 mins

Service management (Get/Start/Stop-Service) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Service Management Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell command?
Consider the following command run on a Windows machine with a service named 'wuauserv' (Windows Update service). What will be the output type of this command?
PowerShell
Get-Service -Name wuauserv | Select-Object -Property Status
AAn error saying the service 'wuauserv' does not exist
BThe full details of the 'wuauserv' service including display name and service type
CA list of all services on the machine
DA table showing the Status property with values like 'Running' or 'Stopped'
Attempts:
2 left
💡 Hint
Think about what Select-Object does when you specify a property.
💻 Command Output
intermediate
2:00remaining
What happens when you run this command on a stopped service?
You run the following command on a service named 'Spooler' which is currently stopped. What will be the output?
PowerShell
Start-Service -Name Spooler; Get-Service -Name Spooler | Select-Object -Property Status
AIt outputs nothing because Get-Service fails after Start-Service
BIt throws an error because the service is stopped
CIt starts the Spooler service and outputs 'Running' as the Status
DIt outputs 'Stopped' because Start-Service does not change the status
Attempts:
2 left
💡 Hint
Start-Service changes the service state if possible.
📝 Syntax
advanced
2:00remaining
Which command correctly stops a service and waits for it to stop?
You want to stop the service named 'bits' and ensure it is fully stopped before continuing. Which command is correct?
AStop-Service -Name bits -Force -PassThru | Wait-Service -Status Stopped
BStop-Service -Name bits; Get-Service -Name bits | Where-Object { $_.Status -eq 'Stopped' }
CStop-Service -Name bits -Force; Start-Sleep -Seconds 5
DStop-Service bits -Wait
Attempts:
2 left
💡 Hint
Look for a way to wait for the service status after stopping it.
🔧 Debug
advanced
2:00remaining
Why does this script fail to start the service?
This script is supposed to start the 'w32time' service but fails with an error. What is the cause?
PowerShell
if ((Get-Service -Name w32time).Status -eq 'Running') {
  Write-Output 'Service already running'
} else {
  Start-Service -Name w32time
  Write-Output 'Service started'
}
AThe script fails because it does not have administrator privileges to start the service
BThe service name 'w32time' is incorrect and causes an error
CThe script syntax is invalid due to missing braces
DGet-Service does not return a Status property
Attempts:
2 left
💡 Hint
Starting services usually requires special permissions.
🚀 Application
expert
3:00remaining
How to create a script that restarts a service only if it is running?
You want a PowerShell script that restarts the 'Spooler' service only if it is currently running. Which script correctly does this?
AGet-Service -Name Spooler | Where-Object { $_.Status -eq 'Running' } | Restart-Service
Bif ((Get-Service -Name Spooler).Status -eq 'Running') { Restart-Service -Name Spooler } else { Write-Output 'Service not running' }
CStop-Service -Name Spooler; Start-Service -Name Spooler
DRestart-Service -Name Spooler -Force
Attempts:
2 left
💡 Hint
Check the service status before restarting it.

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

  1. Step 1: Understand the purpose of Get-Service

    The Get-Service command lists services and their current status (running or stopped).
  2. Step 2: Compare with other commands

    Start-Service and Stop-Service change service states, but Get-Service only shows status.
  3. Final Answer:

    It shows the status of services on the computer. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Start-Service -Name Spooler -> Option A
  4. 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

  1. 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.
  2. Step 2: Determine the output

    The output will be a simple display showing the status of the W32Time service, such as Running or Stopped.
  3. Final Answer:

    Displays the status (Running or Stopped) of the W32Time service. -> Option B
  4. 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

  1. Step 1: Check command syntax

    Stop-Service Spooler is valid because the parameter -Name is positional and can be omitted.
  2. Step 2: Consider permissions

    Stopping services usually requires administrator rights. Without running PowerShell as admin, the command fails with an error.
  3. Final Answer:

    You must run PowerShell as Administrator to stop services. -> Option D
  4. 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 }
C.
$svc = Get-Service -Name W32Time
if ($svc.Status -eq 'Stopped') { Start-Service -Name W32Time }
D.
$svc = Get-Service W32Time
if ($svc.Status = 'Stopped') { Start-Service W32Time }

Solution

  1. Step 1: Retrieve the service object and check status

    Using $svc = Get-Service -Name W32Time stores the service object. Then $svc.Status -eq 'Stopped' correctly compares the status.
  2. Step 2: Start the service if stopped

    If the status is 'Stopped', Start-Service -Name W32Time starts the service.
  3. Final Answer:

    $svc = Get-Service -Name W32Time if ($svc.Status -eq 'Stopped') { Start-Service -Name W32Time } -> Option C
  4. Quick 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