4

Until yesterday when i executed a script inside powershell using either

"foo.bat"

or

"cmd /c commandhere"

It would run the command inside the Powershell and also print the output. Now since today it does not find "cmd" anymore and when use "cmd.exe" or execute a bat script it will open the cmd window instead of running the script inside powershell.

Does someone know if this is configurable or how i can get back to executing scripts just inside the powershell?

Edit: My env is:

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
beberlei
  • 4,297
  • 1
  • 23
  • 25

3 Answers3

1

The PATHEXT environment variable is what determines which files are considered 'executable' and thus show up inline in the console output. Print the value of $env:pathext and make sure .BAT is included there.

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

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

This should then cause .bat files to display output inline in the Powershell console. To make it take effect you have to either restart Powershell or else add .BAT to the Powershell process's own copy of the PATHEXT variable:

$env:pathext = "$env:pathext;.BAT"
Justin Dunlap
  • 181
  • 2
  • 4
1

To add to Justin's answer, "Machine" might not be the target you're looking for here, despite the fact that the code snipet does work.

First of all, if PATHEXT does not contain ".BAT" or ".CMD" anymore, the problem is elsewhere.

If this is a correct behavior in your environment, then read on.

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

When I say "process", I really mean some other executable, launched by other means than your script. Processes launched by your script (as your bat/cmd command) are child-processes and inherit the current environment, thus all the defined variables, unless you specify it otherwise like a second thread "start-process".

For what I can read, this is what you're trying to achieve. the PATHEXT variable, with it's new added content, will only live through the PowerShell script/process and will be available to your bat/cmd script.

Jay
  • 1,635
  • 16
  • 14
0

Use invoke-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 the actuall results

$results = invoke-expression -command "c:\batfile.bat"
Elvar
  • 444
  • 1
  • 4
  • 13