{"id":12581,"date":"2011-09-26T00:01:00","date_gmt":"2011-09-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/09\/26\/use-powershell-and-wmi-to-get-processor-information\/"},"modified":"2011-09-26T00:01:00","modified_gmt":"2011-09-26T00:01:00","slug":"use-powershell-and-wmi-to-get-processor-information","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-and-wmi-to-get-processor-information\/","title":{"rendered":"Use PowerShell and WMI to Get Processor Information"},"content":{"rendered":"<p><strong>Summary<\/strong>: Learn how to get the number of processor cores via WMI and Windows PowerShell.<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hey, Scripting Guy! I need to perform an audit of computers on our network. Specifically, I am tasked with obtaining CPU information. I need the processor speed, number of cores, and number of logical processors. I feel like I should be able to use Windows PowerShell to do this, but I am not certain. Can you help?<\/p>\n<p>&mdash;RS<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hello RS,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. This has been a rather crazy time. This week I am in Seattle, Washington, talking to customers about Windows PowerShell. Later in the week, I will be talking to Windows PowerShell writers on campus at our Microsoft Office in Redmond. I fly back to Charlotte, and then I head north to Canada for a couple of weeks. I really enjoy the opportunity to meet with people who are using Windows PowerShell to solve real world problems. It is cool.<\/p>\n<p>RS, to find out information about the CPU, I use the Windows Management Instrumentation (WMI) class <b>Win32_Processor<\/b>. In Windows PowerShell, a single line of code that uses the <b>Get-WmiObject<\/b> cmdlet to do the heavy lifting is all that is required. The syntax of a command to query WMI and return CPU information is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject Win32_Processor<\/p>\n<p>And I can shorten that command by using the <b>gwmi<\/b> alias:<\/p>\n<p style=\"padding-left: 30px\">gwmi win32_Processor<\/p>\n<p>In the following figure, I illustrate using the <b>Get-WmiObject<\/b> command and the default output from the command.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5148.hsg-9-26-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of using Get-WmiObject and default output\" alt=\"Image of using Get-WmiObject and default output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5148.hsg-9-26-11-01.png\" \/><\/a><\/p>\n<p>The <b>Win32_Processor<\/b> WMI class <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394373(VS.85).aspx\">is documented on MSDN<\/a>, and the article describes what all of the properties and coded values mean. But RS, for your requirements, I do not need that article. What I do need is a good way to select only the information you require. To do this, I am going to choose which properties I need. I then pipe the returned object to the <b>Select-Object<\/b> cmdlet. The reason for this is to remove the system properties that are automatically included with the returned WMI object. To avoid typing the properties twice (once for the <b>Get-WmiObject<\/b> cmdlet and once for the <b>Select-Object<\/b> cmdlet), I store the array of properties in the <b>$property<\/b> variable. The revised command is shown here:<\/p>\n<p style=\"padding-left: 30px\">$property = &#8220;systemname&#8221;,&#8221;maxclockspeed&#8221;,&#8221;addressWidth&#8221;,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;numberOfCores&#8221;, &#8220;NumberOfLogicalProcessors&#8221;<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject -class win32_processor -Property&nbsp; $property |<\/p>\n<p style=\"padding-left: 30px\">Select-Object -Property $property&nbsp;<\/p>\n<p>RS, you mentioned wanting to query computers on your network. The easy way to do this is to use the Active Directory cmdlets. I have <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/active+directory\/windows+powershell\/\">an entire series of articles that talk about how to get the Active Directory cmdlets<\/a>, and how to load and use them. You should refer to that series if you have questions about using Active Directory cmdlets.<\/p>\n<p>RS, I wrote a script called GetAdComputersAndWMIinfo.ps1. The complete text of this script appears here.<\/p>\n<p style=\"padding-left: 30px\"><b>GetAdComputersAndWMIinfo.ps1<\/b><\/p>\n<p style=\"padding-left: 30px\">Import-Module ActiveDirectory<\/p>\n<p style=\"padding-left: 30px\">$pingConfig = @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8220;count&#8221; = 1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8220;bufferSize&#8221; = 15<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8220;delay&#8221; = 1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8220;EA&#8221; = 0 }<\/p>\n<p style=\"padding-left: 30px\">$computer = $cn = $null<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Get-ADComputer -filter * -Credential $cred |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;ForEach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(Test-Connection -ComputerName $_.dnshostname @pingconfig)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { $computer += $_.dnshostname + &#8220;`r`n&#8221;} }<\/p>\n<p style=\"padding-left: 30px\">$computer = $computer -split &#8220;`r`n&#8221;<\/p>\n<p style=\"padding-left: 30px\">$property = &#8220;systemname&#8221;,&#8221;maxclockspeed&#8221;,&#8221;addressWidth&#8221;,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;numberOfCores&#8221;, &#8220;NumberOfLogicalProcessors&#8221;<\/p>\n<p style=\"padding-left: 30px\">foreach($cn in $computer)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;if($cn -match $env:COMPUTERNAME)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Get-WmiObject -class win32_processor -Property&nbsp; $property |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Select-Object -Property $property }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;elseif($cn.Length -gt 0)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Get-WmiObject -class win32_processor -Property $property -cn $cn -cred $cred |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Select-Object -Property $property } }&nbsp;<\/p>\n<p>The first thing to do is to import the <b>ActiveDirectory<\/b> module. In a script, I recommend using the complete name for the <b>ActiveDirectory<\/b> module, instead of using a wildcard character pattern such as <b>*AD*<\/b>. This is because there are many modules available for download from the Internet that would match the <b>*AD*<\/b> pattern. If this is the case, you cannot be certain you have actually loaded the <b>ActiveDirectory<\/b> module. To load the <b>ActiveDirectory<\/b> module, use the <b>Import-Module<\/b> cmdlet as shown here:<\/p>\n<p style=\"padding-left: 30px\">Import-Module ActiveDirectory<\/p>\n<p>Next, I intend to use splatting to simplify using the <b>Test-Connection<\/b> cmdlet. I wrote an article about splatting <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/09\/25\/configure-powershell-cmdlet-default-values-by-using-splatting.aspx\">last week<\/a>. Splatting uses a hash table for the parameters and associated values. This hash table is shown here:<\/p>\n<p style=\"padding-left: 30px\">$pingConfig = @{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8220;count&#8221; = 1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8220;bufferSize&#8221; = 15<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8220;delay&#8221; = 1<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; &#8220;EA&#8221; = 0 }<\/p>\n<p>I then initialize a couple of variables. This helps when running the command multiple times inside the Windows PowerShell ISE. I also retrieve credentials via the <b>Get-Credential<\/b> cmdlet. These two commands are shown here:<\/p>\n<p style=\"padding-left: 30px\">$computer = $cn = $null<\/p>\n<p style=\"padding-left: 30px\">$cred = Get-Credential<\/p>\n<p>Now, I use the <b>Get-ADComputer<\/b> cmdlet to retrieve a listing of computers from Active Directory Directory Services. I use the <b>Foreach-Object<\/b> cmdlet and pass the host names to the <b>Test-Connection<\/b> cmdlet to ensure the computer is online. I then create an array of <b>computernames<\/b> and store the names in the <b>$computer<\/b> variable. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-ADComputer -filter * -Credential $cred |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;ForEach-Object {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(Test-Connection -ComputerName $_.dnshostname @pingconfig)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { $computer += $_.dnshostname + &#8220;`r`n&#8221;} }<b><\/b><\/p>\n<p>The array that gets created is an array of single letters. I split the string based on the carriage return and line feed characters <b>&ldquo;`r`n&rdquo;<\/b> and create a new array that contains the name of each computer in an <b>array<\/b> element. This process leaves an element at the end of the array; this empty element will be dealt with later in the script. Here is the code that creates the new array of ComputerNames:<\/p>\n<p style=\"padding-left: 30px\">$computer = $computer -split &#8220;`r`n&#8221;<\/p>\n<p>I now define an array of property names that are to be collected from WMI. This is a straightforward value assignment:<\/p>\n<p style=\"padding-left: 30px\">$property = &#8220;systemname&#8221;,&#8221;maxclockspeed&#8221;,&#8221;addressWidth&#8221;,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;numberOfCores&#8221;, &#8220;NumberOfLogicalProcessors&#8221;<\/p>\n<p>The online computers are stored in the <b>$computer<\/b> variable. I use the <b>foreach<\/b> statement to walk through the array of computer names. If the computer name matches the local computer name, I do not use credentials because WMI would cause the command to fail. In addition, I check to see if the computer name is greater than 0 in length. This takes care of the empty element at the end of the array. This portion of the code is shown here:<\/p>\n<p style=\"padding-left: 30px\">foreach($cn in $computer)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;if($cn -match $env:COMPUTERNAME)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Get-WmiObject -class win32_processor -Property&nbsp; $property |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Select-Object -Property $property }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;elseif($cn.Length -gt 0)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Get-WmiObject -class win32_processor -Property $property -cn $cn -cred $cred |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Select-Object -Property $property } }<\/p>\n<p>When the script runs, output similar to that shown in the following figure is displayed.<\/p>\n<p>&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3681.hsg-9-26-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of output when script runs\" alt=\"Image of output when script runs\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3681.hsg-9-26-11-02.png\" \/><\/a><\/p>\n<p>RS, that is all there is to using the Active Directory module to retrieve computer names, and to use WMI to query for the processor information.<\/p>\n<p>&nbsp;<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to get the number of processor cores via WMI and Windows PowerShell. Hey, Scripting Guy! I need to perform an audit of computers on our network. Specifically, I am tasked with obtaining CPU information. I need the processor speed, number of cores, and number of logical processors. I feel like I should [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[237,16,22,2,3,4,170,45],"class_list":["post-12581","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-basic-computer-information","tag-desktop-management","tag-retrieving-input","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-splatting","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to get the number of processor cores via WMI and Windows PowerShell. Hey, Scripting Guy! I need to perform an audit of computers on our network. Specifically, I am tasked with obtaining CPU information. I need the processor speed, number of cores, and number of logical processors. I feel like I should [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12581"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12581\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}