Powershell Version Check - Remotely

Welcome Forums General PowerShell Q&A Powershell Version Check - Remotely

This topic contains 2 replies, has 3 voices, and was last updated by

 
Moderator
4 years ago.

  • Author
    Posts
  • #20310

    Participant
    Points: 21
    Rank: Member

    I've been tossing around some ideas on how to check the version of PowerShell remotely. I need to code for the following situations:
    – PowerShell Not Installed (Pre-my employment)
    – PowerShell v1-v2 – HKLM\Software\Microsoft\PowerShell\1\PowerShellEngine\PowerShellVersion (ex: v2.0)
    – PowerShell v3-v4 (5?) – HKLM\Software\Microsoft\PowerShell\3\PowerShellEngine\PowerShellVersion (ex: v4.0

    In terms of my Psuedo code, I'm going to open my registry connection, attempt to connect to the Key with 3 in it. If it does exist, get the PowerShellVersion value.

    If my Try/Catch fails, then try to connect to the Key with 1 in it. If that fails, then assume PowerShell is not installed.

    Is there anything I'm missing, or is there a better way to do this? I don't want to rely on WinRM at this point, as we're trying to find out where PS is not installed, so we can remediate.

  • #20323

    Participant
    Points: 0
    Rank: Member

    Graeme,
    A few things you might want to consider.
    1 – Is PowerShell remoting turned on in your environment?
    2 – PowerShell Version 1 does not have remoting capabilities.
    Below is some code that relies of PowerShell remoting. I did not perform error handling if the client is ofline or cannot respond because it is PowerShell V1. I'll leave that fun for you.

    Jason

    Function Get-PowerShellVersion
    {
    Param (
    [parameter(Mandatory=$true,
    ValueFromPipeline=$true)]
    [String[]]
    $ComputerName
    )
    PROCESS
    {
    # Loop through each Computer.
    ForEach ($C in $ComputerName)
    {
    # Use PowerShell Remoting to recover the PS Version from the registry.
    $Output = Invoke-Command -ComputerName $C -ScriptBlock {
    $Splat = @{"Path" = "HKLM:\software\microsoft\powershell\3\PowerShellEngine"}
    $A = Get-ItemProperty @Splat |
    Select-Object -ExpandProperty PSCompatibleVersion
    $Version = ($A.split(","))[-1]

    $Output = New-Object -TypeName PSObject -Property @{"PSVersion" = $Version}
    Write-Output $Output }

    # Send the object to the pipeline.
    Write-Output $Output
    } # END: ForEach ($C in $ComputerName)
    } # END: Process P{

    } # END: Get-PowerShellVersion

  • #20339

    Moderator
    Points: 24
    Team Member
    Rank: Member

    Graeme,

    An alternative to your Registry approach would be to check the file version of the PowerShell.exe via WMI. If you run below against your estate you should be able to get the file versions to build a version table for v1 to v5.

    Param
    (
        [Parameter(Mandatory = $true)]
        [String[]] $ComputerName,
    
        [String] $FilePath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    )
    
    $Filter = "Name='{0}'" -f $File.Replace('\','\\')
    Get-WmiObject -Class CIM_DataFile -ComputerName $ComputerName -Filter $Filter
    

The topic ‘Powershell Version Check - Remotely’ is closed to new replies.