Author Posts

November 4, 2014 at 9:08 am

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.

November 4, 2014 at 5:58 pm

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

November 5, 2014 at 5:56 am

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