Cmdlet Process and Service Management
Managing processes and services is a very important part of system maintenance and automated operations.
PowerShell provides a set of concise and powerful Cmdlets for viewing, starting, stopping, filtering, and controlling processes and services on local (or remote) computers.
This section explains in detail how to use PowerShell to manage processes and services, including basic operation commands, typical application scenarios, and common problems, to help you improve system management efficiency.
I. Process management (Process)
PowerShell uses .NET process objects to handle process operations; common Cmdlets include:
| Cmdlet | Function |
|---|---|
Get-Process |
Get running processes |
Stop-Process |
Stop a specified process |
Start-Process |
Start a new process (program) |
Wait-Process |
Wait for a process to end |
Viewing processes:Get-Process
View all running processes:
Get-Process
View a specific process (e.g., notepad):
Get-Process -Name notepad
View multiple processes:
Get-Process -Name chrome, notepad
Get process details and sort:
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
根据进程名停止:
Stop-Process -Name notepad
根据进程 ID 停止:
Stop-Process -Id 1234
强制终止进程(避免交互提示):
Stop-Process -Name notepad -Force
启动进程:Start-Process
Open Notepad:
Start-Process notepad
Open PowerShell as administrator (requires the current window to have permission):
Start-Process powershell -Verb RunAs
Launch a web browser and visit the URL:
Start-Process "https://learn.microsoft.com"
Wait for process: Wait-Process
Continue after the process finishes executing:
Start-Process notepad
Wait-Process -Name notepad
Write-Output "记事本已关闭"
2. Service Management (Service)
PowerShell provides very complete support for service management. Commonly used Cmdlets are as follows:
Cmdlet
Description
Get-Service
View service status
Start-Service
Start service
Stop-Service
Stop service
Restart-Service
Restart service
Set-Service
Change service properties
View service: Get-Service
View all services:
Get-Service
Filter running services:
Get-Service | Where-Object {$_.Status -eq "Running"}
View specific service:
Get-Service -Name W32Time
Starting and stopping services
Start the Windows Time service:
Start-Service -Name W32Time
Stop service (an error may occur if permissions are insufficient):
Stop-Service -Name W32Time
Restart service: Restart-Service
Restart-Service -Name W32Time
Change service settings: Set-Service
Set the service startup type to automatic:
Set-Service -Name W32Time -StartupType Automatic
Disable the service:
Set-Service -Name W32Time -StartupType Disabled
3. Difference between services and processes
Comparison
Service (Service)
Process (Process)
Startup method
Automatic startup / Manual startup / Disabled
Manually by user or triggered by a program
Running status
Runs continuously in the background, usually without a UI
Can interact in the foreground, with a shorter lifecycle
Management method
Get-Service, Start-Service, etc.
Get-Process, Start-Process, etc.
Example
Windows Update (wuauserv)
Notepad, chrome.exe, etc.
4. Practical examples
Example 1: Detect and restart a service that has stopped abnormally
$svc = Get-Service -Name Spooler
if ($svc.Status -ne "Running") {
Restart-Service -Name Spooler
Write-Output "服务已重启"
}
Example 2: Close all processes with CPU usage above 50 (use with caution)
Get-Process | Where-Object { $_.CPU -gt 50 } | Stop-Process -Force
Note: Such operations must be performed only in a test environment or after confirming there is no risk.
5. Frequently asked questions and notes
Issue
Cause and recommendations
Cannot stop the service
Requires administrator privileges; it is recommended to use "Run as administrator"
Process name or service name spelling error
You can first use Get-Process or Get-Service to get the exact name
Service has stopped but still displays as Running
Some services have protection mechanisms; it is recommended to use Restart-Service to attempt a restart
6. Summary
PowerShell, through its standardized command set, makes the management of processes and services simple and efficient:
- Use
Get-* to view information, Start-*/Stop-* to control behavior, and Set-* to modify properties
- Pipes + conditional filtering + object manipulation, suitable for batch management tasks
- You can combine scheduled tasks and monitoring scripts to build an automated operations system