How can I determine what version of PowerShell is installed on a computer, and indeed if it is installed at all?
问题:
回答1:
Use $PSVersionTable.PSVersion
to determine the engine version. If the variable does not exist, it is safe to assume the engine is version 1.0
.
Note that $Host.Version
and (Get-Host).Version
are not reliable - they reflect
the version of the host only, not the engine. PowerGUI,
PowerShellPLUS, etc. are all hosting applications, and
they will set the host\'s version to reflect their product
version — which is entirely correct, but not what you\'re looking for.
PS C:\\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
回答2:
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:
To determine if PowerShell is installed, you can check the registry for the existence of
HKEY_LOCAL_MACHINE\\Software\\Microsoft\\PowerShell\\1\\Install
and
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\PowerShell\\3
and, if it exists, whether the value is 1 (for installed), as detailed in the blog post Check if PowerShell installed and version.
To determine the version of PowerShell that is installed, you can check the registry keys
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellEngine\\PowerShellVersion
and
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\PowerShell\\3\\PowerShellEngine\\PowerShellVersion
To determine the version of PowerShell that is installed from a .ps1 script, you can use the following one-liner, as detailed on PowerShell.com in Which PowerShell Version Am I Running.
$isV2 = test-path variable:\\psversiontable
The same site also gives a function to return the version:
function Get-PSVersion {
if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]\"1.0.0.0\"}
}
回答4:
You can look at the built in variable, $psversiontable
. If it doesn\'t exist, you have V1. If it does exist, it will give you all the info you need.
1 > $psversiontable
Name Value
---- -----
CLRVersion 2.0.50727.4927
BuildVersion 6.1.7600.16385
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
回答5:
Just want to add my 2 cents here.
You can directly check the version with one line only by invoking powershell externally, such as from Command Prompt
powershell -Command \"$PSVersionTable.PSVersion\"
EDIT:
According to @psaul you can actually have one command that is agnostic from where it came (CMD, Powershell or Pwsh), thank you for that.
powershell -command \"(Get-Variable PSVersionTable -ValueOnly).PSVersion\"
I\'ve tested and it worked flawlessly on both CMD and Powershell
回答6:
You can verify that Windows PowerShell version installed by completing the following check:
- Click Start, click All Programs, click Accessories, click Windows PowerShell, and then click Windows PowerShell.
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:
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>
回答8:
I found the easiest way to check if installed was to:
- run a command prompt (Start, Run,
cmd
, then OK) - type
powershell
then hit return. You should then get the PowerShellPS
prompt:
C:\\Users\\MyUser>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\\Users\\MyUser>
You can then check the version from the PowerShell prompt by typing $PSVersionTable.PSVersion
:
PS C:\\Users\\MyUser> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
PS C:\\Users\\MyUser>
Type exit
if you want to go back to the command prompt (exit
again if you want to also close the command prompt).
To run scripts, see http://ss64.com/ps/syntax-run.html.
回答9:
$host.version
is just plain wrong/unreliable. This gives you the version of the hosting executable (powershell.exe, powergui.exe, powershell_ise.exe, powershellplus.exe etc) and not the version of the engine itself.
The engine version is contained in $psversiontable.psversion
. For PowerShell 1.0, this variable does not exist, so obviously if this variable is not available it is entirely safe to assume the engine is 1.0, obviously.
回答10:
Use:
$psVersion = $PSVersionTable.PSVersion
If ($psVersion)
{
#PowerShell Version Mapping
$psVersionMappings = @()
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.1.14393.0\';FriendlyName=\'Windows PowerShell 5.1 Preview\';ApplicableOS=\'Windows 10 Anniversary Update\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.1.14300.1000\';FriendlyName=\'Windows PowerShell 5.1 Preview\';ApplicableOS=\'Windows Server 2016 Technical Preview 5\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.0.10586.494\';FriendlyName=\'Windows PowerShell 5 RTM\';ApplicableOS=\'Windows 10 1511 + KB3172985 1607\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.0.10586.122\';FriendlyName=\'Windows PowerShell 5 RTM\';ApplicableOS=\'Windows 10 1511 + KB3140743 1603\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.0.10586.117\';FriendlyName=\'Windows PowerShell 5 RTM 1602\';ApplicableOS=\'Windows Server 2012 R2, Windows Server 2012, Windows Server 2008 R2 SP1, Windows 8.1, and Windows 7 SP1\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.0.10586.63\';FriendlyName=\'Windows PowerShell 5 RTM\';ApplicableOS=\'Windows 10 1511 + KB3135173 1602\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.0.10586.51\';FriendlyName=\'Windows PowerShell 5 RTM 1512\';ApplicableOS=\'Windows Server 2012 R2, Windows Server 2012, Windows Server 2008 R2 SP1, Windows 8.1, and Windows 7 SP1\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.0.10514.6\';FriendlyName=\'Windows PowerShell 5 Production Preview 1508\';ApplicableOS=\'Windows Server 2012 R2\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.0.10018.0\';FriendlyName=\'Windows PowerShell 5 Preview 1502\';ApplicableOS=\'Windows Server 2012 R2\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'5.0.9883.0\';FriendlyName=\'Windows PowerShell 5 Preview November 2014\';ApplicableOS=\'Windows Server 2012 R2, Windows Server 2012, Windows 8.1\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'4.0\';FriendlyName=\'Windows PowerShell 4 RTM\';ApplicableOS=\'Windows Server 2012 R2, Windows Server 2012, Windows Server 2008 R2 SP1, Windows 8.1, and Windows 7 SP1\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'3.0\';FriendlyName=\'Windows PowerShell 3 RTM\';ApplicableOS=\'Windows Server 2012, Windows Server 2008 R2 SP1, Windows 8, and Windows 7 SP1\'}
$psVersionMappings += New-Object PSObject -Property @{Name=\'2.0\';FriendlyName=\'Windows PowerShell 2 RTM\';ApplicableOS=\'Windows Server 2008 R2 SP1 and Windows 7\'}
foreach ($psVersionMapping in $psVersionMappings)
{
If ($psVersion -ge $psVersionMapping.Name) {
@{CurrentVersion=$psVersion;FriendlyName=$psVersionMapping.FriendlyName;ApplicableOS=$psVersionMapping.ApplicableOS}
Break
}
}
}
Else{
@{CurrentVersion=\'1.0\';FriendlyName=\'Windows PowerShell 1 RTM\';ApplicableOS=\'Windows Server 2008, Windows Server 2003, Windows Vista, Windows XP\'}
}
You can download the detailed script from How to determine installed PowerShell version.
回答11:
To check if PowerShell is installed use:
HKLM\\Software\\Microsoft\\PowerShell\\1 Install ( = 1 )
To check if RC2 or RTM is installed use:
HKLM\\Software\\Microsoft\\PowerShell\\1 PID (=89393-100-0001260-00301) -- For RC2
HKLM\\Software\\Microsoft\\PowerShell\\1 PID (=89393-100-0001260-04309) -- For RTM
Source: this website.
回答12:
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
回答13:
The easiest way to forget this page and never return to it is to learn the Get-Variable
:
Get-Variable | where {$_.Name -Like \'*version*\'} | %{$_[0].Value}
There is no need to remember every variable. Just Get-Variable
is enough (and \"There should be something about version\").
回答14:
You can also call the \"host\" command from the PowerShell commandline. It should give you the value of the $host
variable.
回答15:
The below cmdlet will return the PowerShell version.
$PSVersionTable.PSVersion.Major
回答16:
Try it with following command:
Get-Host
Seen here
回答17:
Extending the answer with a select operator:
Get-Host | select {$_.Version}
回答18:
Usually you get it using the Major number that you can get like this:
$PSVersionTable.PSVersion.Major
回答19:
Use the Get-Host command. The second line of the result is the version.
回答20:
get powershell version by $host.Version
command.
output:
Major Minor Build Revision
----- ----- ----- --------
5 1 17134 407
回答21:
Try ;)
((Get-Host).Version).Major
回答22:
I needed to check the version of PS and then run the appropriate code. Some of our servers run v5, others v4. This means that some functions, like compress, may or may not be available.
This is my solution:
if ($PSVersionTable.PSVersion.Major -eq 5) {
#Execute code available in 5, like Compress
Write-Host \"You are running version 5\"
}
else {
#Use a different process
Write-Host \"This is version $PSVersionTable.PSVersion.Major\"
}
回答23:
# Get PowerShell Version
function Global:version
{
$PSVersionTable.PSVersion
}
回答24:
Command $PSVersionTable.PSVersion
is helpfull.
Major Minor Patch PreReleaseLabel BuildLabel ----- ----- ----- --------------- ---------- 6 1 0
回答25:
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