Added close-program.ps1 and close-windows-terminal.ps1

This commit is contained in:
Markus Fleschutz
2020-12-27 10:02:20 +00:00
parent 5c34261e5b
commit a7bbbb99a7
3 changed files with 46 additions and 0 deletions

29
Scripts/close-program.ps1 Executable file
View File

@ -0,0 +1,29 @@
#!/snap/bin/powershell
# Syntax: ./close-program.ps1 [<program-name>]
# 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
}

View File

@ -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
#}