How To Simplify PowerShell Storage Capacity Information
PowerShell can retrieve individual attributes of a machine’s physical disks, but it requires a way to put the data in a more readable format. Here’s how.
November 9, 2022
PowerShell’s Get-PhysicalDisk cmdlet does a great job of displaying a concise summary of your system’s hard disks. Just type Get-PhysicalDisk to see a report of all the disks installed in your system.
You can see what this looks like in Figure 1.
Simplify PowerShell 1
Figure 1. This is the type of output that is displayed when you type Get-Disk.
As nice as PowerShell’s output may be, you won’t typically see disk information formatted so neatly. Microsoft has taken special care to make it so that if you enter the Get-PhysicalDisk cmdlet without any additional parameters, you will 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), then the output quickly becomes more difficult to decipher. Let me show you what I mean.
In Figure 2, I have entered the Get-PhysicalDisk cmdlet as before. This time, however, I have piped the cmdlet’s output into the Select-Object cmdlet. The objects that I am displaying are the disk’s friendly name and its size. Here is what the command looks like:
Get-PhysicalDisk | Select-Object FriendlyName, Size
Simplify PowerShell 2
Figure 2. You can use PowerShell to retrieve individual attributes of the machine’s physical disks.
As you look at the figure above, you will no doubt notice that whereas Figure 1 displayed disk sizes in gigabytes, the command that I used in Figure 2 caused the disk size to be displayed as a long and meaningless string of numbers. After all, it’s tough to tell by looking at the figure if 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 that I will be using.
Get-PhysicalDisk | Select-Object FriendlyName, {$_.Size/1GB}
You can see this command’s output in Figure 3.
Simplify PowerShell 3
Figure 3. The disk sizes have been converted to gigabytes.
Although the disk sizes are now expressed in gigabytes, the output really isn’t much better than what we had before. After all, the column name is a mess. The sizes are still tough to read because so many decimal positions are being displayed.
So, what can we do to improve things?
Let’s start by fixing the column header. In the interest of making the output consistent with what was shown in Figure 1, let’s rename the column to Size. To do so, we have to replace the {$_.Size/1GB} with an array that will allow us to specify a column header name and an output format. Here 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 within the column. For this example, the expression consists of disk sizes in GB format. Figure 4 shows what the output looks like.
Simplify PowerShell 4
Figure 4. I have 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 accurate, but they show way too many decimal positions. The easiest way to fix this problem is to convert the size to a string. That makes it possible to format the output so that only a single decimal place is shown.
Here is what the command looks like:
Get-PhysicalDisk | Select-Object FriendlyName, @{Name="Size";Expression={($_.Size/1GB).tostring("#.#")}}
Simplify PowerShell 5
Figure 5. Now the output has been formatted so that it is much easier to read.
In case you are wondering, the .tostring portion of the command tells PowerShell that after we convert the size to gigabytes, we want to take the resulting number and turn it into a string. The (“#.#”). Portion of the command is what tells PowerShell to display the number with a single decimal place. If you wanted the output to be shown with two decimal places, then you would use (“#.##”) instead.
About the Author
You May Also Like