4

I am trying to make a simple script to set my gcc variables. as a .bat file.

the variable is set like this

$env:Path += ";C:\Users\Brett\Compilers\MinGW\bin"

this runs just fine when I type/paste it into power shell.

but when I paste into a script myscript.bat, and run it through powershell I get this error:

C:\Users\Brett\Compilers>"$env:Path += ";C:\Users\Brett\Compilers\MinGW\bin""
The filename, directory name, or volume label syntax is incorrect.
PS C:\Users\Scruffy\Compilers>
Snuff Movie
  • 384
  • 5
  • 12

3 Answers3

5

PowerShell is a seperate execution enviroment from with Windows Command Line (cmd.exe)

If you want to run powershell commands from a batch file you need to save the powershell script (.ps1) and pass it into powershell.exe as a command line argument.

Example:

powershell.exe -noexit c:\scripts\test.ps1

More Information is available here on Microsoft TechNet

dmck
  • 7,801
  • 7
  • 43
  • 79
  • ok but the error doesnt specifically relate to powershell. the same error is given when I rub the script in cmd. also, powershell does seem to run some batch files, it always has for me before. i only use it in preference to cmd because it's easier to read. if you want to see, same "echo hello" inside a .bat file and run it through powershell. works fine. basically all I need to do is add an entry to the path variable inside a power shell script. or a batch. but powershell is easier to read. – Snuff Movie Oct 01 '12 at 02:22
  • nevermind, I got it to work by saving set PATH=%PATH%;"C:\Users\Brett\Compilers\MinGW\bin" in a bat file and running it through powershell. I guess it was a syntax error. – Snuff Movie Oct 01 '12 at 02:30
  • 4
    PowerShell doesn't understand .bat files. When you run one from within PowerShell, it's kicking off CMD and having it run the .bat. So you have to use the same syntax as you would in a regular command prompt. If you want to save PowerShell syntax to a file and run it on demand, you need to save it as a .ps1 file. – Daniel Richnak Oct 01 '12 at 04:29
1

In general, leave batch stuff to .BAT files and put PowerShell stuff into .ps1 files.

I can duplicate your results here - but those are to be expected. Cmd.exe sees a string then a path and then gets quite confused as the syntax is not one that the command prompt can handle. So it gives that error message.

If you want to add stuff to your path, then why not put the statement inside a .ps1 script file?

Thomas Lee
  • 1,158
  • 6
  • 13
0

As mentioned by others, you need to save the code in a .ps1 file and not .bat.

This line (from Setting Windows PowerShell path variable) will do the trick:

$env:Path = $env:Path + ";C:\Users\Brett\Compilers\MinGW\bin"

Or even shorter:

$env:Path += ";C:\Users\Brett\Compilers\MinGW\bin" 
Community
  • 1
  • 1
Joost
  • 1,126
  • 1
  • 10
  • 19