-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFind-Process.ps1
More file actions
65 lines (50 loc) · 1.41 KB
/
Copy pathFind-Process.ps1
File metadata and controls
65 lines (50 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.SYNOPSIS
Finds processes by name and optionally stops them.
.DESCRIPTION
Searches running processes by name using exact or regex matching.
By default, returns matching process objects. If -Stop is specified, the script
can stop matches with optional confirmation and force mode.
.PARAMETER Pattern
Process name pattern to search.
.PARAMETER Stop
Stops matching processes instead of returning them.
.PARAMETER Force
Stops processes without confirmation and passes -Force to Stop-Process.
.PARAMETER Exact
Matches process names with exact equality instead of regex.
.EXAMPLE
./Find-Process.ps1 -Pattern "code"
Lists processes with names that match "code".
.EXAMPLE
./Find-Process.ps1 -Pattern "node" -Stop -Force
Stops matching node processes without confirmation.
#>
[CmdletBinding()]
param (
[Parameter(Position = 0)]
[Alias('Pettern')]
[string]$Pattern = '',
[switch]$Stop = $false,
[switch]$Force = $false,
[switch]$Exact = $false
)
$processList = if ($Exact) {
Get-Process | Where-Object Name -eq $Pattern
}
else {
Get-Process | Where-Object Name -imatch $Pattern
}
if (-not $Stop) {
return $processList
}
if (-not $Force) {
$answer = Read-Host 'Do you want to stop them? (y/n)'
if ($answer -ne 'y') {
return
}
$processList | Stop-Process
return
}
Write-Host 'Stopping the following processes:' -ForegroundColor Yellow
$processList | Stop-Process -Force