ps1Liner :: Add PowerCLi version to the $PSVersionTable variable

ps1liner
What is one-liner? This is just one line of code (PowerShell/PowerCLi or whatever) intended to perform particular task. It may be very or sometimes very-very long, but is still one line only!
This one-liner adds PowerCLi version to $PSVersionTable environment variable.
The technique described by Microsoft MVP Jeffery Hicks in this article.

1Liner

$PSVersionTable.Add('PowerCLi Version', $(if ((Get-Module VMware.VimAutomation.Core).Version) { if ((Get-Module VMware.VimAutomation.Core).Version.ToString() -lt '6.5.2') { (Get-PowerCLIVersion).UserFriendlyVersion } else { (Get-Module VMware.VimAutomation.Core).Version } }))
  • Note, in the case of version 6.5.2 and above (in the near future), the $PSVersionTable.'PowerCLi Version' is a [version] data type and for old versions it is a simple [string].
$PSVersionTable.'PowerCLi Version'
$PSVersionTable.'PowerCLi Version' | Get-Member

Get-PowerCLIVersion

  • The familiar Get-PowerCLIVersion is deprecated and give us wrong version starting from PowerCLi version 6.5.2. Let’s compare the output.
Get-PowerCLIVersion
Get-Module VMware.VimAutomation.Core

03.ps1Liner_Get-PowerCLIVersion

How to use

  • Open your PowerShell profile script by notepad or your favorite editor.
notepad $PROFILE
ii $PROFILE
  • Copy-Paste the code to your PowerShell profile script and save the changes. Reopen PowerShell console and execute $PSVersionTable.
$PSVersionTable
  • Just a small requirement. For all these to work, you have to import VMware.PowerCLI module previously to run this one-liner. Add below line before one-liner.
Import-Module VMware.PowerCLI -wa SilentlyContinue

04.ps1Liner_PROFILE

Used cmdlets

  • The core cmdlets.
Get-Module
Import-Module
Get-PowerCLIVersion
  • Static methods
$PSVersionTable |gm -MemberType Method
$PSVersionTable.Add()

More 1Liners

Eject CDROM drive for all VMware VM

2 thoughts on “ps1Liner :: Add PowerCLi version to the $PSVersionTable variable

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s