mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-24 15:49:02 +01:00
50 lines
1.5 KiB
PowerShell
Executable File
50 lines
1.5 KiB
PowerShell
Executable File
<#
|
|
.SYNOPSIS
|
|
close-program.ps1 [<FullProgramName>] [<ProgramName>] [<ProgramAliasName>]
|
|
.DESCRIPTION
|
|
Closes the processes of the given program gracefully
|
|
.EXAMPLE
|
|
PS> ./close-program "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 {
|
|
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
|
|
exit 1
|
|
}
|