mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-25 08:09:17 +01:00
59 lines
1.7 KiB
PowerShell
Executable File
59 lines
1.7 KiB
PowerShell
Executable File
<#
|
|
.SYNOPSIS
|
|
Closes a program's processes
|
|
.DESCRIPTION
|
|
This PowerShell script closes a program's processes gracefully.
|
|
.PARAMETER FullProgramName
|
|
Specifies the full program name
|
|
.PARAMETER ProgramName
|
|
Specifies the program name
|
|
.PARAMETER ProgramAliasName
|
|
Specifies the program alias name
|
|
.EXAMPLE
|
|
PS> ./close-program "Google Chrome" "chrome.exe"
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
.NOTES
|
|
Author: Markus Fleschutz | License: CC0
|
|
#>
|
|
|
|
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 isn't running"
|
|
}
|
|
foreach ($Process in $Processes) {
|
|
$_.CloseMainWindow() | Out-Null
|
|
}
|
|
start-sleep -milliseconds 100
|
|
stop-process -name $ProgramName -force -errorAction 'silentlycontinue'
|
|
}
|
|
if ($($Processes.Count) -eq 1) {
|
|
"✔️ $FullProgramName closed, 1 process stopped"
|
|
} else {
|
|
"✔️ $FullProgramName closed, $($Processes.Count) processes stopped"
|
|
}
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|