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
if (@($processors)[0].NumberOfCores)
$cores = @($processors).count * @($processors)[0].NumberOfCores
$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


About Prashanth Jayaram

DB Technologist, Author, Blogger, Service Delivery Manager at CTS, Automation Expert, Technet WIKI Ninja, MVB and Powershell Geek My Profile: https://social.technet.microsoft.com/profile/prashanth jayaram/ http://www.sqlshack.com/author/prashanth/ http://codingsight.com/author/prashanthjayaram/ https://www.red-gate.com/simple-talk/author/prashanthjayaram/ http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/ Connect Me: Twitter @prashantjayaram GMAIL powershellsql@gmail.com The articles are published in: http://www.ssas-info.com/analysis-services-articles/ http://db-pub.com/ http://www.sswug.org/sswugresearch/community/
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.


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 )

Connecting to %s