PowerShell Launch File Explorer with Selection

By Xah Lee. Date: . Last updated: .

Problem:

launch Microsoft Windows file explorer, with a particular file selected.

explore either from PowerShell or cmd.exe

The goal is actuall to call them from emacs lisp.

backslash for path works in PowerShell, not in cmd.exe:

# works in cmd.exe and PowerShell
Explorer /select,c:\Users\xah\xtest.txt
Explorer /select, c:\Users\xah\xtest.txt
Explorer /select,"c:\Users\xah\xtest.txt"

explorer.exe does not understand slashed path:

# slashed path no work. open the wrong dir , in both cmd and PowerShell
Explorer /select, c:/Users/xah/xtest.txt

cmd.exe does not understand single quote:

# single quote, works in PowerShell, wrong dir in cmd
Explorer /select,'c:\Users\xah\xtest.txt'

using Start-Process:

# works in PowerShell
Start-Process -FilePath "Explorer.exe"
# works in PowerShell
Start-Process -FilePath "Explorer.exe" -ArgumentList "/select,c:\Users\xah\xtest.txt"

using Invoke-Expression:

# works in PowerShell
Invoke-Expression "explorer /select,c:\Users\xah\xtest.txt"

launch PowerShell process:

# works in PowerShell , no in cmd
pwsh -Command {Start-Process -FilePath "Explorer.exe" -ArgumentList "/select,c:\Users\xah\xtest.txt"}
# works in PowerShell
PowerShell -Command {Start-Process -FilePath "Explorer.exe" -ArgumentList "/select,c:\Users\xah\xtest.txt"}
# wrong dir
PowerShell -Command Start-Process -FilePath "Explorer.exe" -ArgumentList "/select,c:\Users\xah\xtest.txt"
# syn error
PowerShell -Command Start-Process -FilePath "Explorer.exe" -ArgumentList "/select c:\Users\xah\xtest.txt"
# wrong dir
PowerShell -Command Start-Process -FilePath "Explorer.exe" -ArgumentList "/select","c:\Users\xah\xtest.txt"
PowerShell -Command Start-Process -FilePath "Explorer.exe" -ArgumentList "select","c:\Users\xah\xtest.txt"
# syntax error
PowerShell -Command Start-Process -FilePath "Explorer.exe" -ArgumentList "/select" "c:\Users\xah\xtest.txt"
# this works, but not open the correct dir
pwsh -Command Start-Process explorer -WorkingDirectory c:/Users/xah/web/ergoemacs_org/emacs/i/

Windows File Explorer Command Line Doc

Explorer [/n] [/e] [(,)/root,‹object›] [/select,‹object›]

/n                Opens a new single-pane window for the default
                  selection. This is usually the root of the drive Windows
                   is installed on. If the window is already open, a
                  duplicate opens.

/e                Opens Windows Explorer in its default view.

/root,‹object›    Opens a window view of the specified object.

/select,‹object›  Opens a window view with the specified folder, file or
                  application selected.

Examples:

   Example 1:
Explorer /select,C:\TestDir\TestApp.exe

      Opens a window view with TestApp selected.

   Example 2:
Explorer /e,/root,C:\TestDir\TestApp.exe

      This opens
Explorer with C: expanded and TestApp selected.

   Example 3:
Explorer /root,\\TestSvr\TestShare

      Opens a window view of the specified share.

   Example 4:
Explorer /root,\\TestSvr\TestShare,select,TestApp.exe

      Opens a window view of the specified share with TestApp selected.