PowerShell – Get CPU Details and its Usage

This PowerShell script helpful in finding No of Processors, Speed and It’s current usage based on samples being gathered at frequent intervals.

/* Get No of Processors,Speed and its usage*/

Function Get-CPUs {
param ($server)
$processors = get-wmiobject -computername $server win32_processor
$cpuSpeed = ((get-wmiobject -computername $server Win32_Processor).MaxClockSpeed)/1000
$cores=0
if (@($processors)[0].NumberOfCores)
{
$cores = @($processors).count * @($processors)[0].NumberOfCores
}
else
{
$cores = @($processors).count
}
$sockets = @(@($processors) | % {$_.SocketDesignation} |select-object -unique).count;
$Usage=(get-counter -ComputerName $Server -Counter “\Processor(_Total)\% Processor Time” -SampleInterval 2 -MaxSamples 10 | select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average
return “Cores: $cores, Sockets: $sockets Speed: $cpuSpeed CPUUsage% : $Usage”;
}

-MaxSamples Int

The number of samples to get from each counter.The default is 1 sample. To get samples continuously (no max sample size), use -Continuous.
-SampleInterval int
The time between samples in seconds. The minimum value and the default value are 1 second.

Get the current “% Processor Time” combined values for all processors on the local computer. Collect data every 2 seconds until 10 sample values are collected. The output will take 20 seconds to give you a result. You can change the sample and MaxSample parameter.

Download the code here Code- CPU Details

Output:-Image

About these ads

About Prashanth Jayaram

I’m a Database technologist having 8+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication and PowerShell. hobbies are playing sports and drawing.
This entry was posted in PowerShell and tagged , , , , . Bookmark the permalink.

2 Responses to PowerShell – Get CPU Details and its Usage

  1. isalom says:

    Nice article. I would like to add that when the power management profile is set to high performance, the powershell query will not always return an accurate value for the “CurrentClockSpeed”. I wrote about this on my blog. You may want to test this, to be surer that you are getting the right values.

    http://ignaciosalom.com/?p=623

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s