How to find out the highest, installed Powershell version FROM CommandPrompt?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

How to find out the highest, installed Powershell version FROM CommandPrompt?

#1 Post by tobwz » 12 Apr 2022 23:14

As you know there is a system break in installation release procedure between PowerShell v5.1 and later Powershell versions (e.g. current v7.2.2).

Until v5.1 users could find out on CommandPrompt the maximum locally installed Powershell version by entering:

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "&{Get-Host | Select-Object Version}"

or

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "$psversiontable"

This does not work for newer Powershell versions any more.

If newer Powershell versions are installed: How can I find out their version label from Command Prompt (and DOS batch script) (without fiddling around in Registry and/or installation directory)?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to find out the highest, installed Powershell version FROM CommandPrompt?

#2 Post by aGerman » 16 Apr 2022 13:56

I guess this does still work (at least it works for me). The only difference is that you have to run "pwsh.exe" for the new versions.

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in ('2^>nul powershell.exe -nop -ep Bypass -c "$PSVersionTable.PSVersion|foreach{''+$_.major+'.'+$_.minor}"') do set "psver=%%i"
for /f "delims=" %%i in ('2^>nul pwsh.exe -nop -ep Bypass -c "$PSVersionTable.PSVersion|foreach{''+$_.major+'.'+$_.minor}"') do set "psver=%%i"
echo %psver%
pause
Errors are redirected. So, this should still work if none of the newer versions are installed.

Steffen

tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

Re: How to find out the highest, installed Powershell version FROM CommandPrompt?

#3 Post by tobwz » 27 Apr 2022 07:18

It works.
Thank you

Post Reply