0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Monitor Service Status Easily

Use Get-Service -Name 'ServiceName' | Select-Object Status in PowerShell to check a service's status, and wrap it in a script to monitor and report changes.
📋

Examples

InputServiceName = 'wuauserv'
OutputStatus ------ Running
InputServiceName = 'nonexistentservice'
OutputGet-Service : Cannot find any service with service name 'nonexistentservice'.
InputServiceName = 'Spooler'
OutputStatus ------ Running
🧠

How to Think About It

To monitor a service status, first get the current status using Get-Service. Then compare it over time or report it immediately. The script should handle cases where the service does not exist and output the status clearly.
📐

Algorithm

1
Get the service name as input.
2
Use a command to get the current status of the service.
3
Check if the service exists; if not, show an error message.
4
Display the current status of the service.
5
Optionally, repeat the check to monitor changes over time.
💻

Code

powershell
param([string]$ServiceName = 'wuauserv')
try {
    $service = Get-Service -Name $ServiceName -ErrorAction Stop
    Write-Output "Service '$ServiceName' status: $($service.Status)"
} catch {
    Write-Output "Service '$ServiceName' not found."
}
Output
Service 'wuauserv' status: Running
🔍

Dry Run

Let's trace checking the 'wuauserv' service status through the code

1

Get-Service command

Get-Service -Name 'wuauserv' returns a service object with Status = Running

2

Output status

Prints: Service 'wuauserv' status: Running

StepActionValue
1Get-Service -Name 'wuauserv'Status = Running
2Output status messageService 'wuauserv' status: Running
💡

Why This Works

Step 1: Get-Service fetches service info

The Get-Service cmdlet retrieves the service object including its current status.

Step 2: Error handling for missing service

Using -ErrorAction Stop and try/catch ensures the script handles cases where the service does not exist.

Step 3: Output the status

The script prints the service name and its current status in a clear message.

🔄

Alternative Approaches

Continuous monitoring with loop
powershell
param([string]$ServiceName = 'wuauserv')
while ($true) {
    try {
        $service = Get-Service -Name $ServiceName -ErrorAction Stop
        Write-Output "$(Get-Date): Service '$ServiceName' status: $($service.Status)"
    } catch {
        Write-Output "$(Get-Date): Service '$ServiceName' not found."
    }
    Start-Sleep -Seconds 10
}
This approach checks the service status every 10 seconds, useful for real-time monitoring but uses more resources.
Using WMI for service status
powershell
param([string]$ServiceName = 'wuauserv')
$service = Get-WmiObject -Class Win32_Service -Filter "Name='$ServiceName'"
if ($service) {
    Write-Output "Service '$ServiceName' status: $($service.State)"
} else {
    Write-Output "Service '$ServiceName' not found."
}
Using WMI provides more detailed info but is slightly slower and more complex.

Complexity: O(1) time, O(1) space

Time Complexity

Checking a single service status is a constant time operation since it queries one service.

Space Complexity

The script uses a fixed amount of memory to store the service object and output string.

Which Approach is Fastest?

Using Get-Service is faster and simpler than WMI queries; looping adds overhead but is needed for continuous monitoring.

ApproachTimeSpaceBest For
Get-Service single checkO(1)O(1)Quick one-time status check
Loop with Get-ServiceO(n) with n checksO(1)Continuous monitoring
WMI queryO(1) but slower than Get-ServiceO(1)Detailed info, less speed
💡
Use Get-Service -Name 'ServiceName' with try/catch to handle missing services gracefully.
⚠️
Beginners often forget to handle errors when the service name is incorrect, causing the script to fail.