0

I have a powershell script that runs an .exe tool.

& tool.exe

If I run the script from a cmd console, the exe output is not written through the cmd console...

C:\>powershell .\script.ps1

Insead, if I run the script from a Powershell console everything is alright.

Do you know how to make the exe output visible from the cmd console?

toscanelli
  • 1,202
  • 2
  • 17
  • 40
  • Why are you using, cmd.exe to run PowerShell - which starts the consolehost to run a .ps1 file to call an exe that needs cmd.exe? Use it directly to run your .exe. Anything you do in cmd.exe you can do directly in the consolehost. BTW, I just tested what you have here with a few .exes, and it works without the need for Write-*. So, it seems your exe is the root cause. Output to the screen is the PowerShell default. Write-Host should be avoided, unless you need to use color, or other very specific screen formatting needs. Use Out-Host, or Write-Output instead, if you have to use something. – postanote May 06 '19 at 22:32
  • I didn't know the difference between Write-host and Write-Output. Now it's clear for me, thanks. About the ps script, is a complex script that do many things, among others, run a .exe. In my case, the problem is that the exe output appears in the virtual ps console that is opened when I use "powershell" command in the cmd.exe, but it's is not brought to the cmd console. – toscanelli May 07 '19 at 08:59
  • For exe, cmd.exe gets called, thus a new PowerShell session. Output redirection is defined here: About Redirection : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-6 --- https://stackoverflow.com/questions/19220933/powershell-pipe-external-command-output-to-another-external-command --- https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right --- https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx – postanote May 07 '19 at 21:23

1 Answers1

1

I think I resolved like this:

& tool.exe | Write-Host

It's not very smart, but it works...

toscanelli
  • 1,202
  • 2
  • 17
  • 40