Nadav Caridi
Nadav Caridi

Reputation: 33

Call program from powershell.exe command and manipulate parameters

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

Upvotes: 1

Views: 311

Answers (2)

Maximilian Burszley
Maximilian Burszley

Reputation: 19694

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

Upvotes: 1

Palle Due
Palle Due

Reputation: 6312

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

Upvotes: 1

Related Questions