158

I'm trying to execute an EXE file using a PowerShell script. If I use the command line it works without a problem (first I supply the name of the executable and series of parameters to invoke it):

"C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode

But doing the exact same thing inside of a script returns an error:

The term '"C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode' 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, verify that the path is correct and try again.

(I invoked the command using the "&" operator.)

How can I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eden
  • 3,696
  • 2
  • 24
  • 24

6 Answers6

172
& "C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode

or

[System.Diagnostics.Process]::Start("C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe", "C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode")

UPDATE: sorry I missed "(I invoked the command using the "&" operator)" sentence. I had this problem when I was evaluating the path dynamically. Try Invoke-Expression construction:

Invoke-Expression "& `"C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe`" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode"
Tomas Panik
  • 4,337
  • 2
  • 22
  • 31
  • Odd that the command in the original question, nearly exactly, works for me on a VM in PS2.0. But that same command now fails on PS4.0 system. Interestingly enough, I'm calling TestExecute also. I think I like option 2 best, I'll give that a shot, thanks! – EJA Jun 25 '14 at 18:37
53

It looks like you're specifying both the EXE and its first argument in a single string e.g; '"C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe" C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode'. This won't work. In general you invoke a native command that has a space in its path like so:

& "c:\some path with spaces\foo.exe" <arguments go here>

That is & expects to be followed by a string that identifies a command: cmdlet, function, native exe relative or absolute path.

Once you get just this working:

& "c:\some path with spaces\foo.exe"

Start working on quoting of the arguments as necessary. Although it looks like your arguments should be just fine (no spaces, no other special characters interpreted by PowerShell).

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 3
    Is there anyway to execute the powershell script without the full path, if the script and exe are in the same folder? Or is it possible to use a relative path? – FarFigNewton Sep 28 '12 at 21:38
  • 9
    If the exe is in the same folder then you can do this `.\foo.exe` and you can also use a relative path `..\..\foo.exe` and a relative path with spaces `& '..\..\path with spaces\foo.exe'`. – Keith Hill Sep 28 '12 at 21:51
  • When you have **pipes** in your command than you should use quotes: `cmd /c "C:\temp\strarc.exe -cjd:M:\ | C:\temp\strarc.exe -s:8 -xd:N:\"`. – larkee Dec 08 '14 at 18:00
5

In the Powershell, cd to the .exe file location. For example:

cd C:\Users\Administrators\Downloads

PS C:\Users\Administrators\Downloads> & '.\aaa.exe'

The installer pops up and follow the instruction on the screen.

randomguy
  • 357
  • 2
  • 9
3
  1. clone $args
  2. push your args in new array
  3. & $path $args

Demo:

$exePath = $env:NGINX_HOME + '/nginx.exe'
$myArgs = $args.Clone()

$myArgs += '-p'
$myArgs += $env:NGINX_HOME

& $exepath $myArgs
2

Here is a variation on some of the previous responses. Either of the below options can be added to a Powershell profile to suit your needs.

Use an alias

If you just want to run something without mucking up $env:PATH.

Set-Alias -Name docker -Value 'C:\Program Files\Docker\Docker\resources\bin\docker.exe'

Use a function

Or if you need more scripting.

function docker {
# below is a basic example - go crazy here
& 'C:\Program Files\Docker\Docker\resources\bin\docker.exe' @Args
}
67hz
  • 381
  • 3
  • 7
1

Not being a developer I found a solution in running multiple ps commands in one line. E.g:

powershell "& 'c:\path with spaces\to\executable.exe' -arguments ; second command ; etc

By placing a " (double quote) before the & (ampersand) it executes the executable. In none of the examples I have found this was mentioned. Without the double quotes the ps prompt opens and waits for input.

Taazar
  • 1,545
  • 18
  • 27
RichardE
  • 21
  • 1
  • Hi RichardE. Can you improve the formatting of your answer ? In edit mode, formatting help is provided, take a look at it. – Dorian Turba Feb 24 '20 at 10:39