Convert bytes to megabytes

How can I convert freespace property to megabytes?

My impulse is to do something like this:
$objItem.Freespace = $objItem.Freespace / 1MB
write-host "Free Space: " $objItem.Freespace

but that does not work

Any ideas?

Function ListLogicalDisk {
$colItems = get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2" -computer $args[0] 
write-Host "Disk Information"
foreach ($objItem in $colItems) {
write-host "Description: " $objItem.Description
write-host "Device ID: " $objItem.DeviceID
write-host "Free Space: " $objItem.Freespace
write-host "Size: " $objItem.Size
write-host "Status: " $objItem.Status
write-host
}
}

Open in new window

LVL 1
abenageAsked:
Who is Participating?
 
BSonPoshCommented:
Try this
Function ListLogicalDisk {
$colItems = get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2"
write-Host "Disk Information"
foreach ($objItem in $colItems) {
write-host "Description: " $objItem.Description
write-host "Device ID: " $objItem.DeviceID
write-host "Free Space: " ("{0:n2} MB" -f ($objItem.Freespace/1mb))
write-host "Size: " $objItem.Size
write-host "Status: " $objItem.Status
write-host
}
}

Open in new window

0
 
BSonPoshCommented:
$Freespace = $objItem.Freespace / 1MB
write-host "Free Space: $FreeSpace"
0
 
BSonPoshCommented:
The key here is understanding that $objItem.Freespace is a (read-only) property.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
abenageAuthor Commented:
OK, that makes sense, that it's read-only, but it's still not working correctly.  I get a zero returned.  Even if I change it to:
$Freespace = $objItem.Freespace / 2
I get a 0 returned, so I think this might be a datatype issue.  Is that possible?
0
 
BSonPoshCommented:
whoops.. need to add the -computer argument back
0
 
abenageAuthor Commented:
Perfect.  Even with a label.  Thanks!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.