diff --git a/README.md b/README.md index c18a4c7e..1d8e7c57 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Scripts/close-edge.ps1 b/Scripts/close-edge.ps1 new file mode 100755 index 00000000..6ee6c2ea --- /dev/null +++ b/Scripts/close-edge.ps1 @@ -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 diff --git a/Scripts/close-file-explorer.ps1 b/Scripts/close-file-explorer.ps1 new file mode 100755 index 00000000..17644409 --- /dev/null +++ b/Scripts/close-file-explorer.ps1 @@ -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 diff --git a/Scripts/close-program.ps1 b/Scripts/close-program.ps1 index 9f51e4f6..326d2f02 100755 --- a/Scripts/close-program.ps1 +++ b/Scripts/close-program.ps1 @@ -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])"