YouTip LogoYouTip

Cmdlet Process And Service Management

Process and service management is a very important part of system maintenance and automation 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 will explain in detail how to use PowerShell to manage processes and services, including basic operation commands, typical application scenarios and common questions, to help you improve system management efficiency. * * * ## 1. Process Management (Process) PowerShell uses .NET process objects to handle process operations. The commonly used Cmdlets include: | Cmdlet | Function | | --- | --- | | `Get-Process` | Get running processes | | `Stop-Process` | Stop 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 specific process (such as 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 Stop by process name: Stop-Process -Name notepad Stop by process ID: Stop-Process -Id 1234 Force terminate process (avoid interactive prompt): Stop-Process -Name notepad -Force * * * ### Starting Process: `Start-Process` Open notepad: Start-Process notepad Open PowerShell as administrator (requires current window to have permissions): Start-Process powershell -Verb RunAs Start web browser and visit URL: Start-Process "https://learn.microsoft.com" * * * ### Waiting for Process: `Wait-Process` Continue after process execution completes: Start-Process notepad Wait-Process -Name notepad Write-Output "Notepad has been closed" * * * ## 2. Service Management (Service) PowerShell's support for service management is very comprehensive. The commonly used Cmdlets are as follows: | Cmdlet | Function Description | | --- | --- | | `Get-Service` | View service status | | `Start-Service` | Start service | | `Stop-Service` | Stop service | | `Restart-Service` | Restart service | | `Set-Service` | Change service properties | ### Viewing Services: `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 Windows Time service: Start-Service -Name W32Time Stop service (may report error if permissions are insufficient): Stop-Service -Name W32Time ### Restarting Service: `Restart-Service` Restart-Service -Name W32Time ### Changing Service Settings: `Set-Service` Set service startup type to automatic: Set-Service -Name W32Time -StartupType Automatic Disable service: Set-Service -Name W32Time -StartupType Disabled * * * ## 3. Difference Between Service and Process | Comparison | Service | Process | | --- | --- | --- | | Startup Method | Auto start / Manual start / Disabled | Manually triggered by user or program | | Running Status | Runs continuously in background, usually no interface | Can interact in foreground, 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 an abnormally stopped service $svc = Get-Service -Name Spoolerif ($svc.Status -ne "Running") { Restart-Service -Name Spooler Write-Output "Service has been restarted"} ### 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 under the premise of confirming no risk. * * * ## 5. Common Questions and Precautions | Question | Reason and Suggestion | | --- | --- | | Cannot stop service | Administrator permission is required, it is recommended to "Run as administrator" | | Process name or service name spelling error | You can first use `Get-Process` or `Get-Service` to get the accurate name | | Service has stopped but still shows as Running | Some services have protection mechanisms, it is recommended to try restarting with `Restart-Service` | * * * ## 6. Summary PowerShell makes process and service management simple and efficient through a standardized command set: * Use `Get-*` to view information, `Start-*`/`Stop-*` to control behavior, `Set-*` to modify properties * Pipeline + conditional filtering + object operations, suitable for batch management tasks * Can be combined with scheduled tasks and monitoring scripts to build an automated operations system
← Powershell Pipeline FilteringPowershell Basic Syntax β†’