mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-22 07:53:21 +01:00
122 lines
3.2 KiB
Markdown
122 lines
3.2 KiB
Markdown
The *close-program.ps1* Script
|
|
===========================
|
|
|
|
This PowerShell script closes a program's processes gracefully.
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
/home/markus/Repos/PowerShell/scripts/close-program.ps1 [[-fullProgramName] <String>] [[-programName] <String>] [[-programAliasName] <String>] [<CommonParameters>]
|
|
|
|
-fullProgramName <String>
|
|
Specifies the full program name
|
|
|
|
Required? false
|
|
Position? 1
|
|
Default value
|
|
Accept pipeline input? false
|
|
Accept wildcard characters? false
|
|
|
|
-programName <String>
|
|
Specifies the program name
|
|
|
|
Required? false
|
|
Position? 2
|
|
Default value
|
|
Accept pipeline input? false
|
|
Accept wildcard characters? false
|
|
|
|
-programAliasName <String>
|
|
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.
|
|
```
|
|
|
|
Example
|
|
-------
|
|
```powershell
|
|
PS> ./close-program "Google Chrome" "chrome.exe"
|
|
|
|
```
|
|
|
|
Notes
|
|
-----
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
Related Links
|
|
-------------
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
Script Content
|
|
--------------
|
|
```powershell
|
|
<#
|
|
.SYNOPSIS
|
|
Closes a program
|
|
.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 the 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."
|
|
} else {
|
|
"✅ $fullProgramName closed and $($processes.Count) processes stopped."
|
|
}
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|
|
```
|
|
|
|
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:52)*
|