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.

rem works
Explorer /select, c:\Users\xah\xtest.txt
rem works
Explorer /select, "c:\Users\xah\xtest.txt"
rem no work. open the wrong dir
Explorer /select, c:/Users/xah/xtest.txt

cmd.exe does not understand single quote:

# no work
Explorer /select, 'c:\Users\xah\xtest.txt'
rem no work
Explorer /select, 'c:\Users\xah\xtest.txt'
rem works
Explorer /select, "c:\Users\xah\xtest.txt"

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

# works
Explorer /select, c:\Users\xah\xtest.txt
# does not work
Explorer /select, "c:\Users\xah\xtest.txt"

explorer.exe does not understand path with slash separator

# no work. open the wrong dir
Explorer /select, c:/Users/xah/xtest.txt

using Start-Process:

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

using Invoke-Expression:

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

launch PowerShell process:

# works
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/xahlee_info/emacs/i/

Windows File Explorer Command Line Doc