Most of the time we have been used to cmdlets and it’s various features but do not know about the version and hence this post might help some of you to understand in finding different versions of PowerShell and how to switch between versions (provided multiple versions are installed on your Machine). I’m using Where clause syntax to explain version switching.
Code to find version-
PS:\>Get-Host | Select-Object Version
Version
——-
2.0
PS:\>$Host.Version
Major Minor Build Revision
—– —– —– ——–
2 0 -1 -1
PS:\>$PSVersionTable.PSVersion
Major Minor Build Revision
—– —– —– ——–
3 0 -1 -1
Switch between versions –
Goto Cmd Prompt and type the below command
C:\> Powershell -version 2
PS C:\>Get-Host | select Version
#v2 syntax
PS C:\> Get-Service | Where {$_.name -like “*SQL*”}
Status Name DisplayName
—— —- ———–
Stopped MSSQL$MICROSOFT… Windows Internal Database (MICROSOF…
Running SQLWriter SQL Server VSS Writer
PS C:\> Get-Service | where name -like “*SQL*”
Error Message –
where : Cannot bind parameter ‘FilterScript’. Cannot convert the “name” value of type “System.String” to type “System.Management.Automation.ScriptBlock”.
C:\> powershell -version 3
PS C:\> Get-Host | Select Version
PS C:\> Get-Service | where {$_.name -like “*SQL*”}
Status Name DisplayName
—— —- ———–
Stopped MSSQL$MICROSOFT… Windows Internal Database (MICROSOF…
Running SQLWriter SQL Server VSS Writer
#v3 syntax
PS C:\> Get-Service | Where name -like “*SQL*”
Status Name DisplayName
—— —- ———–
Stopped MSSQL$MICROSOFT… Windows Internal Database (MICROSOF…
Running SQLWriter SQL Server VSS Writer
Syntax for Where-Object
V2 Syntax : Where-Object { $_.<property name> }
V3 Syntax : Where-Object <property name> #Supports V2 syntax
WRONG !!!
PowerShell is diveded into the Host Programm and PowerShell himself.
The Host Programm is the Interface and PowerShell is the Engine build into the host Programm.
For example do a remote session and enter Get-Host or user teh PowerGUI PowerShell Editor and enter Get-Host you will see you get the Host back not PowerShell!
If you use Get-Host or simply the build in Variable $Host you are quering the Version of the Host NOT of the PowerShell engine!
To correct query the PowerShell Version use the build in Variable $PSVersionTable If this Variable is not there you have powerShell Version 1.0 !
LikeLike
Thanks for correcting. Good information.
LikeLike
Pingback: PowerShell – Identify and switch between installed PowerShell Versions | jamesmos