Powershell unexpectedly opens cmd.exe to execute a bit.

Until yesterday, when I ran the script inside powershell using

"foo.bat" 

or

 "cmd /c commandhere" 

It will execute the command inside Powershell and also print the output. Now from today it no longer finds "cmd", and when using "cmd.exe" or executing a bat script, it will open the cmd window instead of running the script inside powershell.

Does anyone know if this is customizable, or how can I get back to running scripts only inside powershell?

Edit: My env:

 PS C:\> echo $env:Path C:\Windows\system32;C:\Windows\;C:\Windows\System32\Wbem;C:\Windows\system32\WindowsPowerShell\v1.0\;C:\xampp\php;C:\code\azure\bin;%SystemRoot%\system32\WindowsPowerShell\v1.0\;C:\Windows\System32 
+4
source share
3 answers

Use expression expression

http://technet.microsoft.com/en-us/library/dd347550.aspx

I think it should be something like this

 invoke-expression -command "c:\batfile.bat" 

or better yet to get bottom line results

 $results = invoke-expression -command "c:\batfile.bat" 
0
source

The PATHEXT environment variable determines which files are considered "executable" and, therefore, appear in a line at the console output. Print the value of $env:pathext and make sure BAT is enabled there.

If the BAT is not listed, you can add it back using the following commands in Powershell:

 $pathext = [Environment]::GetEnvironmentVariable("PATHEXT", "Machine") [Environment]::SetEnvironmentVariable("PATHEXT", $pathext+';.BAT', "Machine") 

This should cause the .bat files to display output in the Powershell console. For it to take effect, you need to either restart Powershell or add a BAT to the Powershell process of your own copy of the PATHEXT variable:

 $env:pathext = "$env:pathext;.BAT" 
0
source

To add to Justin's answer, β€œMachine” may not be the goal you are looking for here, even though snipet code is working.

First of all, if PATHEXT no longer contains ".BAT" or ".CMD", the problem is elsewhere.

If this is the correct behavior in your environment, then read it.

 $pathext = [Environment]::GetEnvironmentVariable("PATHEXT", "Process") [Environment]::SetEnvironmentVariable("PATHEXT", $pathext+';.BAT', "Process") 
  • The target "Machine" places the variable in the "HKEY_LOCAL_MACHINE" registry, ready for the following processes to read
  • The User target places the variable in the HKEY_CURRENT_USER registry, ready to read for the next process
  • The Process target puts the variable in operational state right now for the current PowerShell.exe process.

When I say "process", I really mean a different executable file launched by other means than your script. The processes started by your script command (like your bat / cmd command) are child processes and inherit the current environment, so all defined variables, unless you specify it otherwise than the "initial process" of the second thread.

What I can read for is what you are trying to achieve. the PATHEXT variable with the newly added content will only work through the PowerShell script / process and will be available to your bat / cmd script.

0
source

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


All Articles