Manage Learn to apply best practices and optimize your operations.

PowerShell Illuminates System Components

In reading over user requests for information at this morning, I saw a simple-seeming request for info there. It read “How to show SSD and RAM size using terminal.” Basically, it asked for a way to determine total RAM installed on a PC and the presence and size of SSD drives it might house. Knowing that PowerShell illuminates system components nicely, I knew there had to be a way to do this using that toolbox of cmdlets. So I turned to Google to look things up and figure it out. It took about 15 minutes all told, and shows that PowerShell is powerful juju.

1. PowerShell Illuminates System Components: Total RAM

Sure, you can use the old Windows Management Instrumentation (WMI) calls to do this — even in PowerShell — by typing

wmic computersystem get totalphysicalmemory

But I wanted something a little friendlier and easier to read, with memory displayed in GB, not actual bytes. So I turned to Google again and learned that the Get-CimInstance cmdlet could tell me what I needed with a little script manipulation. CIM stands for “Common Information Model” and is based on a computer industry standard for defining device characteristics to make them accessible to and manageable by sysadmins and management programs alike.

In this case, the basic command is Get-CimInstance -class "Cim_PhysicalMemory" | % {$_.Capacity}. But that lists the capacity of each memory module on the PC, and doesn’t add things up. It also produces the string 8589934592 when I’d like to see 8 GB instead. A little script magic whips the whole thing into proper form:

$TotalRAM=(Get-CimInstance -ClassName 'Cim_PhysicalMemory' |
Measure-Object -Property Capacity -Sum).Sum
$TotalRAM /= (1024*1024*1024)
Write-Host "$TotalRAM GB"

The first long line adds up the capacities for all memory modules on the PC. The second line divides that result by 230 to convert bytes into gigabytes. The third line outputs the calculated value followed by “GB” to tell you how much RAM it detected.

This produces the output 32 GB on my PC, which is what I wanted to see, and would do likewise for RAM on other PCs as well. One down, one more to go. Note: the first three lines in the preceding script are actually one line of script broken for display purposes here. If you want to run this script, go to the end of the first line and hit the delete key to pull up the remaining part of that line. Do that again to make the script work properly.

2. PowerShell Illuminates System Components: SSD Presence and Size

This is a little easier to solve because there’s a cmdlet specifically focused on physical disk devices. Not coincidentally, it’s named Get-PhysicalDisk. One single command line will suffice to produce the requested output, with a little selecting and filtering to provide minimal information. Here ’tis:

 get-physicaldisk | Select FriendlyName, MediaType, Size | where-object {$_.MediaType -eq 'SSD'}

Here again, seeing the string on more than one line means you need to delete spurious line breaks if you cut’n’paste it into PowerShell.

The first part of the string grabs physical disk attributes for all disks on the system (up to first pipeline symbol ‘|’). The second part of the string selects the FriendlyName, MediaType and Size attributes for those disks (up to second pipeline symbol). The third part of the string filters out any entries where the MediaType attribute is not “SSD.” The result is a listing of FriendlyName, MediaType and Size for all SSDs on the system where the string is executed. Here’s what that looks like on my production PC:

Three out of 8 drives on this system are SSDs.
[Click image for full-sized view.]

PowerShell Is Good Stuff!

The more you mess with PowerShell, the more you’ll come to appreciate its many capabilities. Just about any kind of Windows information or action you can think of, you can accomplish using PowerShell. The guy who posted to SuperUser could have spent his time digging into PowerShell himself and solved his issue quickly and easily. The more you learn PowerShell, in fact, the more you’ll end up using it.