If you read system information like the free disk space via WMI, PowerShell will display the result in bytes. The same applies to Get-ChildItem (the counterpart to dir in cmd.exe), which also displays file sizes in bytes. In the case of big values, this is not really what you want. However, it is relatively easy to convert megabytes to gigabytes with PowerShell.

For example, if you want to determine the size of a disk, you can run this command:

Convert into GB ^

In this case, the result shows the size of the first disk as a long number. However, you probably will only want to know the size in GB. You can get this easily by dividing the bytes by the predefined constant 1GB:

Converting bytes into GB

Converting bytes into GB

This command uses a so-called calculated property because you can't just divide the Size property in the Select expression. The conversion to KB or MB works the same way.

Truncate and round decimal places ^

If you want to trim the slew of decimal places, you can truncate them entirely with the method truncate(), or you can round them with round():

The truncate() call follows the same pattern, only you have to omit the second parameter, that is, truncate($_.size/1GB).

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

6+

Users who have LIKED this post:

  • avatar
Share
1 Comment
  1. HK 5 days ago

    Thank you. That helped me complete my script!

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account