Add a -DisplayHint parameter to Measure-Object for disk-space representations that are easy to grasp
See original GitHub issueFollow-up from https://github.com/PowerShell/PowerShell/issues/13423#issuecomment-681153364:
Summary of the new feature/enhancement
To easily grasp the size of a directory, it would be helpful if instead of:
PS> Get-ChildItem -File | Measure-Object -Sum Length
...
Sum: 19241959
...
it would be nice to get an auto-scaled value that is expressed as a multiple of KB, MB, GB:
PS> Get-ChildItem -File | Measure-Object -Sum Length
...
Sum: 18.35MB
....
Additionally, such scaling should be available on demand. Therefore:
Introduce a new -DisplayHint parameter - analogous to Get-Date’s - as an enumeration value that instructs the formatting system to use specific formatting, as follows:
... | Measure-Object -DisplayHint { None | KB | MB | GB | AutoBytes }
-
The parameter would affect the output formatting of the following properties of
GenericMeasureInfo/GenericObjectMeasureInfooutput type (it wouldn’t apply to theTextMeasureInfooutput type selected with any one of the-Line,-Wordor-Characterparameters):Average,Sum,Maximum,Minimum
-
Nonewould be the default, except if the first input object is of type (derived from)System.IO.FileSystemInfoand the-Propertyvalue isLength, in which caseAutoBytesis implied.
Meaning of the enumeration values:
-
Noneyields the current behavior. -
KB,MB,GBdivide the numbers by the implied binary multiplier and represent the result with2decimal places, including the multiplier suffix:- E.g., with
KB, value1234would be represented as1.21KB, the result of'{0:N2}' -f (1234 / 1kb).
- E.g., with
-
AutoBytesauto-select the highest among the binaryKB,MB,GBmultipliers that makes the scaled value greater than or equal to1, with values below theKBthreshold getting suffixed withB.- E.g.,
1100200would be represented as1.05MB, the result of'{0:N2}' -f (1100200 / 1mb), because MB is the highest multiplier that still yields a scaled value greater than1.
- E.g.,
Proposed technical implementation details (optional)
-
Make
Measure-Objectattach an instanceDisplayHintETS member to itsGenericMeasureInfo/GenericObjectMeasureInfo(as needed). -
Add formatting definitions for
GenericMeasureInfoandGenericObjectMeasureInfothat acts on theDisplayHintinstance member.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (3 by maintainers)

Top Related StackOverflow Question
The proposal suggests a - what I consider sensible - default (auto-selection of an appropriate multiplier, depending on the size of the number for
FileSystemInfoinput with propertyLength), supplemented with the ability to override with a fixed multiplier - or none at all - via-DisplayHint.It absolutely makes sense, because in the case of
FileSystemInfoandLengthwe know that bytes are being measured and presenting the output in those terms is the most useful to the human observer.Indeed: Which do you think is preferable?
Get-ChildItem -File | Measure-Object -Sum Length(with-DisplayHint AutoBytesimplied) vs.Get-ChildItem -File | Measure-Object -Sum Length | Format-Table -View AutoBytesAs stated,
Measure-Objectis an unusual cmdlet, because of its open-ended input - that, and only that, is the reason why the-DisplayHintsolution is appropriate.Please spell such a PSMore-based solution out explicitly for the case at hand.
Input objects know nothing about user preferences. I may want the output in terabytes, you in gigabytes. We either use a default or explicitly specify a preference. It makes no sense to put a preference in the cmdlet itself, since there are an infinite number of options. In terms of the existing formatting system, we should use
Format-Table -View <CustomView>. The problem is that it is too time consuming and has many limitations. As far as PSMore, this could overcome some of these limitations. One key PSMOre feature is that users can modify defaults on the fly and attach formatting to objects.