2

There are programs allows us to "View File in Explorer" on a file it opened or is listing (e.g. iTunes). Upon selecting that function, the file explorer is opened to the path containing that file and the corresponding file is highlighted. This is the feature I would like to emulate with PowerShell.

It is simple to open to a directory with PowerShell (i.e. explorer $path). However, attempting to include a file just opens that file with the associated program (e.g. doing explorer $path/$file where $file is a .txt file will just open $file in notepad or whatever program is defined to handle .txt files).

Can PowerShell open the File Explorer to a specific path AND highlight a specific file?

1

1 Answer 1

4

It is possible to select a specific file by sending the select command to explorer.exe. This is actually native to cmd. So, the $path is restricted to the traditional DOS format (e.g. using \ only instead of both \ and /).

# Both Works on PowerShell 5
explorer /select,$path
Invoke-Expression "explorer '/select,$path'"

This info is from this answer by Alex (thanks to AEonAX for pointing this out).

1
  • Also works with double quotes explorer.exe "/select,C:\path\to\file.txt". I've found that the important thing is to wrap the whole /select and file name in quotes.
    – TechSpud
    Commented Feb 16, 2017 at 7:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .