[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2040
  • Last Modified:

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

0
abenage
Asked:
abenage
  • 4
  • 2
1 Solution
 
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
 
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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:
whoops.. need to add the -computer argument back
0
 
abenageAuthor Commented:
Perfect.  Even with a label.  Thanks!!!
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now