forked from fleschutz/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclose-program.ps1
More file actions
executable file
·49 lines (45 loc) · 1.49 KB
/
Copy pathclose-program.ps1
File metadata and controls
executable file
·49 lines (45 loc) · 1.49 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
<#
.SYNOPSIS
close-program.ps1 [<full-program-name>] [<program-name>] [<program-alias-name>]
.DESCRIPTION
Closes the processes of the given program gracefully.
.EXAMPLE
PS> .\close-program.ps1 "Google Chrome" "chrome.exe"
.NOTES
Author: Markus Fleschutz · License: CC0
.LINK
https://github.com/fleschutz/PowerShell
#>
param([string]$FullProgramName = "", [string]$ProgramName = "", [string]$ProgramAliasName = "")
try {
if ($ProgramName -eq "") {
get-process | where-object {$_.mainWindowTitle} | format-table Id, Name, mainWindowtitle -AutoSize
$ProgramName = read-host "Enter program name"
}
if ($FullProgramName -eq "") {
$FullProgramName = $ProgramName
}
$Processes = get-process -name $ProgramName -errorAction 'silentlycontinue'
if ($Processes.Count -ne 0) {
foreach ($Process in $Processes) {
$Process.CloseMainWindow() | Out-Null
}
start-sleep -milliseconds 100
stop-process -name $ProgramName -force -errorAction 'silentlycontinue'
} else {
$Processes = get-process -name $ProgramAliasName -errorAction 'silentlycontinue'
if ($Processes.Count -eq 0) {
throw "$FullProgramName is not started yet"
}
foreach ($Process in $Processes) {
$_.CloseMainWindow() | Out-Null
}
start-sleep -milliseconds 100
stop-process -name $ProgramName -force -errorAction 'silentlycontinue'
}
"✔️ closed $FullProgramName, stopped $($Processes.Count) process(es)"
exit 0
} catch {
write-error "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}