Reputation: 23
I'm looking at adding up the capacity of all disks on localhost machine using a ForEach-Object. I'm able to get the size listed but it lists them separately. How can I combine the size of multiple disks to display as one calculation?
Here is what I have so far:
$drives = Get-WmiObject Win32_logicalDisk
$drivecap = ($drives | ForEach-Object {$_.Size/1GB})
"The Total Capacity of all fixed drives: $drivecap GB" | out-file -FilePath $myfile -append
I'm looking to achieve something like "The Total Capacity of all fixed drives: 800.99000000001 GB"
Upvotes: 2
Views: 172
Reputation: 6472
You could do something like:
$drivecap = ($drives | measure -Property Size -Sum | select -Expand Sum) / 1GB
another alternative:
$drivecap = $drives |% {$x=0} {$x += $_.Size/1GB} {$x}
Upvotes: 0
Reputation: 373
Below code will help you to get the total disk space from the localhost.
$drives = Get-WmiObject Win32_logicalDisk
$total = 0
foreach($drive in $drives) {
$total += $drive.Size/1GB
}
Write-Host "The Total Capacity of all fixed drives:"$total
Upvotes: 1