The powershell command is not running in the .bat file?

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

the variable is set like this:

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

This works fine when I print / paste it into the power shell.

but when I insert myscript.bat in the script 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> 
+4
source share
3 answers

PowerShell - stand-alone runtime from the 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 to powershell.exe as a command line argument.

Example:

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

Additional information is available here at Microsoft TechNet.

+5
source

In general, leave batch material in .BAT files and put PowerShell material in .ps1 files.

I can duplicate your results here, but this can be expected. Cmd.exe sees the line and then the path, and then is rather confusing, since the syntax does not match the command line. So this is an error message.

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

+1
source

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

This line (from Configuring 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" 
0
source

Source: https://habr.com/ru/post/1437088/


All Articles