Skip to content

Commit a7bbbb9

Browse files
committed
Added close-program.ps1 and close-windows-terminal.ps1
1 parent 5c34261 commit a7bbbb9

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfol
1111
* [check-mac-address.ps1](Scripts/check-mac-address.ps1) - checks the given MAC address for validity
1212
* [check-xml-file.ps1](Scripts/check-xml-file.ps1) - checks the given XML file for validity
1313
* [clone-repos.ps1](Scripts/clone-repos.ps1) - clones well-known Git repositories
14+
* [close-program.ps1](Scripts/close-program.ps1) - closes the given program gracefully
15+
* [close-windows-terminal.ps1](Scripts/close-windows-terminal.ps1) - closes Windows Terminal gracefully
1416
* [configure-git.ps1](Scripts/configure-git.ps1) - sets up the Git configuration
1517
* [csv-to-text.ps1](Scripts/csv-to-text.ps1) - converts the given CSV file into a text list
1618
* [display-time.ps1](Scripts/display-time.ps1) - displays the current time

Scripts/close-program.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/snap/bin/powershell
2+
3+
# Syntax: ./close-program.ps1 [<program-name>]
4+
# Description: closes the given program gracefully
5+
# Author: Markus Fleschutz
6+
# Source: github.com/fleschutz/PowerShell
7+
# License: CC0
8+
9+
param([string]$ProgramName)
10+
11+
try {
12+
if ($ProgramName -eq "" ) {
13+
Get-Process | Where-Object {$_.mainWindowTitle} | Format-Table Id, Name, mainWindowtitle -AutoSize
14+
$ProgramName = read-host "Enter program to close"
15+
}
16+
$List = Get-Process -name $ProgramName -erroraction 'silentlycontinue'
17+
$NumProc = 0
18+
$List | Foreach-Object { $NumProc++; $_.CloseMainWindow() | Out-Null } | stop-process -force
19+
if ($NumProc -eq 0) {
20+
write-error "ERROR: No processes for program '$ProgramName' found"
21+
exit 1
22+
} else {
23+
write-output "Done - $NumProc processes stopped."
24+
exit 0
25+
}
26+
} catch {
27+
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
28+
exit 1
29+
}

Scripts/close-windows-terminal.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/snap/bin/powershell
2+
3+
# Syntax: ./close-windows-terminal.ps1
4+
# Description: closes the Windows Terminal program gracefully
5+
# Author: Markus Fleschutz
6+
# Source: github.com/fleschutz/PowerShell
7+
# License: CC0
8+
9+
close-program.ps1 "WindowsTerminal"
10+
#try {
11+
# exit 0
12+
#} catch {
13+
# write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
14+
# exit 1
15+
#}

0 commit comments

Comments
 (0)