Difference between -command and -file

HI,

If I run this command line, I get an error.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Noexit -Command "C:\Program Files\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1"
C:\Program : Le terme C:\Program n'est pas reconnu comme nom d'applet de
commande, fonction, fichier de script ou programme excutable. Vrifiez
l'orthographe du nom, ou si un chemin d'accs existe, vrifiez que le chemin
d'accs est correct et ressayez.
Au caractre Ligne:1 : 1
+ C:\Program
Files\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveS ...
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program:String) [], CommandN
   otFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

If I run:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Noexit -File "C:\Program Files\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1"
Then there is no error message. I am not sure to understand the difference between both and the reason of this error. The PS1 is just an empty file.

February 15th, 2015 7:00am

The -Command parameter is for passing a command directly to the shell. If you open a PowerShell prompt and just type in C:\Program Files\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1, you should get the exact same error as when you pass it via the -Command parameter.

The -File parameter is for passing a file name, and PowerShell will execute the contents of that file.

You can read about the command line options in more detail here.

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 11:28am

Not quite.  Try this:

powershell.exe -ExecutionPolicy Bypass -Noexit -Command{C:\Program Files\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1}

It works just like typing it at a command prompt.

This also works correctlypowershell.exe -ExecutionPolicy Bypass -Noexit -Command C:\'Program Files'\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1

The error message only says that the "Path" was not found.  Probably because it does not exits. to check do this:

powershell.exe -ExecutionPolicy Bypass -Noexit -Command {Test-Path 'C:\Program Files\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1}

It will return true ort false.

February 15th, 2015 1:13pm

Hi,

The script and directories are existing.

I got no good result with

powershell.exe -ExecutionPolicy Bypass -Noexit -Command{C:\Program Files\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1}

But this one is good...

correctlypowershell.exe -ExecutionPolicy Bypass -Noexit -Command C:\'Program Files'\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1

I believe it is because there is a space in Program files and then 'Program files' is just saying to powershell to take the whole value.

I have variables:

"$var_ActiveSetupFolder\$var_FichierActiveSetup" 

Doing this values:

C:\Program Files\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1

To be sure the ' are well position in the variables, is it possible dynamically to position '' around each directory with 2 words? I would like doing so without the need to hardcode program files.

But  -file is working without changing anything.

Unless you have another suggestion?

Thanks,

Franois

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 10:34pm

Sorry but this was what I posted but the formatter on the forum seems to like to edit posts.

powershell.exe -ExecutionPolicy Bypass -Noexit -Command{C:\'Program Files'\PNG\InstTrousses\Trousses\Vcredist2005_Frv1\ActiveSetup\ActiveSetup.ps1}

Notice the single quotes.

February 15th, 2015 10:47pm

So the problem with the -file parameter is it does like space. Like I said in my previous post, would it be a way to enclose every element containing a space by single quotes?

I mean without hardcoding it. As example \Program files\ then replace it by \'program files'\. Or may be using a single quotes for the whole string...

Free Windows Admin Tool Kit Click here and download it now
February 16th, 2015 6:25am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics