2

Can anyone explain to me why:

iex "C:\Program Files\test\test.exe"

Returns:

C:\Program : The term 'C:\Program' 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. At line:1 char:1 + C:\Program Files\test\test.exe + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Program:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException


I've tried to get this working multiple different ways:

  • Wrapping the text in ()
  • Putting the string into a variable and passing it as a variable instead
  • Using single quotes
  • Using double quotes

I don't know how else I can get it to realize that the entire string must be run, not just the first word.


Post-answered example

The question has been answered. Here is something I was trying to get working:

$tool = "C:\Windows\System32\cmd.exe"
$param = "/c ping google.com -n 1"
$test = & $tool $param
Write-Host $test

It turns out that the line with & does NOT work with double quotes "" in this instance, and actually worked without them. I think this has to do with there being arguments/parameters involved.

1 Answer 1

5

Use the & operator together with quotes:

& "C:\Program Files\test\test.exe"

From help about_operators:

  & Call operator
     Runs a command, script, or script block. The call operator, also known as
     the "invocation operator," lets you run commands that are stored in
     variables and represented by strings. Because the call operator does not
     parse the command, it cannot interpret command parameters. 

         C:\PS> $c = "get-executionpolicy"
         C:\PS> $c
         get-executionpolicy

         C:\PS> & $c
         AllSigned
Sign up to request clarification or add additional context in comments.

3 Comments

So is this thing only failing because it's a call to an executable in a directory, and MUST be preceded by an & symbol?
You need to use quotes because of the space, but when you do that, PowerShell thinks you've just entered a string. For example, if you typed "hello" in quotes, PowerShell wouldn't try to execute it. It's just a string. That's why you need the & "call operator" when the string represents a command.
Alright, looks like I got what I needed to work without the quotes. I'll put it in my question so you know what I'm talking about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.