Reputation: 22010
I want to print out the number of cores.
$N_cores = Get-WmiObject –class Win32_processor | ft NumberOfCores
Write-Host $N_cores | Select-Object -Property NumberOfCores
This give me following:
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Mi
crosoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Micros
oft.PowerShell.Commands.Internal.Format.FormatEndData
How can I display the NumberOfCores?
Upvotes: 1
Views: 723
Reputation: 90
There's a few issues here
I suggest you follow the best practice of Filter Left, Format Right
When you pass objects through the pipe to another function, that function attempts to interpret those objects' properties as arguments. If you examine Format-Table output, it no-longer has the NumberOfCores property. That property was a member of the Win32_processor object type.
You cannot pipe output from Write-Host, as it does not return anything to the pipeline
Ultimately the code that you would want is
$N_cores = Get-WmiObject –class Win32_processor | Select-Object -Property NumberOfCores
Format-Table $N_cores |Write-Host
It should also be mentioned that if you intend to do anything with this script besides print to the console, you should not use Format-Table or Write-Host, instead use `Write-Output $N_cores as your second line. This will write the result not only to the console, but also pass the objects in the $N_cores variable to the pipeline Write-Host vs Write-Output
Upvotes: 2
Reputation: 175085
The confusing ouput you're seeing comes from ft
(alias for Format-Table
).
To trim the number of properties you want from Get-WmiObject
, use Select-Object
instead.
For example, to store the NumberOfCores
property in $N_cores
, and subsequently display only its raw value you could do:
# This will create an object with a `NumberOfCores` property
$N_cores = Get-WmiObject –class Win32_processor |Select-Object NumberOfCores
# print just the value of the property
Write-Host $N_cores.NumberOfCores
Upvotes: 3
Reputation: 773
you need to extract just the value of the cores, try this
$N_cores = Get-WmiObject –class Win32_processor | Select-Object -ExpandProperty NumberOfCores
Write-Host "Number of cores is: $N_cores"
Upvotes: 3