4

I'm trying to very simply run an executable and pass a file path in as the first argument. In DOS, this would be the command:

import.exe "C:\Some Path\With\Spaces.txt"

By placing the path in quotes, the path is correctly parsed as the first token into the executable.

In PowerShell I'm using this:

$feeds = dir 'T:\Documents\Company\Product Development\Data
foreach ($feed in $feeds) { start -FilePath import.exe -ArgumentList $feed.FullName }

The problem with the Start-Process cmdlet in PowerShell is that it uses a string array for arguments, so the path is broken up and sent to the executable as separate tokens, or effectively as a string without those important quotes.

Adding quotes to my PowerShell command line force "$feed.FullName" to be treated literally.

And double quotes "" make PowerShell not see anything in the argument list. "The argument is null or empty." it tells me.

I expect that this is a known headache and has had a known workaround from day one.

Thanks

Luke

UPDATES AND RESPONSES

foreach ($feed in $feeds) { Invoke-Expression "start import.exe `"$feed.FullName`"" }

Note: I'm using Start since despite setting my location to the folder in which import.exe resides, PowerShell seems to have been written to look for executables in the file-system current location.

The variable gets interpreted with the .FullName as a literal string!:

filename.txt.FullName

This one:

foreach ($feed in $feeds) { Invoke-Expression "C:\Users\Public\Documents\~Main\Data.Importer\Data.Importer\bin\Release\import.exe ($feed.FullName)" }

Results in:

Invoke-Expression : Unexpected token '_data.txt.FullName' in expression or statement.

Interestingly, on its own, this works:

C:\Users\Public\Documents\~Main\Data.Importer\Data.Importer\bin\Release\import.exe $feed.FullName

But this:

foreach ($feed in $feeds) { Invoke-Expression C:\Users\Public\Documents\~Main\Vuplan\Evoq.Vuplan.Data.Epg.Importer\Evoq.Vuplan.Data.Epg.Importer\bin\Release\import.exe $feed.FullName }

Results in this:

Invoke-Expression : A positional parameter cannot be found that accepts argument 'T:\Documents\Company\Product Development\Data\3112_data.txt'.
2

How about:

Invoke-Expression "start import.exe '$($feed.FullName)'"
  • This works. Thanks Shay - and Bartek @bielawb on Twitter - who both managed to work out how to stop the $feed variable being detached from its .FullName property. – Luke Puplett Dec 29 '10 at 12:10
3

This command works for me:

import.exe '"C:\Some Path\With\Spaces.txt"'

Make sure you nest the double quotes inside single quotes.

1

This sort of thing can be worked around with the invoke-expression cmdlet

invoke-expression "import.exe `"C:\Some Path\With\Spaces.txt`""

The backticks will be interpreted and should start import.exe with the specified string.

  • I was too eager, this actually doesn't work. Still fighting. – Luke Puplett Dec 29 '10 at 11:27
1

After assigning all paths to variables, the Start-Process command can be used like this:

Start-Process -NoNewWindow -FilePath $toolPath -ArgumentList `"$arg1`",`"$arg2`"

By using `" the arguments get passed quoted to the application, i.e.: Screenshot of an example

0

Calling DOS commands in Powershell directly.

For example:

cmd.exe /c start import.exe $myParameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.