//Re-Use PowerShell Scriptblocks//
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
I commented on a blog post today that showed how to use a hash table with Select-Object to format file sizes say as KB.
dir $env:temp -rec | select fullname,@{Name="KB";Expression={$_.length/1kb}}
Since you most likely will want to use something similar for other directories, don't feel you have to re-invent the wheel. Save the hashtable as a scriptblock. Here are some examples:
$KB=@{Name="SizeKB";Expression={$_.length/1kb}}
$MB=@{Name="SizeMB";Expression={$_.length/1mb}}
$GB=@{Name="SizeGB";Expression={$_.length/1gb}}
Now, you can get formatted sizes
dir ~documents -recurse | select fullname,$mb
dir e:vboxdisks -recurse | select name,$gb | format-table -auto
Or if you want to format the number, just update the hash table:
$MB=@{Name="SizeMB";Expression={"{0:F2}"-f ($_.length/1mb)}}
If you define these ahead of time, you can reuse them as needed depending on how you want to format the data.
What other custom scriptblock suggestions do you have?