Cmdlet Process and Service Management
Managing processes and services is a very important part of system maintenance and automated operations.
PowerShell provides a concise yet powerful set of Cmdlets for viewing, starting, stopping, filtering, and controlling processes and services on local (or remote) computers.
This section will explain in detail how to use PowerShell to manage processes and services, including basic operation commands, typical application scenarios, and common issues, helping you improve system management efficiency.
1. Process Management (Process)
PowerShell uses.NETprocess objects to handle process operations. Common Cmdlets include:
| Cmdlet | Function |
|---|---|
Get-Process |
Get running processes |
Stop-Process |
Stop specified processes |
Start-Process |
Start a new process (program) |
Wait-Process |
Wait for a process to end |
View 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 them:
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
Stop-Process
Stop by process name:
Stop-Process -Name notepad
Stop by process ID:
Stop-Process -Id 1234
Forcefully terminate processes (to avoid interactive prompts):
Stop-Process -Name notepad -Force
Start processes:Start-Process
Open Notepad:
Start-Process notepad
Open PowerShell as administrator (the current window needs permission):
Start-Process powershell -Verb RunAs
Start a web browser and visit a URL:
Start-Process "https://learn.microsoft.com"
Wait for processes:Wait-Process
Continue after the process finishes execution:
Start-Process notepad Wait-Process -Name notepad Write-Output "记事本已关闭"
2. Service Management (Service)
PowerShell has comprehensive support for service management. Common Cmdlets are as follows:
| Cmdlet | Function Description |
|---|---|
Get-Service |
View service status |
Start-Service |
Start a service |
Stop-Service |
Stop a service |
Restart-Service |
Restart a service |
Set-Service |
Change service properties |
View services:Get-Service
View all services:
Get-Service
Filter services that are running:
Get-Service | Where-Object {$_.Status -eq "Running"}
View a specific service:
Get-Service -Name W32Time
Start and stop services
Start the Windows Time service:
Start-Service -Name W32Time
Stop a service (an error may occur if permissions are insufficient):
Stop-Service -Name W32Time
Restart services: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 a service:
Set-Service -Name W32Time -StartupType Disabled
3. Differences between Services and Processes
| Comparison Item | Service | Process |
|---|---|---|
| Startup Method | Automatic start / Manual start / Disabled | Triggered manually by user or by a program |
| Running State | Continuously runs in the background, usually without a UI | Can interact in the foreground, with a shorter lifecycle |
| Management Method | Get-Service, Start-Servicewait |
Get-Process, Start-Processwait |
| Example | Windows Update(wuauserv) | Notepad, chrome.exe, etc. |
4. Practical Examples
Example 1: Detect and restart a service that 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 higher than 50 (use with caution)
Get-Process | Where-Object { $_.CPU -gt 50 } | Stop-Process -Force
Note: Such operations must be performed in a test environment or when it is confirmed that there is no risk.
5. Common Issues and Precautions
| Issue | Reason and Suggested Explanation |
|---|---|
| Cannot stop the service | Administrator permissions are required. It is recommended to use "Run as administrator" |
| Incorrect spelling of process name or service name | You can first useGet-ProcessorGet-Serviceto get the exact name |
| The service has stopped but still shows as Running | Some services have protection mechanisms. It is recommended to useRestart-Serviceto try restarting |
6. Summary
PowerShell makes process and service management simple and efficient through its standardized command set:
- Use
Get-*to view information,Start-*/Stop-*to control behavior,Set-*to modify properties - Pipeline + conditional filtering + object operations are suitable for batch management tasks
- It can be combined with scheduled tasks and monitoring scripts to build an automated operations system