Skip to content

Instantly share code, notes, and snippets.

@boredwz
Last active September 22, 2024 18:39
Show Gist options
  • Select an option

  • Save boredwz/1511cb7f81f0c01f3373a3d2ad8a7e9d to your computer and use it in GitHub Desktop.

Select an option

Save boredwz/1511cb7f81f0c01f3373a3d2ad8a7e9d to your computer and use it in GitHub Desktop.
(PowerShell) Restart Explorer.exe and restore tabs
<#
.SYNOPSIS
Restart <explorer.exe> and restore tabs
.DESCRIPTION
[Author]
boredwz | https://github.com/boredwz
[URL]
Gist | https://gist.github.com/boredwz/1511cb7f81f0c01f3373a3d2ad8a7e9d
[Info]
Tested on:
- Windows 10 (build 19045.4780)
- PowerShell v5.1
πŸ”” I don't know if it is possible to open a single window with multiple tabs on Windows 11.
Restart <explorer.exe> and restore tabs:
1. Respectively (save tab state)
Normal -> Normal
Maximized -> Maximized
Minimized -> Normalized and Minimized
2. Minimized
Any -> Minimized
[One-liner] Respectively:
$e='explorer';$t=@();(New-Object -co Shell.Application).Windows()|%{$t+=@{p=$_.LocationURL;w=if($_.Top -lt 0){if($_.Top -lt -8000){'min'}else{'max'}}else{'nor'}}};kill -n $e -for;sleep 1;if(!($null=ps $e -ea 0)){saps $e};sleep 4;$t|%{saps $_.p -win $_.w}
[One-liner] Minimized:
$e='explorer';$t=@();(New-Object -co Shell.Application).Windows()|%{$t+=$_.LocationURL};kill -n $e -for;sleep 1;if(!($null=ps $e -ea 0)){saps $e};sleep 4;$t|%{saps $_ -win min}
.PARAMETER Minimize
Any window state -> Minimized.
Aliases: "Min", "Minimize", "Minimized".
.EXAMPLE
PS C:\> & ".\RestartExplorer.ps1"
*explorer.exe restarting...*
*tabs restoring...*
.EXAMPLE
PS C:\> & ".\RestartExplorer.ps1" -minimized
*explorer.exe restarting...*
*tabs restoring minimized...*
#>
param(
[Alias("min","minimized")][switch]$Minimize
)
# Get explorer windows
$explorerWindows = @()
(New-Object -ComObject Shell.Application).Windows() | ForEach-Object {
$windowState = 'Normal'
if($_.Top -lt 0) {$windowState = 'Maximized'}
if($_.Top -lt -8000 -or $Minimize) {$windowState = 'Minimized'}
$explorerWindows += @{
Location=$_.LocationURL;
WindowState=$windowState
}
}
# Restart explorer.exe
Stop-Process -ProcessName explorer -Force
Start-Sleep 1
if (!($null = Get-Process explorer -ErrorAction SilentlyContinue)) {Start-Process explorer}
Start-Sleep 4
# Open saved explorer windows
$explorerWindows | ForEach-Object {
Start-Process $_.Location -WindowStyle $_.WindowState
}
@boredwz

boredwz commented Sep 12, 2024

Copy link
Copy Markdown
Author

Restart explorer.exe and restore tabs (PowerShell)

Original post: AutoDarkMode/Windows-Auto-Night-Mode#597 (comment)_

Info

πŸ””Β  I don't know if it is possible to open a single window with multiple tabs on Windows 11.

Since taskkill.exe .. start opens random tabs (This PC, Quick Access, Libraries etc.) Stop-Process is being used.
This does restart more smoothly, faster, and without annoying windows.

Tested on:

  • Windows 10 (build 19045.4780)
  • PowerShell v5.1

Function

function RestartExplorer {
    param(
        [Alias("min","minimized")][switch]$Minimize
    )
    
    # Get explorer windows
    $explorerWindows = @()
    (New-Object -ComObject Shell.Application).Windows() | ForEach-Object {
        $windowState = 'Normal'
        if($_.Top -lt 0) {$windowState = 'Maximized'}
        if($_.Top -lt -8000 -or $Minimize) {$windowState = 'Minimized'}
        $explorerWindows += @{
            Location=$_.LocationURL;
            WindowState=$windowState
        }
    }

    # Restart explorer.exe
    Stop-Process -ProcessName explorer -Force
    Start-Sleep 1
    if (!($null = Get-Process explorer -ErrorAction SilentlyContinue)) {Start-Process explorer}
    Start-Sleep 4

    # Open saved explorer windows
    $explorerWindows | ForEach-Object {
        Start-Process $_.Location -WindowStyle $_.WindowState
    }
}

Shortened One-liners

Preserve window state

$e='explorer';$t=@();(New-Object -co Shell.Application).Windows()|%{$t+=@{p=$_.LocationURL;w=if($_.Top -lt 0){if($_.Top -lt -8000){'min'}else{'max'}}else{'nor'}}};kill -n $e -for;sleep 1;if(!($null=ps $e -ea 0)){saps $e};sleep 4;$t|%{saps $_.p -win $_.w}

Minimize all

$e='explorer';$t=@();(New-Object -co Shell.Application).Windows()|%{$t+=$_.LocationURL};kill -n $e -for;sleep 1;if(!($null=ps $e -ea 0)){saps $e};sleep 4;$t|%{saps $_ -win min}

Usage

.\RestartExplorer.ps1 [-Minimized]
  • -Minimized Β  β€” Β  any window state will be minimized

Example 1

# invoke restart explorer from url (current PowerShell session)
iwr -useb "https://gist.github.com/boredwz/1511cb7f81f0c01f3373a3d2ad8a7e9d/raw/RestartExplorer.ps1"|iex

Example 2

# invoke restart explorer (minimized) from url (current PowerShell session)
iex "&{$(iwr -useb "https://gist.github.com/boredwz/1511cb7f81f0c01f3373a3d2ad8a7e9d/raw/RestartExplorer.ps1")} -minimize"

Example 3

# download RestartExplorer.ps1 and call
iwr "https://gist.github.com/boredwz/1511cb7f81f0c01f3373a3d2ad8a7e9d/raw/RestartExplorer.ps1" -o "RestartExplorer.ps1"
& ".\RestartExplorer.ps1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment