DevOps | Scripts | Automation

PowershellScripts

How to Check the PowerShell Version?

To check the PowerShell version on the local system, we can use $PSVersionTable command.

In the above command, the PowerShell version is 5.1, and the build version is 18362 and the revision is 752. You can check the specific PSVersion property.

$PSVersionTable.PSVersion

To check the PowerShell version on the remote computer you can enter the remote session using Enter-PSSession but we don’t want the interactive output. We need output for multiple computers. For that use Invoke-Command.

Invoke-Command -ComputerName Test1-Win2k12,Test1-Win2k16 -ScriptBlock{$PSVersionTable.PSVersion}

Output:

If we have large number of servers, we can use the function below.

Function Get-PSVersion{
    param(
       [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
       [String[]]$ComputerName
    )
  
    Begin{
       Write-Verbose "Starting script to get the PowerShell version from the remote computers"
       $output = @()   
       $comment = ""
    }
 
    process{
       foreach($computer in $ComputerName){
        try {
             if(Test-Connection $computer -Count 1 -Quiet -ErrorAction Ignore){
                $psout = Invoke-Command -ComputerName $Computer -ScriptBlock   {$PSVersionTable.PSVersion} -ErrorAction Stop
                $psver = "$($psout.Major)" + ".$($psout.minor)"
                $comment = 'Connection successful'         
             }  

             else{
                $comment = 'Connection failed'
             }
        }
        catch {
            $comment = $_.Exception.Message
        }   

                $output += [PSCustomObject]@{
                Server = $computer
                PSVersion = $psver
                Comment = $comment 
            }

            $psver = ""
            $comment = ""
       } 
    }
 
    End{
       Write-Verbose 'Generating Output' 
       $output | ft -AutoSize
    }
   
 }

Output:

You can download github version from the below url.

https://github.com/chiragce17/GetPSVersion