Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute an external program with parameters in PowerShell?

Tags:

powershell

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?

like image 604
Norm Avatar asked Sep 18 '12 13:09

Norm


1 Answers

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"
like image 107
Björn Lindqvist Avatar answered Nov 04 '22 08:11

Björn Lindqvist