diff --git a/README.md b/README.md index b62af09f..cfc33a41 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfol * [check-mac-address.ps1](Scripts/check-mac-address.ps1) - checks the given MAC address for validity * [check-xml-file.ps1](Scripts/check-xml-file.ps1) - checks the given XML file for validity * [clone-repos.ps1](Scripts/clone-repos.ps1) - clones well-known Git repositories +* [close-program.ps1](Scripts/close-program.ps1) - closes the given program gracefully +* [close-windows-terminal.ps1](Scripts/close-windows-terminal.ps1) - closes Windows Terminal gracefully * [configure-git.ps1](Scripts/configure-git.ps1) - sets up the Git configuration * [csv-to-text.ps1](Scripts/csv-to-text.ps1) - converts the given CSV file into a text list * [display-time.ps1](Scripts/display-time.ps1) - displays the current time diff --git a/Scripts/close-program.ps1 b/Scripts/close-program.ps1 new file mode 100755 index 00000000..4a37aa6d --- /dev/null +++ b/Scripts/close-program.ps1 @@ -0,0 +1,29 @@ +#!/snap/bin/powershell + +# Syntax: ./close-program.ps1 [] +# Description: closes the given program gracefully +# Author: Markus Fleschutz +# Source: github.com/fleschutz/PowerShell +# License: CC0 + +param([string]$ProgramName) + +try { + if ($ProgramName -eq "" ) { + Get-Process | Where-Object {$_.mainWindowTitle} | Format-Table Id, Name, mainWindowtitle -AutoSize + $ProgramName = read-host "Enter program to close" + } + $List = Get-Process -name $ProgramName -erroraction 'silentlycontinue' + $NumProc = 0 + $List | Foreach-Object { $NumProc++; $_.CloseMainWindow() | Out-Null } | stop-process -force + if ($NumProc -eq 0) { + write-error "ERROR: No processes for program '$ProgramName' found" + exit 1 + } else { + write-output "Done - $NumProc processes stopped." + exit 0 + } +} catch { + write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 +} diff --git a/Scripts/close-windows-terminal.ps1 b/Scripts/close-windows-terminal.ps1 new file mode 100755 index 00000000..8466cf00 --- /dev/null +++ b/Scripts/close-windows-terminal.ps1 @@ -0,0 +1,15 @@ +#!/snap/bin/powershell + +# Syntax: ./close-windows-terminal.ps1 +# Description: closes the Windows Terminal program gracefully +# Author: Markus Fleschutz +# Source: github.com/fleschutz/PowerShell +# License: CC0 + +close-program.ps1 "WindowsTerminal" +#try { +# exit 0 +#} catch { +# write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" +# exit 1 +#}