16

I am trying to run a external exe from a powershell script.

This exe wants 4 parameters.

I have been trying every combo of invoke-item, invoke-command, & 'C:\program files\mycmd.exe myparam', made a shortcut in C:\ to get rid of the spaces in the path.

I can make it work with one parameter, but not with more. I get various errors.

To sum up, how do you send 4 parameters to an exe?

2 Answers 2

25

It's best if shown in longhand. Once you see what's going on, you can shorten it down by just using commas between each argument.

$arg1 = "filename1"
$arg2 = "-someswitch"
$arg3 = "C:\documents and settings\user\desktop\some other file.txt"
$arg4 = "-yetanotherswitch"

$allArgs = @($arg1, $arg2, $arg3, $arg4)

& "C:\Program Files\someapp\somecmd.exe" $allArgs

... shorthand:

& "C:\Program Files\someapp\somecmd.exe" "filename1", "-someswitch", "C:\documents and settings\user\desktop\some other file.txt", "-yetanotherswitch"
Sign up to request clarification or add additional context in comments.

3 Comments

Watch out with & 'C:\program files\someapp\somecmd.exe" -arg $allArgs - the -arg is unnecessary unless you were intending to pass -arg itself as a parameter to somecmd.exe.
Yeah, for whatever reason I referred to this today, and noticed that the line of code with the -arg in it didn't even work! Oops/removed.
I think the ( ... ) are missing from the last line: & "somecmd.exe" ("filename1", "-some...")
12

In the easy case, passing arguments to a native exe is as simple as using a built-in command:

PS> ipconfig /allcompartments /all

You can run into problems when you specify a full path to an EXE and that path contains spaces. For example if PowerShell sees this:

PS> C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\sn.exe -k .\pubpriv.snk

It interprets the command to be "C:\Program" and "Files\Microsoft" as the first parameter, "SDKs\Windows\v7.0\Bin\sn.exe" as the second parameter, etc. The simple solution is to put the path in a string use the invocation operator & to invoke the command named by the path e.g.:

PS> & 'C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\sn.exe' -k .\pubpriv.snk

The next area we run into problems with is when the arguments are complex and/or use characters that PowerShell interprets specially e.g.:

PS> sqlcmd -v user="John Doe" -Q "select '$(user)' as UserName"

This doesn't work and we can debug this by using a tool from the PowerShell Community Extensions called echoargs.exe which shows you exactly how the native EXE receives the arguments from PowerShell.

PS> echoargs -v user="John Doe" -Q "select '$(user)' as UserName"
The term 'user' is not recognized as the name of a cmdlet, function, 
script file, or operable program. Check the spelling of the name, ...
<snip>

Arg 0 is <-v>
Arg 1 is <user=John Doe>
Arg 2 is <-Q>
Arg 3 is <select '' as UserName>

Note that with Arg3 $(user) is interpreted & evaluated by PowerShell and results in an empty string. You can fix this problem and a good number of similar issues by using single quotes instead of double qoutes unless you really need PowerShell to evaluate a variable e.g.:

PS> echoargs -v user="John Doe" -Q 'select "$(user)" as UserName'
Arg 0 is <-v>
Arg 1 is <user=John Doe>
Arg 2 is <-Q>
Arg 3 is <select $(user) as UserName>

If all else fails, use a here string and Start-Process like so:

PS> Start-Process echoargs -Arg @'
>> -v user="John Doe" -Q "select '$(user)' as UserName"
>> '@ -Wait -NoNewWindow
>>
Arg 0 is <-v>
Arg 1 is <user=John Doe>
Arg 2 is <-Q>
Arg 3 is <select '$(user)' as UserName>

Note if you are using PSCX 1.2 you will need to prefix Start-Process like so - Microsoft.PowerShell.Management\Start-Process to use PowerShell's built-in Start-Process cmdlet.

4 Comments

Hm; seeing this makes me wonder again: Is there a good reason for keeping the PSCX versions of cmdlets that PowerShell v2 includes? Get-Random tripped me up a few times due to its completely different semantics.
No there isn't. So we have moved the three cmdlets that have name collisions into a separate module in PSCX 2.0 (Pscx.Deprecated.psd1). There is a 2.0 beta available for download but because it is module based it works only on PowerShell 2.0.
I just noticed this: PowerShell and external commands done right. Nearly identical advice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.