Last active
April 26, 2021 05:28
-
-
Save acast15/7f5f939a7a2fba44628325b11dd97a3d to your computer and use it in GitHub Desktop.
Get Unallocated Space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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