We help IT Professionals succeed at work.

Convert bytes to megabytes

abenage
abenage asked
on
Medium Priority
2,094 Views
Last Modified: 2012-08-13
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

Comment
Watch Question

Commented:
$Freespace = $objItem.Freespace / 1MB
write-host "Free Space: $FreeSpace"

Commented:
The key here is understanding that $objItem.Freespace is a (read-only) property.

Author

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?
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
whoops.. need to add the -computer argument back

Author

Commented:
Perfect.  Even with a label.  Thanks!!!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.