soMuch2Learn
soMuch2Learn

Reputation: 187

Powershell: File properties - Bytes to Mb

Currently have the following script that outputs some file data to a report. The file length is in bytes though and wondering how I can convert that to MB before outputting to the array.

$arr = @()
gci C:\stuff -recurse | ? {$_.PSIsContainer -eq $False} | % {
   $obj = New-Object PSObject
   $obj | Add-Member NoteProperty Directory $_.DirectoryName
   $obj | Add-Member NoteProperty Name $_.Name
   $obj | Add-Member NoteProperty Length $_.Length
   $obj | Add-Member NoteProperty created $_.creationtime
   $obj | Add-Member NoteProperty Access $_.LastAccessTime
   $obj | Add-Member NoteProperty LastWritten $_.LastWriteTime
   $obj | Add-Member NoteProperty Extension $_.Extension
   $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
   $arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\files.csv"

Upvotes: 0

Views: 2587

Answers (1)

Peter Hahndorf
Peter Hahndorf

Reputation: 11222

When converting into MB, just put brackets around it:

 $obj | Add-Member NoteProperty Length ($_.Length/1MB)

or maybe more useful:

 $obj | Add-Member NoteProperty MB ("{0:N3}" -f ($_.Length/1MB))

to only show the first three digits after the point.

Upvotes: 1

Related Questions