14

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?

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
David Lago
  • 307
  • 1
  • 3
  • 11

6 Answers6

16

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)

Syc
  • 185
  • 1
  • 3
  • 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. – Lance U. Matthews Aug 17 '21 at 22:40
11

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.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
JKmeister
  • 111
  • 1
  • 2
  • 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. – JKmeister Oct 31 '20 at 17:04
  • 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. – Timo Oct 31 '20 at 18:35
  • Oh cool thanks for the feedback! I was just back in the office today and was going to give it a shot. ^_^ – JKmeister Nov 02 '20 at 17:37
  • 1
    `(Get-ComputerInfo).CsNumberOfLogicalProcessors` – evandrix Jul 19 '21 at 12:49
  • I can't see the NumberOfCores / CsNumberOfCores property anymore for PS 7.1, W10 21H1 – Umair Ahmed Aug 17 '21 at 09:14
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.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
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
Randy Schuman
  • 357
  • 1
  • 9
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

Mat M
  • 1,786
  • 24
  • 30
0

Using the .NET environment information to get the logical processors count:

$logicalProcessors = [System.Environment]::ProcessorCount
J.Hudler
  • 1,238
  • 9
  • 26
  • The question is asking for the number of physical cores, *not* logical. ("WITHOUT Hyperthreading" = how many cores you'd have if you disabled HT in the BIOS.) – Peter Cordes Jul 07 '23 at 01:57