You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add $PSStyle.HumanReadable for human-readable file sizes and timestamps in listings - #28468
Adds a $PSStyle.HumanReadable settings object whose boolean properties control which kinds of formatted data are rendered in human-readable form, and applies it to directory listings, per the Working Group decision for #24011:
Size (default $true): the Length column of directory listings (Get-ChildItem / Get-Item) renders file sizes in compact binary (1024-based) units, similar to Windows File Explorer:
# before (default) # after (default)
Mode LastWriteTime Length Mode LastWriteTime Length Name
---------------------------------------------------a---04/07/202401:36859832320-a---04/07/202401:36820.1 MB backup.vhdx
-a---04/07/202401:361298450612224-a---04/07/202401:361.1 TB disk.vhdx
-a---04/07/202401:35776-a---04/07/202401:35776 B BackupSpecs.xml
Files not on disk (FileAttributes.Offline) keep the existing parentheses convention, e.g. (2 KB). Setting $PSStyle.HumanReadable.Size = $false restores the raw byte count.
Datetime (default $false): when enabled, the LastWriteTime column renders relative timestamps — just now, 5 min ago, 19 hours ago, 6 days ago — falling back to the existing culture-aware short date/time for values older than 7 days or in the future.
Timespan (default $false): exposed per the WG decision so it can be used by follow-up work on System.TimeSpan views; currently has no effect.
Only display formatting is affected — the underlying Length/LastWriteTime object properties are unchanged. The rendering happens inside the existing LengthString / LastWriteTimeString code properties that the FileSystem format views already reference, so the setting is honored at render time without reloading format data. The $PSStyle default view lists the new settings.
The cmdlet Working Group decision was to expose $PSStyle.HumanReadable with Size/Datetime/Timespan booleans, only Size defaulting to $true, with ps1xml files using $PSStyle to determine when to humanize. The formatting choices follow the implementation design proposals in kilasuit#171:
binary (1024-based) units, matching Windows File Explorer and the existing Utils.DisplayHumanReadableFileSize used for progress reporting
compact, at most one decimal digit, truncated rather than rounded so a value never renders as 1024 of a unit
the offline-file (size) wrapping is preserved, per this note
relative timestamps for recent values with an absolute fallback
Note: the Unix childrenWithUnixStat view's Size column (backed by UnixStat, which reports the link size via lstat(2)) is intentionally left unchanged for now; aligning it can be discussed separately since its semantics differ from FileInfo.Length.
Implement the cmdlet Working Group decision for PowerShell#24011 by exposing a
$PSStyle.HumanReadable settings object whose boolean properties control
which kinds of formatted data are rendered in human-readable form.
- HumanReadable.Size (default $true): the LengthString code property
used by the FileSystem format views renders file sizes in compact
binary units ("745 B", "16.7 KB", "820.1 MB", "1.1 TB"), preserving
the parentheses convention for offline files ("(2 KB)"). Disabling
the setting restores the raw byte count.
- HumanReadable.Datetime (default $false): when enabled,
LastWriteTimeString renders relative timestamps ("just now",
"5 min ago", "19 hours ago", "6 days ago") and falls back to the
existing culture-aware short date/time for values older than 7 days
or in the future.
- HumanReadable.Timespan (default $false): reserved for follow-up work
on System.TimeSpan views; no output changes yet.
Object properties are unaffected; only display formatting changes.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The listing tests use the wrong default view on Linux and macOS, causing assertion failures, while local-time subtraction incorrectly computes file age across daylight-saving transitions.
…tests
- Compute the elapsed time in UTC in ToRelativeTimeString so relative
timestamps stay correct when a daylight-saving time transition
occurs between the file''s LastWriteTime and the current time. The
absolute fallback still renders the original local time.
- Select the ''children'' table view explicitly in the directory listing
tests. On Unix platforms the default view for FileSystemInfo is
''childrenWithUnixStat'', which renders the raw UnixStat size and
formats LastWriteTime directly, so the LengthString and
LastWriteTimeString properties under test are not exercised by the
default view there.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Double conversion loses precision at large unit boundaries, causing 1 EB minus one byte to display as 1 EB rather than the promised truncated 1023.9 PB; decimal arithmetic and a boundary regression test are needed.
Converting the byte count to double loses precision at large unit boundaries. For example, 1152921504606846975 bytes (1 EB minus one byte) becomes exactly 1 EB before the loop runs, so this returns 1 EB instead of the promised truncated 1023.9 PB. Use decimal arithmetic for scaling and truncation, and add a boundary regression test that does not require creating a large file.
Converting the byte count to double rounds values with more than 53
significant bits before scaling, so 1152921504606846975 bytes (1 EB
minus one byte) became exactly 1 EB and rendered as "1 EB" instead of
the truncated "1023.9 PB". Scale and truncate with decimal arithmetic,
which is exact for the entire 64-bit range, and add boundary regression
tests that invoke the internal helper through reflection so that no
large files need to be created.
ToHumanReadableSize now scales and truncates using decimal arithmetic instead of double. As the review noted, converting 1152921504606846975 bytes (1 EB minus one byte) to double rounds it to exactly 2^60 before the scaling loop runs, producing 1 EB; decimal is exact for the entire 64-bit range, so it now renders as the truncated 1023.9 PB promised by the doc comment.
Added boundary regression tests that invoke the internal helper through reflection (resolving Utils from PSStyle's assembly), covering 1024, 1 MB - 1, 1 TB - 1, 1 PB - 1, 1 EB - 1, and [long]::MaxValue — no large files needed.
Verified locally: 1 EB - 1 → 1023.9 PB, 1 PB - 1 → 1023.9 TB, 1 TB - 1 → 1023.9 GB, [long]::MaxValue → 7.9 EB (truncated, per the documented never-show-1024 policy), and the existing cases (1024 → 1 KB, 1500 → 1.4 KB, 745 → 745 B) are unchanged. HumanReadable.Tests.ps1 is 24/24 green.
Since the review had no open threads, replying here rather than on a discussion thread.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The tests use local-time subtraction that can yield incorrect elapsed ages across daylight-saving transitions; both cases should set LastWriteTimeUtc from UtcNow minus the expected delta.
Across a spring-forward transition, subtracting 3.05 local hours can produce a timestamp only 2.05 elapsed hours old, failing the 3 hours ago assertion. Set LastWriteTimeUtc using UtcNow minus $Delta so the expected age remains valid across daylight-saving transitions. Apply the same UTC setup to the five-minute listing test at line 189.
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
Adds a
$PSStyle.HumanReadablesettings object whose boolean properties control which kinds of formatted data are rendered in human-readable form, and applies it to directory listings, per the Working Group decision for #24011:Size(default$true): theLengthcolumn of directory listings (Get-ChildItem/Get-Item) renders file sizes in compact binary (1024-based) units, similar to Windows File Explorer:Files not on disk (
FileAttributes.Offline) keep the existing parentheses convention, e.g.(2 KB). Setting$PSStyle.HumanReadable.Size = $falserestores the raw byte count.Datetime(default$false): when enabled, theLastWriteTimecolumn renders relative timestamps —just now,5 min ago,19 hours ago,6 days ago— falling back to the existing culture-aware short date/time for values older than 7 days or in the future.Timespan(default$false): exposed per the WG decision so it can be used by follow-up work onSystem.TimeSpanviews; currently has no effect.Only display formatting is affected — the underlying
Length/LastWriteTimeobject properties are unchanged. The rendering happens inside the existingLengthString/LastWriteTimeStringcode properties that the FileSystem format views already reference, so the setting is honored at render time without reloading format data. The$PSStyledefault view lists the new settings.PR Context
Fixes #24011.
The cmdlet Working Group decision was to expose
$PSStyle.HumanReadablewithSize/Datetime/Timespanbooleans, onlySizedefaulting to$true, withps1xmlfiles using$PSStyleto determine when to humanize. The formatting choices follow the implementation design proposals in kilasuit#171:Utils.DisplayHumanReadableFileSizeused for progress reporting1024of a unit(size)wrapping is preserved, per this noteNote: the Unix
childrenWithUnixStatview'sSizecolumn (backed byUnixStat, which reports the link size vialstat(2)) is intentionally left unchanged for now; aligning it can be discussed separately since its semantics differ fromFileInfo.Length.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header$PSStyle.HumanReadablesettings for human-readable file sizes and timestamps (PowerShell 7.8) MicrosoftDocs/PowerShell-Docs#13407