Solved Batch get powershell version

July 25, 2015 at 07:45:23
Specs: Windows 7, 1.3GHz, 4GB
I'm working on a batch file that checks for which powershell version is installed.

The PS

get-host
command outputs something similiar to this:


Name             : ConsoleHost
Version          : 2.0
InstanceId       : b9a10d0a-123e-43eb-bfe1-201e81e28630
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : nb-NO
CurrentUICulture : nb-NO
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

I'm no powershell master, though I did manage to come up with this batch script:
for /f "skip=2 tokens=2 delims=:" %%A in ('powershell -command "& {get-host}"') do (
        echo %%A
)

However, this won't work. What am I doing wrong?

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


See More: Batch get powershell version

Report •

#1
July 25, 2015 at 07:49:13

Report •

#2
August 9, 2015 at 16:06:00
Thanks, it helped shed some light on some of my other problems with PS.

I figured it out myself by parsing the output of the "get-host" command:

for /f "skip=3 tokens=2 delims=:" %%A in ('powershell -command "get-host"') do (
	set /a n=!n!+1
	set c=%%A
	if !n!==1 set PSVersion=!c!
)
set PSVersion=!PSVersion: =!

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


Report •

#3
August 14, 2015 at 13:56:20
✔ Best Answer
I should probably point out that the correct way to get the PS version is through the $PSVersionTable . . .

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question