Renamed to open-notepad.exe and close-notepad.exe

This commit is contained in:
Markus Fleschutz 2024-05-29 18:28:42 +02:00
parent e447641434
commit 32347ab51d
3 changed files with 26 additions and 26 deletions

View File

@ -4,7 +4,7 @@
.DESCRIPTION
This PowerShell script closes the Notepad application gracefully.
.EXAMPLE
PS> ./close-note-pad.ps1
PS> ./close-notepad.ps1
.LINK
https://github.com/fleschutz/PowerShell
.NOTES

View File

@ -1,13 +1,13 @@
<#
.SYNOPSIS
Closes a program's processes
Closes a program
.DESCRIPTION
This PowerShell script closes a program's processes gracefully.
.PARAMETER FullProgramName
.PARAMETER fullProgramName
Specifies the full program name
.PARAMETER ProgramName
.PARAMETER programName
Specifies the program name
.PARAMETER ProgramAliasName
.PARAMETER programAliasName
Specifies the program alias name
.EXAMPLE
PS> ./close-program "Google Chrome" "chrome.exe"
@ -17,39 +17,39 @@
Author: Markus Fleschutz | License: CC0
#>
param([string]$FullProgramName = "", [string]$ProgramName = "", [string]$ProgramAliasName = "")
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 ($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
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
$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'
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"
$processes = Get-Process -name $programAliasName -errorAction 'silentlycontinue'
if ($processes.Count -eq 0) {
throw "$fullProgramName isn't running"
}
foreach ($Process in $Processes) {
foreach ($process in $processes) {
$_.CloseMainWindow() | Out-Null
}
Start-Sleep -milliseconds 100
stop-process -name $ProgramName -force -errorAction 'silentlycontinue'
Stop-Process -name $programName -force -errorAction 'silentlycontinue'
}
if ($($Processes.Count) -eq 1) {
"✔️ $FullProgramName closed, 1 process stopped"
if ($($processes.Count) -eq 1) {
"✔️ $fullProgramName closed."
} else {
"✔️ $FullProgramName closed, $($Processes.Count) processes stopped"
"✔️ $fullProgramName closed and $($processes.Count) processes stopped."
}
exit 0 # success
} catch {

View File

@ -2,9 +2,9 @@
.SYNOPSIS
Launches the Notepad app
.DESCRIPTION
This script launches the Notepad application.
This PowerShell script launches the Notepad application.
.EXAMPLE
PS> ./open-note-pad
PS> ./open-notepad.ps1
.LINK
https://github.com/fleschutz/PowerShell
.NOTES