try this
PS> gc c:\scripts\type\shrf.ps1xml
<Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<Name>FileSize</Name>
<GetScriptBlock>
switch($this.length) {
{ $_ -gt 1tb }
{ "{0:n2} TB" -f ($_ / 1tb) }
{ $_ -gt 1gb }
{ "{0:n2} GB" -f ($_ / 1gb) }
{ $_ -gt 1mb }
{ "{0:n2} MB " -f ($_ / 1mb) }
{ $_ -gt 1kb }
{ "{0:n2} KB " -f ($_ / 1Kb) }
default
{ "{0} B " -f $_}
}
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>
PS> Update-TypeData -AppendPath c:\scripts\type\shrf.ps1xml -verbose
PS> get-childItem $env:windir | select Name,FileSize,length
PS> # you can paste this in your profile
PS>
you can also use dynamic type data with PS3:
PS> Update-TypeData -TypeName System.IO.FileInfo -MemberName FileSize -MemberType ScriptProperty -Value {
switch($this.length) {
{ $_ -gt 1tb }
{ "{0:n2} TB" -f ($_ / 1tb) }
{ $_ -gt 1gb }
{ "{0:n2} GB" -f ($_ / 1gb) }
{ $_ -gt 1mb }
{ "{0:n2} MB " -f ($_ / 1mb) }
{ $_ -gt 1kb }
{ "{0:n2} KB " -f ($_ / 1Kb) }
default
{ "{0} B " -f $_}
}
} -DefaultDisplayPropertySet Mode,LastWriteTime,FileSize,Name
3Great answer! It's hard to believe there isn't a switch for
Get-ChildItem
that would just do this out of the box – Ben Collins – 2015-07-30T15:34:54.990Great answer but has anyone got
-DefaultDisplayPropertySet
to work? – Nick Cox – 2017-12-23T03:04:57.827Doesn't work like @ThomasG.Mayfield said. – ChrisBrownie55 – 2018-11-22T05:01:45.957
I really like building it in as an extra property. The only problem using the PS3 version is I get:
Update-TypeData : Error in TypeData "System.IO.FileInfo": The member DefaultDisplayPropertySet is already present.
Running latest PS3 full release from 9/4. – Tom Mayfield – 2012-09-05T15:24:35.680