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)"
$python = & ping -n 1 localhost
code I can get the output ofPython Version: Pinging AWSRIA185.one.ads.bms.com [::1] with 32 bytes of data: Reply from ::1: time<1ms Ping statistics for ::1: Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), App roximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms
. – Mohan Sep 19 '14 at 14:33$python = &.\python.exe -V
then I get the output of& : The term '.\python.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
– Mohan Sep 19 '14 at 14:35