Get Operating System Version

Uses the Win32_OperatingSystem WMI class to determine the version of Windows in use.

 
 
 
 
 
4.4 Star
(9)
Add to favorites
Operating System
2/20/2010
E-mail Twitter del.icio.us Digg Facebook
WMI
  • Possible typo
    3 Posts | Last post February 05, 2015
    • Correct me if I'm wrong, but shouldn't the Server 2003 line in this script be "5.2.3790", not "5.1.3790"? (at least, for SP2, which is what I tested with).
      
      Otherwise, fantastic script!  Solved a problem I was having with a script I'm working on.
      
    • Also be aware that Server 2008R2 SP1 currently shows as "Version not listed" - This can be resolved by adding the following parameter into the script:
      
      "6.1.7601" { $osv.value = "2008R2 SP1" }
    • ## Added 2012 R2 and Win8.1
      
      Function Get-OSVersion($computer,[ref]$osv) 
      { 
       $os = Get-WmiObject -class Win32_OperatingSystem  -computerName $computer 
       Switch ($os.Version) 
        { 
          "5.1.2600" { $osv.value = "xp" } 
          "5.1.3790" { $osv.value = "2003" } 
          "6.0.6001"  
                     {If($os.ProductType -eq 1) 
                         {$osv.value = "Vista" } #end if 
                       Else 
                         {$osv.value = "2008" } #end else 
                     } #end 6001 
          "6.1.7600" 
                      {If($os.ProductType -eq 1) 
                         {$osv.value = "Win7" } #end if 
                       Else 
                         {$osv.value = "2008R2"} #end else 
                     } #end 7600 
          "6.2.9200"
                     {If($os.ProductType -eq 1)
                         {$osv.value = "Win8"}
                       Else
                         {$osv.value = "Server2012"}
                      }
          "6.3.9600"
                     {If($os.ProductType -eq 1)
                         {$osv.value = "Win8.1"}
                       Else
                         {$osv.value = "Server2012R2"}
                      }
           DEFAULT { "Version not listed" } 
        } #end switch 
      } #end Get-OSVersion 
  • Windows 8 and Server 2012 added to switch
    1 Posts | Last post August 16, 2013
    • Switch ($os.Version)
      		{
      			"5.1.2600" { $OSverion = "Windows XP"; break; }
      			"5.1.3790" { $OSverion = "Windows Server 2003"; break; }
      			"6.0.6001" {
      				Switch ($os.ProductType) {
      					"1"	{ $OSverion = "Windows Vista"; break; }
      					"3" { $OSverion = "Windows Server 2008"; break; }
      					DEFAULT { Throw "ProductType not listed" }
      				} #end .ProductType
      			} #end 6001
      			"6.1.7600" {
      				Switch ($os.ProductType) {
      					"1"	{ $OSverion = "Windows 7"; break; }
      					"3" { $OSverion = "Windows Server 2008 R2"; break; }
      					DEFAULT { Throw "ProductType not listed" }
      				} #end .ProductType
      			} #end 7600
      			"6.1.7601" { $OSverion = "Windows Server 2008 R2 SP1"; break; }
      			"6.2.9200" {
      				Switch ($os.ProductType) {
      					"1"	{ $OSverion = "Windows 8"; break; }
      					"3" { $OSverion = "Windows Server 2012"; break; }
      					DEFAULT { Throw "ProductType not listed" }
      				} #end .ProductType
      			} #end 9200
      			DEFAULT { Throw "Version not listed" }
      		} #switch
      
  • error as well
    1 Posts | Last post July 19, 2012
    • Missing expression after unary operator '-'.
      At line:4 char:9
      +        - <<<< computerName $computer
          + CategoryInfo          : ParserError: (-:String) [], ParentContainsErrorRecordException
          + FullyQualifiedErrorId : MissingExpressionAfterOperator
      
      PS U:\> # *** entry point to script ***
      PS U:\> $osv = $null
      PS U:\> Get-OSVersion -computer $computer -osv ([ref]$osv)
      The term 'Get-OSVersion' is not recognized as the name of a cmdlet, function, script file, or operable program. Check t
      he spelling of the name, or if a path was included, verify that the path is correct and try again.
      At line:1 char:14
      + Get-OSVersion <<<<  -computer $computer -osv ([ref]$osv)
          + CategoryInfo          : ObjectNotFound: (Get-OSVersion:String) [], CommandNotFoundException
          + FullyQualifiedErrorId : CommandNotFoundException
  • I get an error running this script
    1 Posts | Last post July 09, 2012
    • Missing expression after unary operator '-'.
      At C:\Users\#\AppData\Local\Temp\eb5c6e8c-fcb5-4b29-8df0-56876d253ba5.ps1:6 char:9
      +        - <<<< computerName $computer 
          + CategoryInfo          : ParserError: (-:String) [], ParseException
          + FullyQualifiedErrorId : MissingExpressionAfterOperator
      
      It looks like it doesn't accept computerName as an operator, but I don't know what to put there.  Ideas?