powershell command not running in .bat file? - shell

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>

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

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?

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"

Related

powershell vs cmd regarding not closing the cmd/powershell window after execution

I have a ipsxe-comp-vars.bat batch file which sets environment variables for the intel fortran compiler, as well as for the c and c++ compilers. I create a shortcut to it, I right-click the shortcut, go to target and put the path of cmd.exe and a /K (such that the command prompt won't close) and a space before the path to the .bat in it, click on apply and ok. Then I can pin the shortcut to the taskbar. When I click on it : the bat is executed and at the end, I am back to the command prompt, the cmd window don't close. I can then start compiling etc in this command windows.
At the same place as the .bat file I created a ipsxe-comp-vars.ps1 file :
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
Set-Location "C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.4.210\windows\bin"
& "C:\Windows\System32\cmd.exe" /E:ON /V:ON "C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.4.210\windows\bin\ipsxe-comp-vars.bat" intel64 vs2015
Then I create a shortcut for this .ps1 file, right-click the shortcut, and modify it's target as follows :
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit - command "& 'C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.4.210\windows\bin\ipsxe-comp-vars.ps1'"
Then I can also pin this new shortcut to the taskbar. When I click on it : the .ps1 file is executed, but the window closes, annihilating the leverage of the very notion of shorcut in this case.
What am I doing wrong ?
Try the following which is how my shortcuts with -noexit are set up.
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -File "C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.4.210\windows\bin\ipsxe-comp-vars.ps1"
-Command
Executes the specified commands (and any parameters) as though they were typed at the PowerShell command prompt, and then exits, unless the NoExit parameter is specified. Essentially, any text after -Command is sent as a single command line to PowerShell (this is different from how -File handles parameters sent to a script).
-File []
Runs the specified script in the local scope ("dot-sourced"), so that the functions and variables that the script creates are available in the current session. Enter the script file path and any parameters. File must be the last parameter in the command, because all characters typed after the File parameter name are interpreted as the script file path followed by the script parameters and their values.
You can include the parameters of a script, and parameter values, in the value of the File parameter. For example: -File .\Get-Script.ps1 -Domain Central Note that parameters passed to the script are passed as literal strings (after interpretation by the current shell). For example, if you are in cmd.exe and want to pass an environment variable value, you would use the cmd.exe syntax: powershell -File .\test.ps1 -Sample %windir% If you were to use PowerShell syntax, then in this example your script would receive the literal "$env:windir" and not the value of that environmental variable: powershell -File .\test.ps1 -Sample $env:windir
Typically, the switch parameters of a script are either included or omitted. For example, the following command uses the All parameter of the Get-Script.ps1 script file: -File .\Get-Script.ps1 -All
https://learn.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help?view=powershell-6

How to set ps as shortcut to open powershell

The question says it all. I want to learn and use powershell as my go to terminal in windows and I want powershell to open if I type Win+R followed by ps. Much like how cmd is used to open command prompt.
You can create a symbolic link (similar to a shortcut) to Powershell with any name you want.
This will create one called ps.exe in the powershell folder, this folder is already listed in PATH so will enable you to run ps from the RUN box like you want.
mklink %SystemRoot%\system32\WindowsPowerShell\v1.0\ps.exe %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Be sure to run the command in an elevated command prompt.
The "default" command for opening PowerShell is powershell. If you want to make it ps, your best bet is to create a batch file named ps.bat containing the single line
#powershell %*
and place it in one of the directories named in your system PATH variable.

Problems Executing Powershell from .cmd file

I am attempting to run a build task from a .cmd file where Powershell extracts a zip file, which helps bypass a problem with Visual Studio's limit on the number of directory characters. However, I am having problems getting the Powershell command to execute correctly. I've tried a number of variations with the quotations, and I either get a termination error, or the Powershell command outputs as a string with the zip file not extracted. Below is an example of my current .cmd file:
//%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
set Source=%1%directory.zip
set Destination=%1%directory
powershell -Command $pscmd = '{Add-Type -assembly "system.io.compression.filesystem";[io.compression.zipfile]::ExtractToDirectory("%Source%", "%Destination%");}'; Write-Host $pscmd;
I'm very open to a number of variations that can get this to work, provided that this task runs on the command line, uses Powershell, and can be executed from a .cmd file, which is triggered by our app's build process. I'll be happy to provide additional information if needed. Thanks!
This was a strange one. Your code above has some sort of hidden character in it. I took the code and opened it in notepad, saved as ANSI, and when you type it to command line or open it again in a new instance of notepad you can see the error.
Neither add-type nor ExtractToDirectory give output, so I removed your pscmd var.
I would open your existing script, save as ansi as a new file name, delete the original, rename the new one back to the original name.
Here is what I came up with to troubleshoot your script, and it works on my machine.
I named my script L:\util\unzip.cmd
setlocal
//%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
set _Source='%1\directory.zip'
set _Destination='%1\directory'
echo _Source=%_Source%
echo _Destination=%_Destination%
set _c1=Add-Type -assembly system.io.compression.filesystem;
set _c2=[io.compression.zipfile]::ExtractToDirectory(%_Source%, %_Destination%)
echo _c1=%_c1%
echo _c2=%_c2%
set _Command=^& {%_c1% %_c2%};
: to echo, use double quotes or cmd thinks text after & is another command
echo _Command="%_Command%"
pause
powershell -Command "%_Command%"
endlocal
I ran it like this, and it worked: unzip.cmd L:\util
I'll bet this this info, you are good to go.

Copy-Item command working in Powershell Command line but not in bat file

Trying to copy files from one folder to another using Copy-Item command. The following command is working in powershell command line but throwing error when run inside a *.bat file:
Copy-Item C:\script\* D:\
It throws the following error:
'Copy-Item' is not recognized as an internal or external command,
operable program or batch file.
You can't run PowerShell cmdlets in batch directly, instead you have to invoke powershell.exe and pass the command to it:
powershell -Command "Copy-Item C:\script* D:\"
Or, you need to make not a *.bat file but a *.ps1 file. These are run in Powershell, and therefore accept any Powershell commands, so Copy-Item will work without extra efforts.

How to call an executable file directly in notepad++?

In windows,I use the Notepad++ to write tex file, in the "run..." dialog,I input that:
cmd /k D:\CTEX\MiKTeX\miktex\bin\xelatex $(FULL_CURRENT_PATH)
then run it,however the result shows that 'xdvipdfmx' is not an executable file. But I am sure that I have add its path to the system environment variable,and when I direct run it in the terminal, it's ok.
So,I want to know what I should do to run it in the notepad++ correctly.
Try these improvements:
enclose paths into quotes to avoid problems with spaces
add .exe to executable file name
test the full command in command prompt to see if it works (replace $(FULL_CURRENT_PATH)) with actual file name
please let me know the result
Your example after changes:
cmd /k "D:\CTEX\MiKTeX\miktex\bin\xelatex.exe" "$(FULL_CURRENT_PATH)"
Test it in command prompt like:
cmd /k "D:\CTEX\MiKTeX\miktex\bin\xelatex.exe" "D:\Data\MyDoc1.tex"

Resources