In general, I know how to capture the output of a command into a variable in Powershell:
> $output = python3 --version
> Write-Host $output
Python 3.7.0
Python 3 prints its version but it's captured into the variable $output and nothing is displayed on the console. As expected:
> $output = python3 --version
> Write-Host $output
Python 3.7.0
But in the case of Python 2, it doesn't work. Python 2 prints its version but its displayed on the console instead of captured and the variable $output is empty.
> $output = python2 --version
Python 2.7.15
> Write-Host $output
>
I have tried some alternative syntaxes but so far nothing helped:
> $output = (python2 --version) # use a subcommand
Python 2.7.15
> $output = "$(python2 --version)" # enclose the subcommand into a string
Python 2.7.15
If I use it in Write-Command, the output looks like the following:
> Write-Host "Python version: $(python2 --version)"
Python 2.7.15
Python version:
>
How is Python 2 printing its version that it's not captured into a variable and is it possible to capture it anyhow?
stderr, you can capture the output with-ErrorVariable varnameor by mergingstderrintostdout:python2 --version 2>&1stderr, it doesn't make a sense to me. It's working fine when I merge both streams. In case of the-ErrorVariableoption - that's an option of what command, please?python2 --version, command status$?isTruebut when I runpython2 --version 2>&1, the command status isFalse. Why is that? I need that command to be successful.