Thursday, September 3, 2015

Powershell Open A Windows Explorer Window And Select A File

Just a quick useful function that I have used a couple times and always forget about. If you have a script that persists a file to disk, you can open a Windows Explorer window and select the file with this function. This can be useful in desktop applications as well. I'll leave it to the reader to translate it to C# (it leverages straight-up .Net functions already).
function Show-InExplorer($file) {
 $explorerArgs = "/select, $file";
 [System.Diagnostics.Process]::Start("explorer.exe", $explorerArgs) | Out-Null;
}
NOTE: Dispite what people have said on several StackOverflow posts, I found that I had to have the comma after the /select.

No comments: