mBrice1024
mBrice1024

Reputation: 838

Powershell - get the output from this Get-WmiObject command (getting the number of cores using powershell)

So my aim is to get the number of cores on the machine my powershell script runs on and work with it as an integer. Some googleing lead me to this nice and simple command to get the number of cores:

Get-WmiObject -Class Win32_ComputerSystem | fl NumberOfLogicalProcessors

Which displays an output like this:

NumberOfLogicalProcessors : 4

Now my issue is, how so I extract the number "4" from this? I tried .Split(":") but the output is not a string so that doesn't work. Next I tried

PS C:\Windows\system32> Get-WmiObject -Class Win32_ComputerSystem | fl NumberOfLogicalProcessors | select NumberOfLogicalProcessors

But this just yields:

"NumberOfLogicalProcessors
--------------------------------------"

Not helpful. What am I missing? What is this Get-WmiObject returning and how do I work with it?

Edit: Thank you mhu, that did the trick!

Upvotes: 0

Views: 2464

Answers (1)

mhu
mhu

Reputation: 18061

Don't use Format-List (fl), but directly select the required property, like this:

Get-WmiObject -Class Win32_ComputerSystem | select "NumberOfLogicalProcessors" -ExpandProperty "NumberOfLogicalProcessors"

As said by Mike, you could shorten this to:

Get-WmiObject -Class Win32_ComputerSystem | select -ExpandProperty "NumberOfLogicalProcessors"

Upvotes: 1

Related Questions