We are continuing our series that answers common questions, or questions that have been asked about/of PowerShell. Today we discuss Gathering CPU Information from the local machine. What we intend to provide is one of the many, many ways you can accomplish these same tasks within PowerShell.
All of the solutions are ours and demonstrate the author’s skill and ability level at the time of writing. That is to say, we might not always write the best PowerShell code, however, if you know of a better way we welcome that input.
Let’s start. The question is:
I have a script that is written in PowerShell using WMI to get CPU information. Is there a way to do this without using WMI?
The Answer:
Yes. Yes there is. To start we are going to use the Get-CimInstance
command for the Win32_Processor
class. The command looks identical to the Get-WMI
command we should no longer be using.
$CPU = Get-CimInstance -class Win32_Processor
That’s almost it. From here we can get things like which CPU is it using $cpu.DeviceID
, or maybe you want to know the Caption. To find this data you would type $cpu.Caption
.
Maybe we need to know the current-voltage or the current clock speed. We can find both from the original command. Using $cpu.CurrentClockSpeed
gives us the clock speed of the processor as we ran the initial command and not what it shows currently. $cpu.CurrentVoltage
will give us just that, the current-voltage being used by the processor at the time of the initial command being run.
Lastly, as it does not fit into the original command we want to find the load percentage of the current processor. To do this I used to use a command that looks like this:
$CPU_Used=(Get-CimInstance win32_processor).LoadPercentage
However, I found recently that we just need to look to the original command again and use $cpu.LoadPercentage
to get the same results.
As always, here is the completed script: