Skip to content

Instantly share code, notes, and snippets.

@tostka
Last active March 28, 2018 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tostka/1b009a4003ba3a0862328600f576720a to your computer and use it in GitHub Desktop.
Save tostka/1b009a4003ba3a0862328600f576720a to your computer and use it in GitHub Desktop.
close-WinsAll.ps1 - Gracefully close (prompted saves) on all desktop windows, Control Panel Wins, and Explorer Wins
<# trimmed gist, see full close-WinsAll.ps1 for details #>
write-host -foregroundcolor green "***v ISSUING CLOSE ON _ALL_ DESKTOP WINDOWS! v***" ;
(get-process | ? { $_.mainwindowtitle -ne "" -and $_.processname -ne "powershell" } )| % {"proc:$($_.Name)" ; $_.CloseMainWindow() ; } ;
<# express equiv: KILL everything but ps, dead
(get-process | ? { $_.mainwindowtitle -ne "" -and $_.processname -ne "powershell" } )| stop-process ;
#>
# 10:07 AM 3/13/2018 #25 IE close: first do all IE, windows and blow past the 'Close All' prompts (only IE turns up in ShellApp):
$ShellApp = New-Object -ComObject Shell.Application ;
$SWinNames="Control Panel","Windows Explorer","Windows Internet Explorer","Internet Explorer" ;
write-host -foregroundcolor green "***v ISSUING CLOSE ON _ALL_ SHELL WINDOWS! v***" ;
foreach($SWinN in $SWinNames){
if($TWins=(New-Object -comObject Shell.Application).Windows() |?{$_.Name -eq $SWinN}){
switch -regex ($SWinN){
"Control\sPanel" { $wTag="CP" }
"Windows\sExplorer" { $wTag="Explr" }
"((Windows\s)*)Internet\sExplorer" { $wTag="IE" }
} ;
"`n==close $(($TWins|measure).Count) $($wTag) wins..." ;
$TWins| % {"$($wTag):$($_.LocationName)" ; $_.quit()} ;
} ;
} ; # loop-E
# 10:40 AM 3/20/2018 FINE, still have IE sitting there on close all tabs! KILL IT!
if($ieZombs=(get-process | ? { $_.processname -eq "iexplore" }) ){
write-host -foregroundcolor green "***v KILLING REMAINING IEXPLORE WINDOWS! v***" ;
$ieZombs| % {"proc:$($_.Name)" ; $_.Kill() ; } ;
} ;
# 10:23 AM 3/23/2018 and we're ending up with open Outlook windows as well. Rerun it!
if($othrZombs=(get-process | ? { $_.mainwindowtitle -ne "" -and $_.processname -ne "powershell" } ) ){
write-host -foregroundcolor green "***v RE-CLOSING REMAINING DESKTOP WINDOWS! v***" ;
$othrZombs | % {"proc:$($_.Name)" ; $_.CloseMainWindow() ; } ;
} ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment