Reputation: 275
I'm trying to check the powerscript version in a batch file and update the version if need be. Right now, I'm running $PSVersion to print out the version of powershell but I'm not sure how to parse it.
$PSVersion prints the entire table when all I need is the value(ex. 5.1.14393.103)
What I need to do is check that the $PSVersion is equal to version 5, and if not, then fetch the version and download it from the internet (I have this part covered)
Upvotes: 1
Views: 287
Reputation: 38708
Here is a method using the Windows registry:
@Echo Off
Set "bK=HKLM\SOFTWARE\Microsoft\PowerShell"
Set "eK=PowerShellEngine"
Set "kV=PowerShellVersion"
For /F "Tokens=2*" %%A In ('Reg Query "%bK%\3\%eK%" /V "%kV%" 2^>Nul^
^|^|Reg Query "%bK%\1\%eK%" /V "%kV%" 2^>Nul') Do Set "pV=%%~nB"
If Not "%pV%" GEq "5" Echo Installing a newer version of PowerShell
Timeout -1
If you're happy with the output, just replace the string Echo Installing a newer version of PowerShell
with your actual installation command and remove the Timeout -1
line.
Upvotes: 0
Reputation: 880
This should work on PS 2.0 (the default for Win 7)
if (($PSVersionTable.PSVersion.Major -lt 5) -and ($PSVersionTable.PSVersion.Minor -lt 1))
{#put your upgrade code here
}
else
{"PS is v 5.1"}
Upvotes: 0
Reputation: 10001
You could check for $PSVersionTable.PSVersion.Major
which in your example would be 5
Upvotes: 2