2024-11-08 12:38:20 +01:00
|
|
|
The *close-program.ps1* Script
|
|
|
|
===========================
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2022-02-10 09:01:07 +01:00
|
|
|
This PowerShell script closes a program's processes gracefully.
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
2024-11-08 12:35:11 +01:00
|
|
|
/home/markus/Repos/PowerShell/scripts/close-program.ps1 [[-fullProgramName] <String>] [[-programName] <String>] [[-programAliasName] <String>] [<CommonParameters>]
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
-fullProgramName <String>
|
2021-11-08 21:36:42 +01:00
|
|
|
Specifies the full program name
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
-programName <String>
|
2021-11-08 21:36:42 +01:00
|
|
|
Specifies the program name
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
-programAliasName <String>
|
2021-11-08 21:36:42 +01:00
|
|
|
Specifies the program alias name
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 3
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Example
|
|
|
|
-------
|
2021-11-08 21:36:42 +01:00
|
|
|
```powershell
|
|
|
|
PS> ./close-program "Google Chrome" "chrome.exe"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2022-11-17 19:46:02 +01:00
|
|
|
Author: Markus Fleschutz | License: CC0
|
2021-11-08 21:36:42 +01:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Related Links
|
|
|
|
-------------
|
2021-11-08 21:36:42 +01:00
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2022-11-17 20:05:34 +01:00
|
|
|
```powershell
|
2022-11-17 20:02:26 +01:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2024-08-15 09:51:46 +02:00
|
|
|
Closes a program
|
2022-11-17 20:02:26 +01:00
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script closes a program's processes gracefully.
|
2024-08-15 09:51:46 +02:00
|
|
|
.PARAMETER fullProgramName
|
2022-11-17 20:02:26 +01:00
|
|
|
Specifies the full program name
|
2024-08-15 09:51:46 +02:00
|
|
|
.PARAMETER programName
|
2022-11-17 20:02:26 +01:00
|
|
|
Specifies the program name
|
2024-08-15 09:51:46 +02:00
|
|
|
.PARAMETER programAliasName
|
2022-11-17 20:02:26 +01:00
|
|
|
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
|
|
|
|
#>
|
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
param([string]$fullProgramName = "", [string]$programName = "", [string]$programAliasName = "")
|
2022-11-17 20:02:26 +01:00
|
|
|
|
|
|
|
try {
|
2024-08-15 09:51:46 +02:00
|
|
|
if ($programName -eq "") {
|
|
|
|
Get-Process | where-object {$_.mainWindowTitle} | format-table Id, Name, mainWindowtitle -AutoSize
|
|
|
|
$programName = Read-Host "Enter the program name"
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2024-08-15 09:51:46 +02:00
|
|
|
if ($fullProgramName -eq "") {
|
|
|
|
$fullProgramName = $programName
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
$processes = Get-Process -name $programName -errorAction 'silentlycontinue'
|
|
|
|
if ($processes.Count -ne 0) {
|
|
|
|
foreach ($process in $processes) {
|
|
|
|
$process.CloseMainWindow() | Out-Null
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2023-05-26 12:20:18 +02:00
|
|
|
Start-Sleep -milliseconds 100
|
2024-08-15 09:51:46 +02:00
|
|
|
Stop-Process -name $programName -force -errorAction 'silentlycontinue'
|
2022-11-17 20:02:26 +01:00
|
|
|
} else {
|
2024-08-15 09:51:46 +02:00
|
|
|
$processes = Get-Process -name $programAliasName -errorAction 'silentlycontinue'
|
|
|
|
if ($processes.Count -eq 0) {
|
|
|
|
throw "$fullProgramName isn't running"
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2024-08-15 09:51:46 +02:00
|
|
|
foreach ($process in $processes) {
|
2022-11-17 20:02:26 +01:00
|
|
|
$_.CloseMainWindow() | Out-Null
|
|
|
|
}
|
2023-05-26 12:20:18 +02:00
|
|
|
Start-Sleep -milliseconds 100
|
2024-08-15 09:51:46 +02:00
|
|
|
Stop-Process -name $programName -force -errorAction 'silentlycontinue'
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
2024-08-15 09:51:46 +02:00
|
|
|
if ($($processes.Count) -eq 1) {
|
2024-11-08 12:35:11 +01:00
|
|
|
"✅ $fullProgramName closed."
|
2022-11-17 20:02:26 +01:00
|
|
|
} else {
|
2024-11-08 12:35:11 +01:00
|
|
|
"✅ $fullProgramName closed and $($processes.Count) processes stopped."
|
2022-11-17 20:02:26 +01:00
|
|
|
}
|
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
2022-11-17 20:05:34 +01:00
|
|
|
```
|
2022-11-17 20:02:26 +01:00
|
|
|
|
2024-11-20 11:52:20 +01:00
|
|
|
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:52)*
|