Determine installed PowerShell version

2018-12-31 22:59发布

How can I determine what version of PowerShell is installed on a computer, and indeed if it is installed at all?

标签: powershell
25条回答
伤终究还是伤i
2楼-- · 2018-12-31 23:22

I would use either Get-Host or $PSVersionTable. As Andy Schneider points out, $PSVersionTable doesn't work in version 1; it was introduced in version 2.

get-host

Name             : ConsoleHost
Version          : 2.0
InstanceId       : d730016e-2875-4b57-9cd6-d32c8b71e18a
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-GB
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

$PSVersionTable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.4200
BuildVersion                   6.0.6002.18111
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1
查看更多
初与友歌
3楼-- · 2018-12-31 23:22

The below cmdlet will return the PowerShell version.

$PSVersionTable.PSVersion.Major
查看更多
旧人旧事旧时光
4楼-- · 2018-12-31 23:24

Microsoft's recommended forward compatible method for checking if PowerShell is installed and determining the installed version is to look at two specific registry keys. I've reproduced the details here in case the link breaks.

According to the linked page:

Depending on any other registry key(s), or version of PowerShell.exe or the location of PowerShell.exe is not guaranteed to work in the long term.

To check if any version of PowerShell is installed, check for the following value in the registry:

  • Key Location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1
  • Value Name: Install
  • Value Type: REG_DWORD
  • Value Data: 0x00000001 (1

To check whether version 1.0 or 2.0 of PowerShell is installed, check for the following value in the registry:

  • Key Location: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine
  • Value Name: PowerShellVersion
  • Value Type: REG_SZ
  • Value Data: <1.0 | 2.0>
查看更多
只靠听说
5楼-- · 2018-12-31 23:28

Use Get-Host to get the details for the PowerShell version:

PS C:\Users\ashash001c> Get-Host

Name             : ConsoleHost
Version          : 2.0
InstanceId       : ##################################
UI               : System.Management.Automation.Internal.Host.InternalHostUserI
                   nterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
查看更多
长期被迫恋爱
6楼-- · 2018-12-31 23:29

You can verify that Windows PowerShell version installed by completing the following check:

  1. Click Start, click All Programs, click Accessories, click Windows PowerShell, and then click Windows PowerShell.
  2. In the Windows PowerShell console, type the following command at the command prompt and then press ENTER:

    Get-Host | Select-Object Version

You will see output that looks like this:

Version
-------
3.0

http://www.myerrorsandmysolutions.com/how-to-verify-the-windows-powershell-version-installed/

查看更多
裙下三千臣
7楼-- · 2018-12-31 23:30

Since the most helpful answer didn't address the if exists portion, I thought I'd give one take on it via a quick-and-dirty solution. It relies on PowerShell being in the path environment variable which is likely what you want. (Hat tip to the top answer as I didn't know that.) Paste this into a text file and name it

Test Powershell Version.cmd

or similar.

@echo off
echo Checking powershell version...
del "%temp%\PSVers.txt" 2>nul
powershell -command "[string]$PSVersionTable.PSVersion.Major +'.'+ [string]$PSVersionTable.PSVersion.Minor | Out-File ([string](cat env:\temp) + '\PSVers.txt')" 2>nul
if errorlevel 1 (
 echo Powershell is not installed. Please install it from download.Microsoft.com; thanks.
) else (
 echo You have installed Powershell version:
 type "%temp%\PSVers.txt"
 del "%temp%\PSVers.txt" 2>nul
)
timeout 15
查看更多
登录 后发表回答