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?
- 440
- 1
- 5
- 14
3 Answers
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
- 440
- 1
- 5
- 14
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.
- 21
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)}
- 21