Linux HowTo: how to make windows 10’s explorer focus existing window when opening the same folder

Original Source Link

There is a scenario: suppose I have two open explorer windows, window 1 showing the folder C:/data and window 2 showing the folder C:/.

What I want is that when I double-click (open) the folder data from within the window 1 (C:/), the window 2 (C:/data) get focused instead of opening the folder data in window 1.

I found a similar question here : so thread but it is dated from 2018 and not aiming Windows 10. I wonder if things are different in my case.

Thanks in advance,

I have written a powershell script SwitchOrOpenFolder.ps1 that could do the job in combination with a simple registry hack in HKEY_CLASSES_ROOTFolder.

Note □ always make a backup copy of the Windows registry before creating, editing, or modifying any keys or values. ■

  1. Add SwitchOrOpen option to context menu for drives and folders and associate it with silent run of the D:PShelltestsSwitchOrOpenFolder.ps1 script (change the path to fit your circumstances). This step should result to the following:
reg query "HKEY_CLASSES_ROOTFoldershellSwitchOrOpencommand"
HKEY_CLASSES_ROOTFoldershellSwitchOrOpencommand
    (Default)    REG_SZ    mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -NoProfile -ExecutionPolicy Bypass -File """"D:PShelltestsSwitchOrOpenFolder.ps1"""" -Path """"%V"""""",0:close")
  1. Make the SwitchOrOpen verb a default option in context menu for drives and folders (optional: maps this option to a mouse double-click or keyboard Enter); result should be as follows:
reg query "HKEY_CLASSES_ROOTFoldershell" -ve
HKEY_CLASSES_ROOTFoldershell
    (Default)    REG_SZ    SwitchOrOpen
  1. Use the following SwitchOrOpenFolder.ps1 script:
param ( [Parameter(Mandatory)] [string]$Path )
if ( '"' -eq $Path.Substring($Path.Length-1) ) {
    $Path = $Path.Substring(0, $Path.Length-1) + ''
}
$type = 'Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes.WindowAPI' -as [type]
if ( $null -eq  $type ) {
    $sig = @'
    [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd);
    [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
'@
    $type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
}
$FileExplorer = (New-Object -ComObject 'Shell.Application').Windows() |
    Where-Object Name -EQ 'File Explorer'
$windows = $FileExplorer |
    Where-Object { ( $Path -eq $_.Document.Folder.Self.Path ) } |
        Select-Object -First 1
if ( $null -eq $windows ) {
    $windows = $FileExplorer |
        Where-Object { ( $type::GetForegroundWindow() -eq $_.HWND ) }
    if ( $null -eq $windows ) {
        C:WINDOWSexplorer.exe /e,$Path
    } else {
        $windows.Navigate($Path)
    }
    return
}
$hwnd = $windows.HWND
if ( -32000 -in $windows.Left, $windows.Top ) {
    $null = $type::ShowWindowAsync($hwnd, 4) # ShowNoActivate
}
$null = $type::SetForegroundWindow($hwnd) 

Addendum. The following Windows 10 registry file SwitchOrOpen.reg represents both above steps 1 and 2:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOTFoldershell]
@="SwitchOrOpen"

[HKEY_CLASSES_ROOTFoldershellSwitchOrOpen]

[HKEY_CLASSES_ROOTFoldershellSwitchOrOpencommand]
@="mshta.exe vbscript:Execute("CreateObject(""WScript.Shell"").Run ""powershell -NoProfile -ExecutionPolicy Bypass -File """"D:\PShell\tests\SwitchOrOpenFolder.ps1"""" -Path """"%V"""""",0:close")"

Tagged : / /

Leave a Reply

Your email address will not be published. Required fields are marked *