VMware Cloud Community
vamsiCloud
Contributor
Contributor
Jump to solution

Convert MB to GB in Get-Datastore

Hello Experts,
The following script gives me datastores sizes in MB. I would like to get FreeSpaceMB,CapacityMB
converted into GB.Please help.
$DS = @(foreach($Datastore in Get-Datastore){
get-datastore $Datastore | select Name,FreeSpaceMB,CapacityMB})| Sort-Object FreeSpaceMB
Thanks
Vamsi
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use calculated properties and divide the MB values with the builtin constant 1KB to get GB

$DS = Get-Datastore | Select Name,@{N="FreeSpaceGB";E={$_.FreeSpaceMB/1KB}},@{N="CapacityGB";E={$_.CapacityMB/1KB}} | Sort-Object -Property FreeSpaceGB

You don't need a foreach loop, the pipeline does that for you.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi Vamsi,

to get the datastore sizes in GB instead of MB you can do:

Get-Datastore | `
Select-Object -Property Name,
  @{N="FreeSpaceGB";E={$_.FreeSpaceMB/1KB}},
  @{N="CapacityGB"; E={$_.CapacityMB/1KB}} | `
Sort-Object -Property FreeSpaceGB


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can use calculated properties and divide the MB values with the builtin constant 1KB to get GB

$DS = Get-Datastore | Select Name,@{N="FreeSpaceGB";E={$_.FreeSpaceMB/1KB}},@{N="CapacityGB";E={$_.CapacityMB/1KB}} | Sort-Object -Property FreeSpaceGB

You don't need a foreach loop, the pipeline does that for you.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos