Restart Explorer - With Window Position and Size Preservation
A PowerShell script that restarts Windows Explorer while preserving all open windows with their exact positions, sizes, states, and tabs.
🎯 What Does This Script Do?
This script allows you to restart the Explorer.exe process with automatic restoration of all open folder windows including:
- Exact window positions (X, Y coordinates)
- Window sizes (width x height)
- Window states (normal, minimized, maximized)
- Tabs within Explorer windows (Windows 11 feature)
Perfect for when Explorer becomes slow or unresponsive, but you don't want to manually reopen and rearrange all your windows.
✨ Key Features
Window Management
✅ Preserves window positions - Each window returns to its exact location
✅ Preserves window sizes - Width and height are maintained
✅ Preserves window states - Minimized, maximized, or normal
✅ Tab support - Preserves and restores tabs in Explorer windows (Windows 11)
✅ Duplicate path handling - Correctly handles multiple windows/tabs pointing to the same location
Intelligent Restoration
✅ Intelligent waiting - Waits for windows and tabs to fully load before proceeding
✅ Progress tracking - Shows detailed information about each step
✅ Automatic cleanup - Removes unwanted "This PC" windows that may appear
✅ Diagnostic logs - Shows final state of all Explorer windows
Reliability
✅ R6016 error prevention - Optimized delays prevent Runtime Error R6016
✅ Error handling - Comprehensive error handling with informative messages
✅ Slow system optimization - Works reliably even on resource-constrained systems
📋 How to Use
Method 1: Run the BAT file (RECOMMENDED)
Double-click RestartExplorer.bat
Method 2: Run PowerShell directly
Right-click RestartExplorer.ps1 → "Run with PowerShell"
Method 3: From PowerShell console
💻 Requirements
- Windows 10 or newer (Windows 11 recommended for tab support)
- PowerShell 5.0 or newer (included by default in Windows 10/11)
- No additional software or packages required
🔧 How It Works
The script follows these steps:
Step 1: Save Window Information
- Scans all open Explorer windows
- Records path, position, size, and state of each window
- Groups windows with identical positions as tabs
- Displays summary of saved windows
Step 2: Close Explorer
- Terminates the Explorer.exe process
- Waits for clean shutdown
Step 3: Start Explorer
- Launches Explorer.exe
- Waits for initialization
- Closes any automatically opened default windows
Step 4: Restore Windows
Phase 1: Opens all windows simultaneously (with delay for stability)
Phase 2: Sets positions, sizes, and states for each window
Phase 3: Adds tabs to windows (intelligently waits for loading)
Step 5: Cleanup
- Removes any unwanted "This PC" windows
- Shows diagnostic logs of final state
📊 Example Output
=== Restart Explorer - with window position and size preservation ===
Step 1: Saving information about open folders...
Saved window: I:\
Position: (2544, 28) Size: 865x692 State: Normal
Saved window with 2 tabs:
Position: (4257, 702) Size: 865x692 State: Normal
- D:\Programming
- F:\
Saved window with 2 tabs:
Position: (3407, 5) Size: 860x700 State: Normal
- C:\Users\Username\Desktop\Tools
- I:\
Saved 3 windows.
Step 2: Closing Explorer.exe...
Step 3: Starting Explorer.exe...
Closing automatically opened windows...
No automatically opened windows.
Step 4: Restoring windows with positions and sizes...
PHASE 1: Opening all windows (with delay for stability)...
Opening: I:\
Opening: D:\Programming
Opening: C:\Users\Username\Desktop\Tools
Waiting for windows to load (intelligently)...
Loaded: I:\ (after 4602ms)
Loaded: D:\Programming (after 8957ms)
Loaded: C:\Users\Username\Desktop\Tools (after 9072ms)
All 3 windows loaded in 9072ms
PHASE 2: Setting positions and sizes of all windows...
Setting window: I:\
Position: (2544, 28) Size: 865x692 State: Normal
Setting window: D:\Programming
Position: (4257, 702) Size: 865x692 State: Normal
Setting window: C:\Users\Username\Desktop\Tools
Position: (3407, 5) Size: 860x700 State: Normal
PHASE 3: Adding tabs to windows...
Adding 1 tabs to: D:\Programming
Tab: F:\
Loaded after 1232ms
Adding 1 tabs to: C:\Users\Username\Desktop\Tools
Tab: I:\
Loaded after 1446ms
Step 5: Closing unwanted 'This PC' windows...
No unwanted 'This PC' windows.
=== Completed successfully! ===
=== DIAGNOSTIC LOGS ===
Currently open Explorer windows:
[1] HWND: 8000072 - PATH: I:\
[2] HWND: 2564422 - PATH: D:\Programming
[3] HWND: 8461394 - PATH: C:\Users\Username\Desktop\Tools
[4] HWND: 2564422 - PATH: F:\
[5] HWND: 8461394 - PATH: I:\
Total number of open Explorer windows: 5
=============================
Closing in 3 seconds...
🆚 Comparison with Old Script
Old Script (VBS + BAT):
- ❌ Only restored folder paths
- ❌ Windows opened in default positions and sizes
- ❌ No progress information
- ❌ No tab support
- ❌ No state preservation (minimized/maximized)
New Script (PowerShell):
- ✅ Restores exact window positions
- ✅ Restores exact window sizes
- ✅ Preserves window states (minimized/maximized)
- ✅ Supports tabs in Explorer windows
- ✅ Shows detailed progress information
- ✅ Intelligent waiting for window loading
- ✅ Better error handling
- ✅ Uses Windows API for precise window control
- ✅ Automatic cleanup of unwanted windows
- ✅ Optimized for slow systems
🛠️ Technical Details
Technologies Used
- Shell.Application COM - Gets list of open Explorer windows
- Windows API (user32.dll) - Window manipulation:
GetWindowPlacement - Gets window position, size, and state (including minimized)
SetWindowPos - Sets window position and size
ShowWindow - Sets window state (normal/minimized/maximized)
SetForegroundWindow - Activates window for tab operations
IsIconic - Checks if window is minimized
IsZoomed - Checks if window is maximized
- WScript.Shell - Sends keyboard shortcuts (Ctrl+T, Alt+D, etc.) for tab operations
- PowerShell - Main script engine
Intelligent Features
Tab Detection:
- Groups windows with identical positions (±5 pixels tolerance) as tabs
- Counts windows before and after tab creation to verify successful loading
Duplicate Path Handling:
- Tracks windows by HWND (window handle) instead of path
- Allows multiple windows/tabs with the same path
- Excludes already-processed windows when searching
Timing Optimization:
- 400ms delay between opening windows (prevents R6016 error)
- 1500ms after Ctrl+T (ensures tab is fully loaded)
- 500ms after Alt+D (ensures address bar is ready)
- 5000ms timeout for tab loading (accommodates slow systems)
- 200ms check interval (reduces system load)
🐛 Troubleshooting
Script doesn't start
- Ensure PowerShell is installed (default in Windows 10/11)
- Check execution policy:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Windows don't restore to exact positions
- May result from differences in screen resolution
- Ensure all monitors are connected before running
- Some windows may be adjusted by Windows
"No open Explorer windows to save"
- This is normal if you have no folder windows open
- The script will exit without making changes
R6016 Runtime Error appears
- This has been fixed in the latest version with optimized delays
- If it still occurs, try closing some resource-intensive applications before running
Tabs timeout or don't load
- Increase timeout in Wait-ForTabToLoad function (default: 5000ms)
- Check if drives are accessible (network drives may be slow)
- Some paths may require administrator privileges
Unwanted "This PC" windows appear
- Step 5 automatically closes these windows
- If they persist, they may be opened by other applications
📝 Diagnostic Logs
The script shows diagnostic logs at the end to help identify issues:
- Lists all currently open Explorer windows
- Shows HWND and path for each window
- Highlights unwanted "This PC" windows in red
- Displays total window count
Use these logs to verify:
- All expected windows are open
- No unwanted windows remain
- Tabs are correctly grouped (same HWND = tabs in same window)
🚀 Performance
Typical execution times (on average system):
- Window loading: 8-12 seconds
- Tab loading: 1-2 seconds per tab
- Total time: 15-30 seconds (depends on number of windows/tabs)
On slow systems:
- Window loading: 10-15 seconds
- Tab loading: 2-5 seconds per tab
- Total time: 20-60 seconds
💡 Use Cases
- Explorer is slow or unresponsive - Restart without losing your workspace
- After Windows updates - Restore your window layout after system restart
- Daily workflow - Maintain consistent window organization
- Development work - Keep project folders in specific positions
- Multi-monitor setups - Preserve window distribution across screens
👨💻 Author
Enhanced version with window position, size, state, and tab preservation.
Based on original script by Hackoo 2020.
📄 License
Free for personal and commercial use.
🔄 Version History
v2.0 (Current)
- Added tab support for Windows 11 Explorer
- Added window state preservation (minimized/maximized)
- Fixed R6016 runtime error with optimized delays
- Added intelligent waiting for window and tab loading
- Added automatic cleanup of unwanted "This PC" windows
- Added diagnostic logs
- Optimized for slow/resource-constrained systems
- Translated to English
- Improved error handling
v1.0
- Initial version with position and size preservation