Check PowerShell Version


posted by Tobias
01-25-2010

Downloads: 1,322
File size: 2.4kB
Views: 6,787

Embed
Check PowerShell Version
  1. function Test-PSVersion
  2. [CmdletBinding()] 
  3. param
  4. [parameter(Position=0,ValueFromPipeline=$true)] 
  5. [ValidateNotNullOrEmpty()] 
  6. [String[]] 
  7. $ComputerName = @('.'), 
  8.  
  9. [Parameter()] 
  10. [ValidateNotNull()] 
  11. [System.Management.Automation.Credential()] 
  12. $Credential = [System.Management.Automation.PSCredential]::Empty 
  13. process { 
  14.  
  15.   if (Test-Connection -ComputerName $computername -Count 1 -ErrorAction SilentlyContinue) { 
  16.    try { 
  17.     $OS = Get-WmiObject -Namespace root\CIMV2 -Class Win32_OperatingSystem ` 
  18.     -ComputerName $computername -Credential $credential -ErrorAction SilentlyContinue 
  19.     if ($OS) { 
  20.      $path = "$($OS.SystemDirectory -replace '\\','\\')\\WindowsPowerShell\\v1.0\\powershell.exe" 
  21.      $OSName = $OS.Name.Split('|')[0] 
  22.      $query = "SELECT Version FROM CIM_DataFile WHERE Name = '$path'" 
  23.      $PSEXE = Get-WmiObject -Query $query -ComputerName $computername -Credential $credential 
  24.      if ($PSEXE.Version) { 
  25.       $buildversion = $PSEXE.Version.Split()[0] 
  26.       $versionPresent = [version]$buildversion 
  27.       $versionRequired = [version]'6.0.6002.18111' 
  28.       if ($versionPresent -ge $versionRequired) { 
  29.        $psversion = "V2 RTM" 
  30.       } elseif ($versionPresent.Major -ge 6) { 
  31.        $psversion = "V2 CTP Prerelease - Update to V2 RTM!" 
  32.       } else
  33.        $psversion = "V1" 
  34.       
  35.  
  36.       New-Object PSObject -Property @{ 
  37.        ComputerName=$OS.__SERVER; 
  38.        BuildVersion=[version]$buildversion 
  39.        Version=$psversion
  40.        Status=$true;Description='OK'
  41.        OSName = $OSName 
  42.        
  43.      
  44.     } else
  45.      New-Object PSObject -Property @{ 
  46.       ComputerName=$computername[0]; 
  47.       BuildVersion=[version]$null
  48.       Version='n/a'
  49.       Status=$false
  50.       Description='Unable to access OS information via WMI.'
  51.       OSName = 'n/a' 
  52.      
  53.     
  54.    
  55.   }  
  56.   catch { 
  57.    New-Object PSObject -Property @{ 
  58.     ComputerName=$computername[0]; 
  59.     BuildVersion=[version]$null
  60.     Version='n/a'
  61.     Status=$false
  62.     Description=($_.Exception.Message); 
  63.     OSName='n/a' 
  64.    
  65.    continue 
  66.   }  
  67.  
  68. } else
  69.   New-Object PSObject -Property @{ 
  70.    ComputerName=$computername[0]; 
  71.    BuildVersion=[version]$null
  72.    Version='n/a'
  73.    Status=$false
  74.    Description='Computer did not respond to ping, skipped.'
  75.    OSName='n/a' 
  76.   
  77.  
  78. "localhost", "pc77", "localhost", "storage1" |  
  79. Test-PSVersion 
Filed under: , ,

Checks local or remote PowerShell version and differentiates V1, V2 CTPs and V2 to help you find outdated PowerShell installations.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.