0
0
PowerShellscripting~10 mins

Service management (Get/Start/Stop-Service) in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Service management (Get/Start/Stop-Service)
Start
Get-Service
Check service status
Start-Service if stopped
Stop-Service if running
Confirm status
End
This flow shows how to get a service, check its status, then start or stop it accordingly.
Execution Sample
PowerShell
$serviceStatus = (Get-Service -Name "wuauserv").Status
if ($serviceStatus -eq 'Stopped') {
  Start-Service -Name "wuauserv"
} else {
  Stop-Service -Name "wuauserv"
}
Get-Service -Name "wuauserv"
This script checks the Windows Update service status and starts it if stopped, otherwise stops it.
Execution Table
StepActionService StatusConditionCommand ExecutedOutput
1$serviceStatus = (Get-Service -Name 'wuauserv').StatusRunningN/AGet-ServiceStatus: Running
2Check if status is 'Stopped'RunningFalseCondition checkSkip Start-Service
3Else branch: Stop-Service 'wuauserv'RunningN/AStop-ServiceService stopping...
4Confirm service statusStoppedN/AGet-ServiceStatus: Stopped
💡 Service status changed to Stopped, script ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
serviceStatusN/ARunningRunningRunningStopped
Key Moments - 2 Insights
Why does the script check the service status twice?
The first check (Step 1) gets the current status. The second check (Step 4) confirms the status after starting or stopping the service, ensuring the command worked.
What happens if the service is already running?
At Step 2, the condition is false, so the script runs Stop-Service (Step 3) to stop it, as shown in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the service status after Step 1?
AStopped
BPaused
CRunning
DStarting
💡 Hint
Check the 'Service Status' column in row for Step 1.
At which step does the script decide to stop the service?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Command Executed' column to find where Stop-Service runs.
If the service was initially stopped, which command would run?
AStart-Service
BStop-Service
CGet-Service only
DNo command
💡 Hint
Refer to the condition in Step 2 and the script logic in execution_sample.
Concept Snapshot
Get-Service retrieves service info.
Check service status property.
Start-Service starts a stopped service.
Stop-Service stops a running service.
Use conditions to decide which command to run.
Full Transcript
This example shows how to manage Windows services using PowerShell commands Get-Service, Start-Service, and Stop-Service. First, the script gets the status of the 'wuauserv' service. If the service is stopped, it starts it. Otherwise, it stops the service. The execution table traces each step, showing the service status and commands run. The variable tracker follows the service status change from running to stopped. Key moments clarify why the status is checked twice and what happens if the service is running. The quiz tests understanding of the service status at each step and the commands executed based on conditions.