5

I have run into a problem while writing my first basic PowerShell utility. I need to open an Explorer window with a file selected, but can't get the syntax of the command right.

I found this answer on SO, https://stackoverflow.com/a/12162855/957246 and had a read of this excellent blog on external commands, http://edgylogic.com/blog/powershell-and-external-commands-done-right/ - but still can't get it to work.

This is my example script (test.ps1):

$myFile = "C:\Projects\Scripts\any.txt"
# The backslash-backticks are to escape the speech marks, once for PowerShell and then again for DOS
& "C:\Projects\Scripts\EchoArgs.exe" /select",\`"$myFile\`""
& "explorer" /select",\`"$myFile\`""

..and this is the batch file that calls it, in case there's something wrong with the way I'm calling the PowerShell script:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Projects\Scripts\test.ps1'"

I've tried a few different combinations and the output from echoargs suggests that the syntax is right (assuming the parameter string for explorer is taken as a single string) but whatever I try just opens an explorer window on 'my documents' and the parameters are ignored/being thrown away somewhere.

There's a bit on the MSDN page for explorer.exe switches that says the comma is an empty parameter, maybe that's the thing I need to be providing by formatting the command differently?

Community
  • 1
  • 1
trapper_hag
  • 780
  • 9
  • 14

2 Answers2

5

What is the point of using batch commands from powershell? Your only going to confuse yourself like that.. and me for that matter.

$myFile = "C:\Projects\Scripts\any.txt"
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = Split-Path $myFile -Parent 
$OpenFileDialog.FileName = Split-path $myfile -leaf
$OpenFileDialog.ShowDialog() | Out-Null

EDIT

If you need to keep going the explorer route try this :

Start-Process -FilePath C:\Windows\explorer.exe -ArgumentList "/select, ""$myFile"""

Cole9350
  • 5,444
  • 2
  • 34
  • 50
  • Sorry for the multiple comments.. That just freezes for me when I paste it into my test script, or if I run it from the PowerShell IDE, it says "cannot be loaded because the execution of scripts is disabled on this system". Other scripts work without issue so the text of the error message could be a red herring. Maybe there's something else I should've included or some other common element the script needs to make your example work? – trapper_hag Aug 14 '15 at 18:44
  • I'm on PowerShell v2 if that's relevant, maybe there's something that v2 doesn't like? I've updated the question with the powershell-v2.0 tag. Also - the script I'm writing (which calls a couple of other executables with parameters without issue) does some stuff with a file. I then want to open Windows Explorer with that file selected for the user (which is why the example has the filename in a variable and is called from an example batch file). Your example looks like it's opening a file selection dialog rather than Explorer? – trapper_hag Aug 14 '15 at 18:44
  • @trapper_hag Before you can use powershell scripts on any machine you must set the execution policy: `Set-ExecutionPolicy RemoteSigned` ... See `Get-Help about_Execution_Policies` for more info – Cole9350 Aug 14 '15 at 19:02
  • Thank you very much for your help :) your example now works from the Powershell IDE - but it's opening a file selection dialog rather than Windows Explorer. I've changed the title to make it clearer that I need to select the file in Windows Explorer. – trapper_hag Aug 14 '15 at 19:18
  • @trapper_hag you need to set execution policy for IDE and console separately. Also, I updated my post to better suit your needs – Cole9350 Aug 14 '15 at 19:20
  • Thank you again - from your reply I found that this also works: & "explorer" "/select,""$myFile""" - It was the extra speech marks to escape the path that threw me. – trapper_hag Aug 14 '15 at 19:26
  • @Cole9350 Is there a way to specify multiple file paths to have it all selected when the folder is opened? – rhythmo Apr 21 '21 at 13:46
1

Also you could just use:

$dir = "\\PATH\TO\YOUR\DIRECTORY"
C:\Windows\explorer.exe "/select,$dir"