19

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?

Community
  • 1
  • 1
Norm
  • 659
  • 3
  • 10
  • 20

2 Answers2

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"
Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
10

Simply use & operator

& "Program\path\program.exe" "arg1" "arg2" ....
MUY Belgium
  • 2,330
  • 4
  • 30
  • 46
  • 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 – ivan866 Feb 26 '23 at 00:58