How to use PowerShell to detect unallocated space on a MBR disk?

1.5k views Asked by At

I am currently facing an issue with one of my MBR disks on a Windows server. The disk is supposed to have a maximum size of 2048GB, but I have noticed that there is additional space allocated to it from the storage end. After checking the disk, I found out that there is about 152GB of unallocated space on the disk.

It is worth noting that if the allocated size was less than 2048GB, it would be easy to detect the unallocated space using the diskpart command. However, in this case, the allocated size is more than the maximum size, which makes it difficult to detect the unallocated space.

I have tried to detect this issue using various methods, such as the diskpart command, but I was unable to find any information about this unallocated space. I also tried using the procmon tool and opening the diskmgmt snap-in, but the log was too huge and I couldn't find any useful information.

I am now seeking help from the community to come up with a PowerShell script or command that can detect this unallocated space on the MBR disk. I have been searching online but haven't found any solutions that work for me. If anyone has any experience with this issue or know of a PowerShell script that can detect unallocated space on an MBR disk, please share it with me. I would greatly appreciate any help or guidance on this matter.

1

There are 1 answers

2
Mathias R. Jessen On

I'm not sure how accurate this is, but you can try and count the total amount of allocated space and subtract that number from the total underlying disk size to get the maximum unallocated space:

# Query the Win32_DiskDrive instance representing the physical disk
$disk = Get-CimInstance Win32_DiskDrive -Filter 'Index = 2'

# Find all associated partitions
$partitions = $disk |Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition

# Calculate total allocation for configured partitions
$allocated = $partitions |Measure-Object -Sum Size |Select-Object -Expand Sum

# Calculate the remaining void
$unallocated = $disk.Size - $allocated

Write-Host ("There is {0}GB of disk space unallocated" -f $($unallocated/1GB))