Get-WindowsPowerShellVersion.ps1

<#PSScriptInfo
 
.VERSION 1.1
 
.GUID b587c363-bef2-402b-8a52-8202a61ee7c6
 
.AUTHOR saw-friendship
 
.COMPANYNAME
 
.COPYRIGHT saw-friendship
 
.TAGS saw-friendship Windows Remote PowerShell Version
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Get Windows PowerShell Version by WMI on local and remote system
 
.LINK
 https://sawfriendship.wordpress.com/
 
.EXAMPLE
 Get-WindowsPowerShellVersion
 
.EXAMPLE
 Get-WindowsPowerShellVersion FileServer01 -Credential (Get-Credential)
 
.EXAMPLE
 Get-WindowsPowerShellVersion FileServer01,FileServer02
  
.EXAMPLE
Get-ADComputer -Filter {Enabled -eq $true -and OperatingSystem -like '*server*'} | select * | Get-WindowsPowerShellVersion
 
#>
 

[CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][Alias("Host","DNSHostName","IP","IPAddress")][string[]]$ComputerName = $env:COMPUTERNAME,
        [PSCredential]$Credential
    )
    
    BEGIN {}
    
    PROCESS  {
        Foreach ($Comp in $ComputerName) {
            $param = @{
                'Path' = "\\$Comp\root\CIMv2:StdRegProv"
                'Name' = 'GetStringValue'
                'ArgumentList' = @(2147483650,'SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine','PowerShellVersion')
                'ErrorVariable' = 'Error'
            }
            if($Credential -and ($Comp -notin @($env:COMPUTERNAME,'.'))){$param.Credential = $Credential}
                
            $PSVersionKey = Invoke-WmiMethod @param
            
            if (!$Error) {
                if($PSVersionKey.ReturnValue -eq 0){
                    $Comp | Select-Object -Property @(
                        ,@{Name = 'ComputerName'; Expression = {$_}}
                        ,@{Name = 'PowerShellVersion'; Expression = {[Version]($PSVersionKey.sValue)}}
                    )
                    
                } else {
                    $Comp | Select-Object -Property @(
                        ,@{Name = 'ComputerName'; Expression = {$_}}
                        ,@{Name = 'PowerShellVersion'; Expression = {[Version]'2.0'}}
                    )
                }
            } else {
                $Comp | Select-Object -Property @(
                    ,@{Name = 'ComputerName'; Expression = {$_}}
                    ,@{Name = 'PowerShellVersion'; Expression = {$null}}
                )
            }
            

        }
    }
    
    END {}