Back in July 2011, I posted about getting the number of processors and cores and gave passing mention of the fact that there’s a known issue with PSH and some versions of Windows.  Well, PSH V3 now has the NumberOfLogicalProcessors property in the data returned from Get-WmiObject Win32_Processor, and the number of actual objects returned corresponds to the number of physical processors.

Given that I have a mixed environment, I wanted to have a way to return the right data regardless of the PSH version.

 

function Get-ProcessorCount { 
     param ( [parameter(ValueFromPipeline=$true,Position=0)][string[]]$ComputerName = @($env:COMPUTERNAME) );
    
     process {
         $ComputerName | % {
             $computer = $_;
             Write-Progress (Get-Date) $computer;
             $wmi = [Object[]](Get-WmiObject -ComputerName $computer Win32_Processor);
             $true | Select-Object -Property @{
                 n = 'ComputerName';
                 e = { $computer; }
             }, @{
                 n = 'NumProcs';
                 e =
                     $wmi | Group-Object SocketDesignation | Measure-Object | % { $_.Count }
                 }
             }, @{
                 n = 'NumCores';
                 e =
                     $wmi | % {
                         if ($_.NumberOfLogicalProcessors) {
                             $_.NumberOfLogicalProcessors;
                         } else {
                             1;
                         }
                     } | Measure-Object -Sum | % { $_.sum }                    
                 }
             }
         }
     }
}