1

In my powershell script I am trying to get the python version in windows server 2008. This script just prints the version in console but I am not getting the value into variable.

Code:

$python = & python -V
write-host "Python Version: " + $python

Expected output:

Python Version: Python 2.7.8

Actual output:

Python 2.7.8
Python Version:

Any one help me out.

5
  • 1
    I was unable to reproduce this with something simple like & 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 -V but the output worked fine. I'm also using PowerShell 3.0. What happens when you try $python = & ping -n 1 localhost and write-host "Python Version: $python".FYI variable will expand inside doublequotes. Commented Sep 19, 2014 at 13:30
  • Thanks @Matt. When I use <code>$python = & ping -n 1 localhost</code> code I can get the output of <code>Python 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</code>. Commented Sep 19, 2014 at 14:33
  • If I run the code <code>$python = &.\python.exe -V</code> then I get the output of <code>& : 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.</code> Commented Sep 19, 2014 at 14:35
  • In my example i was dot sourcing the python executable since it was not part of my path. That was not meant to be a fix or anything. It just means that in your current directory in PowerShell you dont have a python.exe. The ping command in Write-Host does work. Interesting. Also use a backtick for code in comments. Commented Sep 19, 2014 at 14:39
  • for code $puthon = & .\python.exe -V write-host "Python Version: $python" my output is Python 2.7.8 Python Version: Commented Sep 19, 2014 at 14:48

2 Answers 2

3

python.exe -V writes the version information to the Error output stream instead of the Success output stream. You get the observed output because the "error" message is printed before Write-Host is run, and the first command produces no output on the Success output stream to be assigned to $python, so the variable is empty when used in the Write-Host statement.

Change this line:

$python = & python -V

to this:

$python = & python -V 2>&1

and the problem will disappear.

Sign up to request clarification or add additional context in comments.

3 Comments

I wonder then if the newer versions of python change this since i dont have his issue with Python 3.1.4
@Matt Possibly. I tested with Python 3.3.2.
I have a typo. My version that i tested was 3.4.1
0

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)"

1 Comment

$python = & python -V 2>&1 this line solves my problem. Thanks @Matt for the response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.