Search code examples
powershellwindowfocusmicrosoft-file-explorer

How to Set an Open Explorer Window to be Topmost in PowerShell Without Extra Modules?


In PowerShell how can I set an open explorer window topmost without any extra modules

the explorer window is is c:\users\public\fonts

Regards

I found this one liner (New-Object -ComObject WScript.Shell).AppActivate((get-process notepad).MainWindowTitle) but it doesn't work for explorer.


Solution

  • Making a window topmost is different from activating it:

    • The topmost window remains visible in front of all other windows whether or not it is the active window.

    • The active window is the one with the input focus (the one that respond to user input), which is by default also the one visible in front of all other windows - but may be preempted as such by the window designated as topmost.


    It looks like activating a window by its title is what you're looking for:

    $null = (New-Object -ComObject WScript.Shell).AppActivate('c:\users\public\fonts')
    
    • For predictable activation, specify the window title exactly as shown in the File Explorer window of interest - except that case does not matter.

    • The .AppActivate() method seemingly also accepts substrings of window titles, though the exact requirements for what substrings constitute a match aren't clear.