Get CPU and Memory info

One day I was wondering if it is possible to get any physical memory information without downloading 3rd party tools in Windows. After some research, I learned about WMI and CIM. This was an eye-opener, I opened my trusty Powershell ISE and started writing a script to get CPU and memory info in Windows.

As I mentioned above, I needed to familiarize myself with the Get-WmiObject cmdlet. Get-WmiObject is a very powerful cmdlet for accessing classes in Windows Management Instrumentation (WMI). Once I got some basic knowledge of how to use Get-WmiObject. And what class has some actionable data only thing left to do is get some results.

Make it human readable

Results were there, except they were not so human-friendly. This presented itself as very annoying. To fix this I needed help from the mathematical library in .NET. AHA moment happened when I remembered that PowerShell has access to .NET libraries. One call to the Mathematical [Math] library was all I needed to translate those pesky bytes into GigaBytes.

What is CIM and what is used for?

Another powerful cmdlet that is used here is Get-CimInstance. This is a fairly new feature in PowerShell. If you are not familiar with CIM cmdlets I suggest reading this blog, personally, I find it very helpful.

After reading about CIM, I decided that an excellent place to start is the Get-CimInstance cmdlet.
Get-CimInstance allows us to access detailed information about memory modules using Win32_PhysicalMemory CIM instance in this case. This has produced output that was not as friendly as one would like. In order to remedy this output is piped to format a table or in short ft cmdlet. I find the format table to be a very powerful and easy-to-use tool.

And for the finale to get CPU info in Windows we will use win32_processor class. We are only interested in Type and Cores. If you want more information feel free to play with win32_processor class.

example:
  
Write-Output "========================================"
Write-Output "|                                      |"
Write-Output "|            Machine Info              |"
Write-Output "|                                      |"
Write-Output "========================================"

$h = Get-WmiObject -Class Win32_ComputerSystem
        $TotalRAM = ([Math]::Round(($h.TotalPhysicalMemory/1GB),0) )
        $mth = @{expression = {$_.DeviceLocator};Label="Slot"},`
               @{expression = {$_.Speed};Label="Speed MHZ"},`
               @{expression = {$_.Manufacturer};Label="Manufacturer"},`
               @{expression = {($_.Capacity/1GB)};Label="Size GB"}
        
        Write-Output "Domain: $( $h.domain.toUpper())"
        Write-Output "HostName: $( $h.name.toUpper())"
        Write-Output "Total RAM: $TotalRAM GB";
           
        Get-CimInstance Win32_PhysicalMemory | ft $mth
    
        $cth = @{expression = {$_.DeviceID};Label="CPUID"},@{expression = {$_.Name};Label="Type"},@{expression = {$_.NumberofCores};Label="Cores"}
        WmiObject -class win32_processor | ft $cth
 note:

While developing, use the most powerful command in PowerShell Get-Help <cmdlet>.