2024-10-01 15:11:03 +02:00
|
|
|
|
<#
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.SYNOPSIS
|
2024-05-29 18:28:42 +02:00
|
|
|
|
Closes a program
|
2021-10-04 21:29:23 +02:00
|
|
|
|
.DESCRIPTION
|
2022-01-29 12:47:46 +01:00
|
|
|
|
This PowerShell script closes a program's processes gracefully.
|
2024-05-29 18:28:42 +02:00
|
|
|
|
.PARAMETER fullProgramName
|
2021-10-16 16:50:10 +02:00
|
|
|
|
Specifies the full program name
|
2024-05-29 18:28:42 +02:00
|
|
|
|
.PARAMETER programName
|
2021-10-16 16:50:10 +02:00
|
|
|
|
Specifies the program name
|
2024-05-29 18:28:42 +02:00
|
|
|
|
.PARAMETER programAliasName
|
2021-10-16 16:50:10 +02:00
|
|
|
|
Specifies the program alias name
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.EXAMPLE
|
2021-09-24 17:19:49 +02:00
|
|
|
|
PS> ./close-program "Google Chrome" "chrome.exe"
|
2021-07-13 21:10:02 +02:00
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
2022-01-29 12:47:46 +01:00
|
|
|
|
.NOTES
|
2022-09-06 21:42:04 +02:00
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
2020-12-29 15:14:21 +01:00
|
|
|
|
#>
|
2020-12-27 11:02:20 +01:00
|
|
|
|
|
2024-05-29 18:28:42 +02:00
|
|
|
|
param([string]$fullProgramName = "", [string]$programName = "", [string]$programAliasName = "")
|
2020-12-28 10:23:17 +01:00
|
|
|
|
|
2021-02-18 20:17:55 +01:00
|
|
|
|
try {
|
2024-05-29 18:28:42 +02:00
|
|
|
|
if ($programName -eq "") {
|
|
|
|
|
Get-Process | where-object {$_.mainWindowTitle} | format-table Id, Name, mainWindowtitle -AutoSize
|
|
|
|
|
$programName = Read-Host "Enter the program name"
|
2021-07-15 15:51:22 +02:00
|
|
|
|
}
|
2024-05-29 18:28:42 +02:00
|
|
|
|
if ($fullProgramName -eq "") {
|
|
|
|
|
$fullProgramName = $programName
|
2021-07-15 15:51:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 18:28:42 +02:00
|
|
|
|
$processes = Get-Process -name $programName -errorAction 'silentlycontinue'
|
|
|
|
|
if ($processes.Count -ne 0) {
|
|
|
|
|
foreach ($process in $processes) {
|
|
|
|
|
$process.CloseMainWindow() | Out-Null
|
2021-02-13 16:36:47 +01:00
|
|
|
|
}
|
2023-03-01 10:52:37 +01:00
|
|
|
|
Start-Sleep -milliseconds 100
|
2024-05-29 18:28:42 +02:00
|
|
|
|
Stop-Process -name $programName -force -errorAction 'silentlycontinue'
|
2021-02-13 16:36:47 +01:00
|
|
|
|
} else {
|
2024-05-29 18:28:42 +02:00
|
|
|
|
$processes = Get-Process -name $programAliasName -errorAction 'silentlycontinue'
|
|
|
|
|
if ($processes.Count -eq 0) {
|
|
|
|
|
throw "$fullProgramName isn't running"
|
2021-02-13 16:36:47 +01:00
|
|
|
|
}
|
2024-05-29 18:28:42 +02:00
|
|
|
|
foreach ($process in $processes) {
|
2021-02-13 16:36:47 +01:00
|
|
|
|
$_.CloseMainWindow() | Out-Null
|
|
|
|
|
}
|
2023-03-01 10:52:37 +01:00
|
|
|
|
Start-Sleep -milliseconds 100
|
2024-05-29 18:28:42 +02:00
|
|
|
|
Stop-Process -name $programName -force -errorAction 'silentlycontinue'
|
2020-12-27 11:02:20 +01:00
|
|
|
|
}
|
2024-05-29 18:28:42 +02:00
|
|
|
|
if ($($processes.Count) -eq 1) {
|
2024-10-01 13:37:53 +02:00
|
|
|
|
"✅ $fullProgramName closed."
|
2021-10-21 15:52:34 +02:00
|
|
|
|
} else {
|
2024-10-01 13:37:53 +02:00
|
|
|
|
"✅ $fullProgramName closed and $($processes.Count) processes stopped."
|
2021-10-21 15:52:34 +02:00
|
|
|
|
}
|
2021-09-27 10:09:45 +02:00
|
|
|
|
exit 0 # success
|
2020-12-27 11:02:20 +01:00
|
|
|
|
} catch {
|
2022-04-13 12:06:32 +02:00
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
2020-12-27 11:02:20 +01:00
|
|
|
|
exit 1
|
|
|
|
|
}
|