Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Enhancing PSVersionTable

Posted on August 24, 2017August 24, 2017

Not too long ago I posted a PowerShell function that could provide detail abut the PowerShell engine driving your current PowerShell session. I like having a function that writes an object to the pipeline, can take parameters and offer help documentation. But there’s an alternative approach you could also take.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Most of the information is already in $PSVersiontable. So why not use that as a vehicle? This variable is a simple hashtable that is constructed every time you start a PowerShell session. This means you can add key/value pairs to it with the Add() method.

$PSVersionTable.add("Path",(Get-Process -id $pid).Path)
$PSVersionTable.add("Host",$host.name)
$PSVersionTable.add("Culture",$host.CurrentCulture.DisplayName)
$PSVersionTable.add("Installed",(get-item $PSVersionTable.path).CreationTime)
$PSVersionTable.add("LanguageMode",$host.Runspace.LanguageMode)
$PSVersionTable.add("Computername",$(hostname))

Remember that this will only change the variable for the current PowerShell session. If you wanted to always have this extended information, you would need to add these lines to your PowerShell profile script or create a simple function. If you go that route, you’ll want to include some testing and error handling because you can’t add a value that already exists. When you are done you get results like these:

image

imageimageimage

$PSVersiontable is not an ordered hashtable so you can't predict the display order, but it does offer a quick and dirty way to store additional information you might want to reference.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Enhancing PSVersionTable”

  1. Bruce says:
    September 13, 2017 at 5:32 pm

    This was awesome but I ran into an issue when trying to remote –

    This error would occur

    WriteErrorStream : True
    Exception : System.Management.Automation.Remoting.PSRemotingDataStructureException: An error has occurred
    which Windows PowerShell cannot handle. A remote session might have ended. —>
    System.ArgumentException: The type of the value System.Management.Automation.PSLanguageMode is
    not valid. The PSPrimitiveDictionary class accepts only values of types that are fully
    serializable over Windows PowerShell remoting. See the Help topic about_Remoting for a list of
    fully-serializable types.

    Once I removed “$PSVersionTable.add(“LanguageMode”,$host.Runspace.LanguageMode)” from my profile, all worked as expected.

    Still haven’t found out the “why.”

    1. Jeffery Hicks says:
      September 14, 2017 at 11:49 am

      I can’t figure out how you were using this code with a remote session so I’m not sure what is going on.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d