Skip to content

Add $PSStyle.HumanReadable for human-readable file sizes and timestamps in listings - #28468

Open
soroshsabz wants to merge 3 commits into
PowerShell:masterfrom
soroshsabz:add-psstyle-human-readable
Open

soroshsabz wants to merge 3 commits into
PowerShell:masterfrom
soroshsabz:add-psstyle-human-readable

Conversation

@soroshsabz

@soroshsabz soroshsabz commented Nov 20, 2026 •

Copy link
Copy Markdown

PR Summary

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/2024 01:36      859832320    -a--- 04/07/2024 01:36  820.1 MB backup.vhdx
    -a--- 04/07/2024 01:36 1298450612224     -a--- 04/07/2024 01:36    1.1 TB disk.vhdx
    -a--- 04/07/2024 01:35           776     -a--- 04/07/2024 01:35    776 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.

PR Context

Fixes #24011.

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.

PR Checklist

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.
@soroshsabz
soroshsabz requested a review from a team as a code owner November 20, 2026 14:48
Copilot AI balanced review requested due to automatic review settings November 20, 2026 14:48
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

2 open issues
What changed in this PR

Addresses #24011 by adding $PSStyle.HumanReadable settings for file-system display formatting without changing underlying file metadata.

Changes:

  • Enables compact binary file sizes by default and optional relative timestamps, preserving offline-file parentheses and absolute-date fallbacks.
  • Exposes Size, Datetime, and reserved Timespan settings in $PSStyle and its default view. The Unix-specific listing view remains unchanged.
  • Adds formatting tests and updates existing expectations.
File Description
test/​powershell/​Modules/​Microsoft.PowerShell.Utility/​Format-List.Tests.ps1 Updates the expected file-length display.
test/​powershell/​engine/​Formatting/​PSStyle.Tests.ps1 Checks setting defaults and visibility.
test/​powershell/​engine/​Formatting/​HumanReadable.Tests.ps1 Tests size and timestamp rendering, toggles, and fallbacks.
src/​System.Management.Automation/​namespaces/​FileSystemProvider.cs Applies settings in display-property getters.
src/​System.Management.Automation/​FormatAndOutput/​DefaultFormatters/​PowerShellCore_format_ps1xml.cs Displays the new settings in the $PSStyle view.
src/​System.Management.Automation/​FormatAndOutput/​common/​PSStyle.cs Defines and initializes human-readable settings.
src/​System.Management.Automation/​engine/​Utils.cs Adds size and relative-time formatting helpers.

Review effort: Balanced

…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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Preserve precision when scaling large byte counts

src/​System.Management.Automation/​engine/​Utils.cs:1584

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.

2 resolved since last review

Review effort: Balanced

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.
@soroshsabz

Copy link
Copy Markdown
Author

Addressed the previously missed issue from this review in 1d4cfae:

  • 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Use UTC timestamps for DST-safe age assertions

test/​powershell/​engine/​Formatting/​HumanReadable.Tests.ps1:167

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider easier to read file lengths in directory listings.

2 participants