0
0
PowerShellscripting~30 mins

Service management (Get/Start/Stop-Service) in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use if ($service.Status -eq "Running") { Stop-Service -Name $serviceName; Write-Output "Service stopped" }.