Skip to content

Instantly share code, notes, and snippets.

@acast15
Last active April 26, 2021 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acast15/7f5f939a7a2fba44628325b11dd97a3d to your computer and use it in GitHub Desktop.
Save acast15/7f5f939a7a2fba44628325b11dd97a3d to your computer and use it in GitHub Desktop.
Get Unallocated Space
$disks = Get-WmiObject win32_diskdrive ## Gets the list of disks
$partitions = Get-WmiObject win32_diskpartition ## Gets the list of partitions
## Defining Variables
$i = 0
$partitionCount = 0
$myTable = $null
## Scanning Disks and Partitions and their Sizes
foreach($disk in $disks) {
foreach($partition in $partitions) {
if($partition.DiskIndex -eq $disk.index) {
$partitionCount++
$i += $partition.size
}
}
## Create object to store the values of the Disk, Partitions and their Sizes
$myTable = [PSCustomObject]@{
"Disk Index" = $disk.index
"Partition Count" = $partitionCount
"Total Space(MB)" = $(($disk.size)/1mb)
"Unallocated Space(MB)" = $(($disk.size - $i)/1mb)
}
## Display values stored in $myTable
Write-Output $myTable
## Resetting the variable values
$myTable = $null
$i=0
$partitionCount=0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment