How to Get Partition Size in GB Using PowerShell
!powershell-get-partition-size-in-gb
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
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
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.
Related Articles
Disk & Partition Operations
- PowerShell Get-Partition - Get partition information
- PowerShell Get-Disk - Get disk properties
- PowerShell Get-Volume - Get volume information
WMI & CIM Operations
- PowerShell Get-WmiObject - Query WMI information
- PowerShell Get-CimInstance - Query CIM instances
- PowerShell Win32 Classes - WMI class reference
System Monitoring
- PowerShell Get-Process - Process information
- PowerShell Get-Service - Service information
- PowerShell Get-RAM-Details - Memory information
- PowerShell Get-CPU-Usage - CPU metrics
Data Selection & Filtering
- PowerShell Select-Object - Select partition properties
- PowerShell Where-Object - Filter partitions
- PowerShell ForEach-Object - Process multiple partitions
Data Calculation & Aggregation
- PowerShell Measure-Object - Calculate disk statistics
- PowerShell Group-Object - Group partitions by properties
Display & Formatting
- PowerShell Format Table - Format partition output
- PowerShell Output Table - Create partition tables
- PowerShell Format List - List format display
Control Flow & Logic
- PowerShell If-Else Statement - Conditional disk checks
- PowerShell Switch Statement - Switch on partition type
- PowerShell Try-Catch - Error handling
- PowerShell For Loops - Loop through partitions
Variables & Collections
- PowerShell Variables - Store partition data
- PowerShell Arrays - Work with partition arrays
- PowerShell Hashtables - Store disk configuration
- PowerShell Add-Member - Add custom partition properties
Output & Export
- PowerShell Output to File - Export partition data
- PowerShell Export CSV - Export to CSV
- PowerShell Import CSV - Import partition data
File System Operations
- PowerShell List Files - List directory files
- PowerShell Get Folder Size - Calculate folder size
- PowerShell Get-ChildItem - List directory contents
Functions & Automation
- PowerShell Functions - Create disk monitoring functions
- PowerShell Measure-Object - Aggregate disk statistics
Comprehensive Guides
- Complete PowerShell Guide - Full PowerShell reference
- Complete PowerShell Tutorial - Comprehensive course