June 29th, 2018

PowerTip: Determine your version of PowerShell and host operating system

Doctor Scripto
Scripter

Summary:  Identify your PowerShell environment by making use of built-in PowerShell variables.

   Hey, Scripting Guy! If I’m writing a script, how can I detect if I am running on Windows, Linux, or Mac with PowerShell?

   Just use the built-in variable $PSVersionTable, which has all of the properties you’ll need. Here is an example:

# Currently operating version

$PSVersionTable.PSVersion

# Current Host operating System

# If this value is $NULL you are currently running Windows PowerShell 5.x

# else it returns the host environment from the open Source release

$PSVersionTable.OS

Drawing of Dr. Scripto

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Lyubomir Dimitrov

    With Powershell 5.1.X $PSVersionTable.OS returns nothing. No such member of the $PSVersionTable