Get Total Number of Cores from a computer WITHOUT HyperThreading
Asked Answered
M

5

19

This is a tough one.

I need to use a command to output the exact number of cores from my servers.

My tests:

  • X: On a Windows server with 4 processors (sockets) and 2 cores each without HT.
  • Y: On a Windows Server with 2 processors (sockets) and 6 cores each with HT.

GetSystemInfo only gets me the number of processors installed: 4 for X, 2 for Y.

|                | X: 8 cores  | Y: 12 cores |
|                | 4x2 (no HT) | 2x6 (HT)    |
|----------------|-------------|-------------|
| Desired output | 8           | 12          |
| GetSystemInfo  | 4           | 2           | 

%NUMBER_OF_PROCESSORS% is a good one, but it takes HT into account. It tells me 8 for X and 24 for Y (since it has HT, I needed it to show 12 instead).

|                        | X: 8 cores  | Y: 12 cores |
|                        | 4x2 (no HT) | 2x6 (HT)    |
|------------------------|-------------|-------------|
| Desired output         | 8           | 12          |
| GetSystemInfo          | 4           | 2           | 
| %NUMBER_OF_PROCESSORS% | 8           | 24          |

"wmic cpu get NumberOfCores" gets me info for each socket. For example:

X:

>wmic cpu get NumberOfCores
NumberOfCores
2
2
2
2

Y:

>wmic cpu get NumberOfCores
NumberOfCores
6
6

Meaning

|                            | X: 8 cores  | Y: 12 cores |
|                            | 4x2 (no HT) | 2x6 (HT)    |
|----------------------------|-------------|-------------|
| Desired output             | 8           | 12          |
| GetSystemInfo              | 4           | 2           | 
| %NUMBER_OF_PROCESSORS%     | 8           | 24          |
| wmic cpu get NumberOfCores | 2,2,2,2     | 6,6         |

Sigh.

I wished to keep it simple, inside the CMD, but I'm thinking about starting a Powershell script to do all that math and stuff.

Any thoughts?

Massimiliano answered 1/9, 2016 at 14:30 Comment(0)
M
21

suggested command does not work on computer with mote than 64 logical cores

(Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors

provide number of logical cores (HT cores)

Montagu answered 20/12, 2018 at 14:31 Comment(1)
What command? The question is pretty clear that it wants the total count of physical — not logical — cores. NumberOfProcessors wouldn't work, either, because that returns the number of sockets, not cores.Nannana
D
18

You can use Get-ComputerInfo and scope to the property you want.

$processor = Get-ComputerInfo -Property CsProcessors
$processor.CsProcessors

That should give you something like

Name                      : Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz
Manufacturer              : GenuineIntel
Description               : Intel64 Family 6 Model 78 Stepping 3
Architecture              : x64
AddressWidth              : 64
DataWidth                 : 64
MaxClockSpeed             : 2808
CurrentClockSpeed         : 2607
NumberOfCores             : 2 <== that one
NumberOfLogicalProcessors : 4 
…
…

Then, just look for NumberOfCores in the results.

Dorene answered 27/10, 2020 at 19:47 Comment(5)
What version of powershell were you using? I'm on 7.1 so I'm guessing I should try 5.1 and see what windows powershell gets. If that's the difference I'll add that constraint to the answer.Dorene
Oh my bad. I've only looked at the table from Get-ComputerInfo and that one only had logical cores, but the sub table CsProcessors has the properties like you said. Works on 5.1, 6 and 7.Burka
Oh cool thanks for the feedback! I was just back in the office today and was going to give it a shot. ^_^Dorene
(Get-ComputerInfo).CsNumberOfLogicalProcessorsParadigm
I can't see the NumberOfCores / CsNumberOfCores property anymore for PS 7.1, W10 21H1Gagliardi
N
7

If I understand your question for each server you want to retrieve a single integer that is the total number of physical processors (cores). Depending on if Hyper-Threading is available and enabled this may be half the number of logical processors, but you specifically want to exclude those.

Instances of the Win32_Processor WMI class represent a single processor socket. The NumberOfCores property indicates the number of physical processors provided by that socket. We can query this in PowerShell using the Get-CimInstance cmdlet:

Get-CimInstance -ClassName 'Win32_Processor' `
    | Select-Object -Property 'DeviceID', 'Name', 'NumberOfCores'

To add up the NumberOfCores property for each Win32_Processor instance we can use the Measure-Object cmdlet:

Get-CimInstance -ClassName 'Win32_Processor' `
    | Measure-Object -Property 'NumberOfCores' -Sum

In the result object Sum will contain the total number of physical cores and Count will contain the total number of sockets.

Note that on older versions of Windows/PowerShell you may need to substitute the Get-WmiObject cmdlet for Get-CimInstance. Also, if you did want to get the count of logical (Hyper-Threaded) processors, you can substitute the NumberOfLogicalProcessors property for NumberOfCores. Since Windows 10/Server 2016 there is a NumberOfEnabledCore property as well.

Nannana answered 20/1, 2017 at 5:20 Comment(0)
P
4

If I understand you correctly, this vbscript will give you both. Powershell method - https://blogs.technet.microsoft.com/heyscriptingguy/2011/09/26/use-powershell-and-wmi-to-get-processor-information/

On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL",wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
  WScript.Echo "NumberOfCores: " & objItem.NumberOfCores
  WScript.Echo "NumberOfLogicalProcessors: " & objItem.NumberOfLogicalProcessors
Next

Powershell may provide a better report layout if the server contains multiple physical processors.

Get-WmiObject –class Win32_processor | ft systemname,Name,DeviceID,NumberOfCores,NumberOfLogicalProcessors
Perfuse answered 1/9, 2016 at 18:12 Comment(0)
T
3

Late to the party, but this will do as a full DOS solution

set val=0 & for /f  "tokens=2 delims==" %d in ('wmic cpu get NumberOfCores /value ^| findstr "="') do @set /A val+=%d >NUL
  • set val=0 to initialize your variable
  • wmic cpu get NumberOfCores /value retrieve the value in a list format, more practical to parse. wmic has a lot of blank lines in its output. The List format gives us a common character to filter, =. Thus, the ^| findstr "=". The pipe is escaped to be caught by the for and not the shell itself.
  • set /A allows to do computations, here the sum of all found values. This command also outputs its result, thus the NUL redirection.

So, on X you get val=8, and on Y val=12

Thistly answered 12/2, 2019 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.