5

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?

1

3 Answers 3

6

This batch script:

  1. Makes a list of currently opened windows → saves it to a txt file
  2. Restarts windows explorer.exe
  3. Re-opens folders from the txt list → deletes txt 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

  1. Right Click → Sent to → Desktop (create shortcut)
  2. Right Click shortcut → Run: Minimized → and add your shortcut
2
  • 2
    In 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, 2022 at 11:42
  • 2
    Can you convert this to powershell script?
    – fatFeather
    Feb 20, 2023 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.

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)}
2
  • 2
    As 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 Bot
    Jul 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.
    – LesFerch
    Oct 6, 2023 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .