When I have to restart explorer.exe, usually I have many folders open, which get closed in the process. So is there a way to reopen these folders automatically after restarting explorer?
Asked
Active
Viewed 1,005 times
4
goldnick7
- 372
- 2
- 14
3 Answers
5
This batch script:
- Makes a list of currently opened windows → saves it to a
txtfile - Restarts windows
explorer.exe - Re-opens folders from the
txtlist → deletestxtfile
@echo off
setlocal enabledelayedexpansion
powershell @^(^(New-Object -com shell.application^).Windows^(^)^).Document.Folder.Self.Path >> prevfolderpaths.txt
taskkill /im explorer.exe /f
start explorer.exe
FOR /F "tokens=*" %%f IN (prevfolderpaths.txt) DO (
set "var=%%f"
set "firstletters=!var:~0,2!"
IF "!firstletters!" == "::" ( start /min shell:%%~f ) ELSE ( start /min "" "%%~f" )
)
del "prevfolderpaths.txt"
Once you save the code as restart_explorer.bat .. next you should
- Right Click → Sent to → Desktop (create shortcut)
- Right Click shortcut → Run: Minimized → and add your shortcut
goldnick7
- 372
- 2
- 14
-
2In **Folder Options** there is a checkbox called **Restore previous folder windows at logon** and this restores them whenever you restart or signout or whatever. There is a way to get this done with batch alone, and not depend on powershell, but it's a lot more complicated and not convenient. However I don't know whether it's possible to save whether windows were fullscreen, or minimized or windowed... so this script justs opens the windows minimized. – goldnick7 Jan 19 '22 at 11:42
-
2Can you convert this to powershell script? – Eyal Cohen Feb 20 '23 at 13:44
2
so this script justs opens the windows minimized
I can't comment yet but would like to improve goldnick7's answer. You can keep your folders maximized by deleting second /min:
IF "!firstletters!" == "::" ( start /min shell:%%~f ) ELSE ( start "" "%%~f" )"
I left first /min there to remember this flag exists.
Also, in multi-user environment you can use this command to affect only your processes:
taskkill /f /fi "UserName eq %UserName%" /im explorer.exe
It is important that /fi goes before /im, otherwise it won't work.
TTxD
- 21
- 5
-1
powershell $open_folders = @((New-Object -com shell.application).Windows()).Document.Folder.Self.Path; Stop-Process -Name explorer -Force; foreach ($element in $open_folders){Invoke-Item $($element)}
-
2As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 27 '23 at 07:43