1

I'm trying to call an EXE file program that accepts command line parameters from PowerShell. One of the parameters I'm required to send is based on the string length of the parameters.

For example,

app.exe /param1:"SampleParam" /paramLen:"SampleParam".length

When I run the above, or for example:

notepad.exe "SampleParam".length

Notepad opens with the value 11 as expected.

I would like to achieve the same result when calling PowerShell from cmd / task scheduler.

For example,

powershell notepad.exe "SampleParam".length

But when I do that I get "SampleParam".length literally instead of the "SampleParam".length calculated value.

The expected result was:

running notepad.exe 11

2 Answers 2

1

Use the -Command parameter for powershell.exe:

powershell -Command "notepad.exe 'SampleParam'.length"

Be careful with the "'s since they can be picked up by the Windows command processor. This will also work:

powershell -Command notepad.exe 'SampleParam'.length

But this will not:

powershell -Command notepad.exe "SampleParam".length
Sign up to request clarification or add additional context in comments.

Comments

1

I'd suggest using variables to store your string, etc.

$Arg1 = 'SampleParam'
## This will try to open a file named 11.txt
powershell notepad.exe $Arg1.Length

In your specific example:

app.exe /param1:$Arg1 /paramLen:$Arg1.Length

Utilizing splatting:

## Array literal for splatting
$AppArgs = @(
    "/param1:$Arg1"
    "/paramLen:$($Arg1.Length)"
)
app.exe @AppArgs

5 Comments

FYI splatting was adding in PowerShell v2.
That doc says "Revised for PowerShell 3.0." It doesn't say "splatting first appeared in PowerShell 3.0." I tested splatting in 2.0 and it works. Also see here: Use Splatting to Simplify Your PowerShell Scripts.
@Bill_Stewart Interesting.. I've definitely had a number of situations where it hasn't worked on PSv2, although after migrating my environment to 5.1, it hasn't really been a worry anymore
I never had a problem with splatting in v2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.