Your code should work as designed. For whatever reason it is sending output to console instead of the variable. This code does work for me on PowerShell 3.0
$python = & python -V
write-host "Python Version: " + $python
You even mention in comments that similar commands work as expected like the following.
$python = & ping -n 1 localhost
write-host "Python Version: $python"
While I am not sure of the root cause you can write some code that will force capture of standard output into a System.Diagnostics.Process object. I took the code from another SO answer.
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "C:\Python34\python.exe"
$pinfo.Arguments = "-V"
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
Write-Host "Python Version: $stdout"
I am not sure why a simple two liner does not work for you. What happens when you do something like this?. Again I dont have Python in my Path variable so i used the fullname of the file in my example.
Write-Host "Python Version: $(&"C:\Python34\python.exe" -V)"
or
Write-Host "Python Version: $(& python.exe -V)"
& ping -n 1 localhost. So I actually intalled Python to test just in case and your code does work. I had to dot source python.exe for testing but$python = &.\python.exe -Vbut the output worked fine. I'm also using PowerShell 3.0. What happens when you try$python = & ping -n 1 localhostandwrite-host "Python Version: $python".FYI variable will expand inside doublequotes.python.exe. The ping command inWrite-Hostdoes work. Interesting. Also use a backtick for code in comments.$puthon = & .\python.exe -V write-host "Python Version: $python"my output isPython 2.7.8 Python Version: