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?
-
There is a PowerShell module that does exactly this as an answer to this duplicate question: superuser.com/a/1770244/216046– jjzJan 2 at 21:28
3 Answers
This batch
script:
- Makes a list of currently opened windows → saves it to a
txt
file - Restarts windows
explorer.exe
- Re-opens folders from the
txt
list → deletestxt
file
@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
-
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. Jan 19, 2022 at 11:42
-
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.
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.– Community BotJul 27, 2023 at 7:43
-
This appears to be a reply to the request above "Can you convert this to powershell script?". It's a one-liner that restarts Explorer with the same windows open as before.– LesFerchOct 6, 2023 at 20:21