Home » How to Get Partition Size in GB Using PowerShell

How to Get Partition Size in GB Using PowerShell

To get partition size in GB using PowerShell, you can use the Get-Partition cmdlet. The Get-Partition cmdlet lists all partitions, we then use the Select-Object cmdlet with a calculated property to convert the partition size from bytes to gigabytes.

The following methods show how you can do it with syntax.

Method 1: Get partition size in gigabytes using Get-Partition

Get-Partition | Select-Object @{Label="SizeGB";Expression={[math]::truncate($_.Size / 1GB)}}, DriveLetter

This example returns the list of partitions with size in gigabytes.

Method 2: Get partition size in GB using Get-WmiObject

((Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'").Size)/1GB

This example outputs the size of the specified partition in gigabytes.

The following examples show how you can use these methods to get partition size in GB using PowerShell.

Get Partition Size in Gigabytes Using Get-Partition in PowerShell

You can use the Get-Partition cmdlet in PowerShell to list all partitions and then use the Select-Object cmdlet to convert partition sizes from bytes to gigabytes.

Get-Partition | Select-Object @{Label="SizeGB";Expression={[math]::truncate($_.Size / 1GB)}}, DriveLetter

Output:

PowerShell get partition size in GB using Get-Partition
PowerShell get partition size in GB using Get-Partition

In this example, the Get-Partition cmdlet returns the list of partitions and pipe them to the Select-Object cmdlet.

The Select-Object command uses the calculated property to convert the partition sizes from bytes to gigabytes. The [math]::truncate() method is used to round down the nearest whole number to GB.

After running the PowerShell script, it displays the partition size in GB along with its drive letter.

For example, the C drive has a total of 952 GB.

Get partition size in GB using Get-WmiObject in PowerShell

You can use the Get-WmiObject cmdlet to query the Win32_LogicalDisk class to retrieve the information about the partition and then divide the bytes by 1GB to get the partition size in GB.

# Get the partition size in bytes
$partitionSizeBytes = (Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'").Size

# convert bytes to gigabytes
$partitionSizeInGB = $partitionSizeBytes / 1GB

# Display the partition size in GB
Write-Output $partitionSizeInGB

Output:

PowerShell get partition size in gb using Get-WmiObject
PowerShell get partition size in gb using Get-WmiObject

In this example, the Get-WmiObject command retrieves the information about the partition with device Id C: drive. It returns the partition size in bytes and is stored in the $partitionSizeBytes variable.

The $partitionSizeInGB variable stores the result of the conversion from bytes to gigabytes (GB).

After running the script, the Write-Output cmdlet outputs the size of the specified partition in gigabytes.

Conclusion

I hope the above article on getting partition size in GB using PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.