How to Simplify PowerShell Storage Capacity Information

0

PowerShell’s Get-PhysicalDisk cmdlet does an excellent job of displaying a concise summary of your system’s hard drives. Simply type Get-PhysicalDisk to see a report of all disks installed on your system.

You can see what it looks like in Figure 1.

Brian Posey

Figure 1. This is the type of output that appears when you type Get-Disk.

As nice as PowerShell’s output might be, you usually won’t see disk information formatted so neatly. Microsoft has taken care to ensure that if you enter the Get-PhysicalDisk cmdlet without any additional parameters, you’ll see an easy-to-read disk summary. However, if you use the Get-PhysicalDisk cmdlet to retrieve these parameters manually (using Select-Object or some other means), the output quickly becomes harder to decipher. Let me show you what I mean.

In Figure 2, I entered the Get-PhysicalDisk cmdlet as before. This time, however, I piped the cmdlet’s output into the Select-Object cmdlet. The objects I exhibit are the disk friendly name and its size. This is what the command looks like:

Get-PhysicalDisk | Select-Object FriendlyName, Size

Simplify PowerShell 2.jpg

Figure 2. You can use PowerShell to retrieve individual attributes of the machine’s physical disks.

Looking at the figure above, you will no doubt notice that while Figure 1 displayed disk sizes in gigabytes, the command I used in Figure 2 caused the disk size to be displayed below the form of a long, meaningless string of numbers. After all, it’s hard to tell by looking at the figure whether these values ​​represent bytes, kilobytes, megabytes, or something else. Fortunately, there is a way to turn the output into something more readable. Here is the command I will use.

Get-PhysicalDisk | Select-Object FriendlyName, {$_.Size/1GB}

You can see the output of this command in Figure 3.

Brian PoseySimplify PowerShell 3.jpg

Picture 3. Disk sizes have been converted to gigabytes.

Although disk sizes are now in gigabytes, the output really isn’t much better than what we had before. After all, the column name is a mess. Sizes are always difficult to read because many decimal places are displayed.

So what can we do to make things better?

Let’s start by fixing the column header. In order to make the output consistent with what was shown in Figure 1, let’s rename the column to size. To do this, we need to replace the {$_.Size/1GB} with an array that will allow us to specify a column header name and an output format. This is what such a command looks like:

Get-PhysicalDisk | Select-Object FriendlyName, @{Name="Size";Expression={$_.Size/1GB}}

In this case, the @ sign indicates that we are creating an array. This array contains two elements: name and expression. The name is just a text label that indicates what we want to call the column. The expression refers to the value that will be displayed in the column. For this example, the expression consists of disk sizes in GB format. Figure 4 shows what the output looks like.

Brian PoseySimplify PowerShell 4.jpg

Figure 4. I fixed the column headers.

Now that the column header has been fixed, let’s turn our attention to the values. Right now the values ​​are exact, but they show way too many decimal places. The easiest way to solve this problem is to convert the size to a string. This formats the output so that only one decimal place is displayed.

This is what the command looks like:

Get-PhysicalDisk | Select-Object FriendlyName, @{Name="Size";Expression={($_.Size/1GB).tostring("#.#")}}
Brian PoseySimplify PowerShell 5.jpg

Figure 5. Now the output has been formatted so that it is much easier to read.

In case you were wondering, the .tostring part of the command tells PowerShell that after converting the size to gigabytes, we want to take the resulting number and turn it into a string. There (“#.#”). Part of the command tells PowerShell to display the number with a single decimal place. If you wanted the output to be displayed with two decimal places, you would use (“#.##”) instead.

Share.

About Author

Comments are closed.