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

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])"