Added close-edge.ps1 and close-file-explorer.ps1

This commit is contained in:
Markus Fleschutz 2020-12-28 09:23:17 +00:00
parent 4946b67d1f
commit b425425da0
4 changed files with 33 additions and 8 deletions

View File

@ -1,7 +1,7 @@
Collection of Useful PowerShell Scripts
=======================================
This repository contains 70+ useful and cross-platform PowerShell scripts - to be used by command-line interface (CLI), for remote control (RC), by context menu, by voice control, by automation software (e.g. Jenkins), or simply to learn PowerShell.
This repository contains 80+ useful and cross-platform PowerShell scripts - to be used by command-line interface (CLI), for remote control (RC), by context menu, by voice control, by automation software (e.g. Jenkins), or simply to learn PowerShell.
List of Scripts
---------------
@ -12,6 +12,8 @@ The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfol
* [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-chrome.ps1](Scripts/close-chrome.ps1) - closes Google Chrome gracefully
* [close-edge.ps1](Scripts/close-edge.ps1) - closes Microsoft Edge gracefully
* [close-file-explorer.ps1](Scripts/close-file-explorer.ps1) - closes Microsoft File Explorer gracefully
* [close-program.ps1](Scripts/close-program.ps1) - closes the given program gracefully
* [close-thunderbird.ps1](Scripts/close-thunderbird.ps1) - closes Mozilla Thunderbird gracefully
* [close-windows-terminal.ps1](Scripts/close-windows-terminal.ps1) - closes Windows Terminal gracefully

10
Scripts/close-edge.ps1 Executable file
View File

@ -0,0 +1,10 @@
#!/snap/bin/powershell
# Syntax: ./close-edge.ps1
# Description: closes Microsoft Edge gracefully
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
$ExitCode = close-program.ps1 "msedge"
exit $ExitCode

10
Scripts/close-file-explorer.ps1 Executable file
View File

@ -0,0 +1,10 @@
#!/snap/bin/powershell
# Syntax: ./close-file-explorer.ps1
# Description: closes Microsoft File Explorer gracefully
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
$ExitCode = close-program.ps1 "explorer"
exit $ExitCode

View File

@ -13,16 +13,19 @@ try {
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'
$NumProcKilled = 0
$List | Foreach-Object {
$Processes = Get-Process -name $ProgramName -erroraction 'silentlycontinue'
foreach ($Process in $Processes) {
$_.CloseMainWindow() | Out-Null
$NumProcKilled++
} | stop-process -force
if ($NumProcKilled -eq 0) {
}
start-sleep -milliseconds 100
Stop-Process -name $ProgramName -force -erroraction 'silentlycontinue'
$ProcessCount = $Processes.Count
if ($ProcessCount -eq 0) {
throw "program '$ProgramName' is not started yet"
}
write-output "OK - program '$ProgramName' has been closed ($NumProcKilled processes)."
write-output "OK - program '$ProgramName' with $ProcessCount process(es) has been closed."
exit 0
} catch {
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"