I’m trying to retrieve some information about my hard drives using PowerShell (very new to PS btw).
When calculating the occupancy of the drives some drives are not mounted and will return a size of 0. So I need to introduce a condition where a drive with size 0 must not calculate occupancy.
$fmt = "{0,-5} | {1,-18} | {2,12:N2} | {3,21:N2} | {4,17:p} | {5,-15}"
$docc = { if ( $_.size -ne "0" ) { (($_.size-$_.freespace)/$_.size)} else {"0"} }
Get-WmiObject Win32_logicaldisk | foreach -begin {
$fmt -f "Unit","File System","Capacity(GO)", `
"Available Space(GO)","Occupancy","Observation" } {
$fmt -f $_.deviceID, $_.FileSystem, ($_.size/1GB), `
($_.freespace/1GB), $docc, ""}
The preceding code will only return the if as a string not interpret it (even without using a variable).
You need to execute the scriptblock contained in
$docc– I use the call (&) operator below. The scriptblock doesn’t have access to$_either, so I pass it as a parameter.Hope this helps.