Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a $size variable from bytes into GB in Powershell?

In Powershell, I would like to convert $size variable from bytes into GB gigabytes. What is the best way to do it? So far I have the following script:

$dir = "C:\Users\student"
$count = @{}
$size = @{}
$hostname = @{}
gci $dir -recurse |%{
[int]$count[$_.extension] += 1
[int64]$size[$_.extension] += $_.length
}
$results = @()
$count.keys | sort |% {
$result = ""|select extension,count,size,hostname
$result.extension = $_
$result.count = $count[$_]
$result.size = $size[$_]
$result.hostname = $(get-content env:computername)
$results += $result
}
$results | ft -auto
$results | sort-object -property size -Descending | select-object -first 30| export-csv c:\"$env:computername-$(get-date -f dd-MM-yyyy-HH-mm)".csv
like image 971
Gazel Avatar asked Jul 04 '13 09:07

Gazel


1 Answers

there is a trick for it

$result.size = $size[$_] /1Gb

and if you want better view for results you can truncate it

$result.size = [math]::round($size[$_] /1Gb, 3)

http://technet.microsoft.com/en-us/library/ee692684.aspx

like image 179
sqladmin Avatar answered Oct 06 '22 15:10

sqladmin