Home ยป How to Convert Bytes to GB in PowerShell

How to Convert Bytes to GB in PowerShell

You can convert bytes to GB in PowerShell by dividing the bytes by 1GB constant.

The following method shows how you can do it with syntax.

Method 1: Convert bytes to GB in PowerShell

$gb = 1234567890/1GB

This example returns the gigabytes in $gb by dividing bytes by 1GB.

The following example shows how to convert bytes to GB in PowerShell.

Convert Bytes to GB in PowerShell

To convert bytes to gigabytes (GB) in PowerShell, you can divide the number of bytes by 1GB. Here’s how you can do it.

# Define the number of bytes

$bytes = 1024 * 1024 * 1024

# Convert the bytes to gigabytes
$gb = $bytes / 1GB

# Display the result

Write-Output $gb

Output:

PowerShell convert bytes to gb
PowerShell convert bytes to gigabytes

In this example, the $bytes variable stores the number of bytes you want to convert to gigabytes. 1GB is the conversion factor.

Then, we convert the bytes to gigabytes by dividing $bytes by 1GB, and the resulting value in gigabytes is stored in the $gb variable.

When you run the script, the output will display the number of bytes converted to gigabytes.

If you want to display the result with a specific number of decimal places, you can use the ToString() method to specify the number of decimal places.

$bytes = 1234567890

$gigabytes = ($bytes / 1GB).ToString("N2")

Write-Output $gigabytes

Output:

1.15

This will display the converted value in the gigabytes with two decimal places.

Conclusion

I hope the above article on converting bytes to GB in PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.