I can get all my logical drives and the corresponding freespace with this script:
$elements = get-WmiObject Win32_LogicalDisk
$array=@()
foreach ( $drive in $elements ) {
$freespace = $drive.freespace / (1024*1024*1024)
$freespace = [math]::round($freespace, 1)
$name=$drive.Name
$d=New-Object PSObject
$d | Add-Member -Name Drive -MemberType NoteProperty -Value $name
$d | Add-Member -Name Free_Space -MemberType NoteProperty -Value $freespace
$array+=$d
}
The problem is that I can't figure out how to manipulate my $array
to return the name of the drive with the bigger freespace, with measure -Maximum
for example
Try this:
$LogicalDisks = get-WmiObject Win32_LogicalDisk
$MostSpace = 0
$MostName = "None"
foreach ( $drive in $LogicalDisks ) {
$FreeSpace = $drive.FreeSpace / (1024*1024*1024)
$FreeSpace = [math]::round($FreeSpace, 1)
if ($FreeSpace -gt $MostSpace) {
$MostSpace = $FreeSpace
$MostName = $Drive.Name
}
}
Write-Host $MostName
It'll put the drive letter into $MostName and then you can do what you want with it.