Menu
Use PowerShell and WMI to Get CPU Information

Suppose you want to get some information about the CPU in your computer. Can you remember which Control Panel utility will provide that information? Is it My Computer or System? Unfortunately, like a department store that keeps moving merchandise around to confuse you into thinking they have something new, with each new version Microsoft keeps rearranging Windows operating system utilities.

PowerShell is a more powerful replacement for the command shell. WMI (Windows Management Interface) is part of the Windows operating system that can be used to gather system statistics, monitor system health, and manage system components. If you use a PowerShell script to access WMI you can always get the information you want no mater how they rearrange Control Panel.

The best way to use PowerShell is to create and run scripts. Once you've written a script, you can save it to use again, on your machine or a friends machine. Because of PowerShell's awesome power, its execution policy may not allow you to run scripts. To change PowerShell's execution policy, you'll need to run PowerShell as an administrator.

To run PowerShell, in the task bar Search box, type "PowerShell". In the menu that appears, right-click on Windows PowerShell, and in the menu that appears, select "Run as administrator". Enter the following command after the PS prompt:

PS C:\> Set-ExecutionPolicy Unrestricted

In the User Account Control warning box that appears, click on the [Yes] button. In the PowerShell window the message "... Do you want to change the execution policy?" will appear. Enter Y and press [Enter].

The best way to run PowerShell scripts is to create a shortcut on your desktop, in the shortcut's location of item for shortcut textbox, enter:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit

Note: If you have Windows 8.1 or Windows Server 2012 R2, on your machine along with PowerShell, you have PowerShell version 2, so you may want a different shortcut path. However, you can still use PowerShell version 1. Note the -NoExit switch at the end of the path, this keeps the PowerShell window from closing before you can see the returned results.

The Get-WMIObject Cmdlet gets instances of WMI classes. WMI classes are the software objects that query the system's various devices and system resources. To use PowerShell with WMI, write a script that uses the Get-WMIObject Cmdlet.

To create a PowerShell script open a basic ASCII text editor like Windows Notepad and type in the following comand.

(Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name

Save the file extension .ps1 for example getCompname.ps1. To run the script, drag it to the shortcut that you created above.

The above command uses the WMI interface's Win32_ComputerSystem class to access the local computer's name. To close the PowerShell window, type exit at the prompt.

Now to get information about the CPU in your computer, create another script named say, cpuinfo.ps1, and enter the following command, replacing COMPUTER-NAME with your computer's name returned by the script above.

Get-WmiObject Win32_processor -ComputerName COMPUTER-NAME
Information returned by the Win32_processor class

Shown above is an example of the information returned by the Win32_processor class.

To get the number of cores and the number of logical processors in your computer, use the script shown below.

Get-WmiObject -ComputerName COMPUTER-NAME win32_processor | select NumberOfCores, NumberOfLogicalProcessors

Information returned by the Win32_processor class

NumberOfCores is the actual number of physical processors in your computer. NumberOfLogicalProcessors is the number of physical cores times the number of threads that can run on each core through the use of hyperthreading. Each logical core is capable of executing its own sequence of instructions or thread in parallel with other logical cores.


Learn more at amazon.com

More Windows Administration Information:
• Synchronize Your PC's Clock with an Internet Time Server
• Three Important Techniques for Securing a Wireless Network
• How to Setup DHCP (Dynamic Host Configuration Protocol) on a Windows Server
• The Fastest Way to Move PC to PC
• How to Map a Network Folder in Windows 7
• Script to Print a Directory File List
• PowerShell Script to Show Last 5 Errors in Event Log
• How to Install Hyper-V on Windows Server 2019
• How to Backup Mails and Address Book of Outlook Express
• Configure Vista's Data Execution Prevention