20

I have read this answer stackoverflow answer and it get's me there half way. Here is what I need to do.

Execute this command:

"c:\myexe.exe <c:\Users\Me\myanswerfile.txt"

If I run that straight from within my powershell script

&'c:\myexe.exe <c:\Users\Me\myanswerfile.txt'

I get this error:

The term 'C:\myexe.exe <c:\Users\Me\myanswerfile.txt' is not recognized as the name of
a cmdlet, function, script file, or operable program. Check the spelling of the name,or 
if a path was included, verif that the path is correct and try again.

Now I have tried several variations of this including placing the original command in a variable called $cmd and then passing the

If I append the '<' to the $cmd variable the command fails with a similar error as the first one.

I'm stumped. Any suggestions?

2 Answers 2

29

If you want to run a program, just type its name and parameters:

notepad.exe C:\devmy\hi.txt

If you want to run an exe and redirect stdin to it which your example seems to be an attempt of, use:

Get-Content c:devmy\hi.txt | yourexe.exe 

If you need to specify the full path to the program then you need to use ampersand and quotes otherwise powershell thinks you are defining a plain string:

&"C:\Program Files (x86)\Notepad++\notepad++.exe"
Sign up to request clarification or add additional context in comments.

Comments

11

Simply use & operator

& "Program\path\program.exe" "arg1" "arg2" ....

1 Comment

important to notice, it is safer to ALWAYS surround all parameters for external program with double quotes, otherwise very strange and unexpected results may emerge

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.