How and where would I add a rounding operator to round the inventory script I have showing how much storage is being used? I would like to only show one number after the decimal point. Anyone know?
Get-VM | Select-Object Name,MemoryGB,NumCpu,UsedSpaceGB,@{n="TotalHDSizeGB"; e={(Get-HardDisk -VM $_ |Measure-Object -Sum CapacityGB).Sum}},@{n="Network"; e={(Get-NetworkAdapter -VM $_ |Select -unique -expand NetworkName)}} | Sort-Object Network
Above shows one output with used space and way too many numbers after the decimal
Hello, AutomationStation -
You can use the Round() method of the .NET class Math to round your numbers. For example, to round to one decimal place:
PS C:\> [System.Math]::Round(123.42324, 1)
123.4
So, for your example, you could use it as such:
Get-VM | Select-Object Name, MemoryGB, NumCpu,
@{n="UsedSpaceGB"; e={[System.Math]::Round($_.UsedSpaceGB, 1)}},
@{n="TotalHDSizeGB"; e={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}},
@{n="Network"; e={(Get-NetworkAdapter -VM $_ | Select -Unique -Expand NetworkName)}} | Sort-Object Network
How does that do?