Skip to content

Get-Content -Raw should always return a string, and should therefore return the empty string for zero-byte (empty) files - scalars vs. arrays (collections) #3911

Closed

Description

This is a follow-up to #3866.

Note: Even if the conclusion will be that Get-Content's behavior mustn't change for the sake of backward compatibility, the underlying scalar-vs.-collection design question is worth getting clarity on, for future reference.

The reasoning for expecting Get-Content -Raw to always return a string scalar is:

  • -Raw explicitly asks for the file contents to be returned as a single string.

  • By contrast, without -Raw - irrespective of the -Encoding requested (including -Byte) - Get-Content inherently returns a collection of items (even though PowerShell unwraps a single-item collection to a scalar for convenience):

    • an array of lines - by default and with any -Encoding value other than Byte
    • an array of bytes with -Encoding Byte

Therefore, with an empty (zero-byte) file:

  • -Raw should return the empty string ('')
  • Not using -Raw should return a null collection ("nothing", which is technically the [System.Management.Automation.Internal.AutomationNull]::Value singleton).

Note: The competing viewpoint from the linked issue is to conceive of a zero-byte file that is read with neither -Raw nor -Encoding Byte as a special case of a one-line file and therefore return the empty string in that case, rather than a null collection.

Steps to reproduce

# Create empty (zero-byte) file.
New-Item -Type File zero.txt

# Read it as a collection of lines, and count the line.
Get-Content zero.txt | Measure-Object | % Count

# Read it as a single string, and count that string (i.e., test if there actually IS a string).
Get-Content -Raw zero.txt | Measure-Object | % Count

# Explicitly compare the single string read to ''
'' -eq (Get-Content -Raw zero.txt)

Expected behavior

0    # Get-Content zero.txt returns a null collection
1    # Get-Content -Raw zero.txt *should* return string scalar ''
True  # Get-Content -Raw zero.txt *should return ''

Actual behavior

0      # OK: Get-Content zero.txt returns a null collection
0      # !! Get-Content -Raw zero.txt also returns a null collection
False  # !! Get-Content -Raw zero.txt does NOT return ''

Environment data

PowerShell Core v6.0.0-beta (v6.0.0-beta.1)

Activity

  1. KirkMunro commented on Jun 2, 2017

    @KirkMunro
    Contributor

    By pulling this out into its own issue, you have discarded the idea that Get-Content without -Raw and without using -Encoding Byte should return an empty string if the file does not contain any characters. There are good arguments indicating that having Get-Content behave differently for empty files than it does for files with one line and differently than it does for files with more than one line is just confusing. That idea should not be discarded just because you don't agree with it. It should be included in this issue as a point of discussion, awaiting input from the PowerShell Team to determine what can/will be done, if anything, with this issue.

  2. mklement0 commented on Jun 2, 2017

    @mklement0
    ContributorAuthor

    Kirk Munro (@KirkMunro)

    While I created this issue from the perspective of what makes sense to me:

    • I linked to the originating issue,

    • and let everyone know there that I had done so.

    That idea should not be discarded

    With your response it was just reintroduced, and that was precisely the point of creating this issue: so we can have a focused discussion here.

    Let the conversation continue - feel free to copy previously made points by you or Keith Hill (@rkeithhill) to this thread.

  3. mklement0 commented on Jun 2, 2017

    @mklement0
    ContributorAuthor

    Kirk Munro (@KirkMunro): I've added a paragraph to the initial post to represent your viewpoint. I hope it sums it up reasonably well. If you agree, let's just continue the conversation here.

  4. mklement0 commented on Jun 2, 2017

    @mklement0
    ContributorAuthor

    having Get-Content behave differently for empty files than it does for files with one line and differently than it does for files with more than one line is just confusing.

    • The one-item collection case (one-line case here) is not specific to Get-Content at all: it is at the heart of PowerShell, as has been mentioned: a one-item collection is automatically unwrapped, by PowerShell itself, irrespective of what command produces it:

      • This behavior is very convenient on the one hand, but, prior to v3, was a constant source of confusion and bugs: Because people thought that a cmdlet returned a collection, they didn't anticipate not getting one if the cmdlet happened to return just a one-item collection that was automatically unwrapped.

      • v3 ingeniously resolved this to retain the best of both worlds: even a scalar can now be treated like a collection, which means that you can consistently use .Count to count the number of items returned and [0] to get the 1st item - whether there's only one or not. In other words: we no longer need to worry about the special-casing of one-item results, and can live happily ever after in the collection mindset.

        • On a side note: foreach ($v in $null) { 'here' } not causing the loop to be entered strikes me as an inconsistency: a scalar $null - as opposed to a null collection - should be treated as an object to iterate over, just as happens with $null | % { 'here' }

    So, now that cmdlets (with the appropriate options) that return collections in principle can consistently be treated as collections in the one- and multi-item cases, why should the no-item case be any different?

    Also note that with your proposal a file made up of just a newline would be treated the same as a zero-byte file.
    With the collection approach, you'd get a null collection in the zero-byte file case, and an empty string in the just-a-newline case.

  5. added
    Issue-Discussion
    on Jun 2, 2017
  6. KirkMunro commented on Jun 2, 2017

    @KirkMunro
    Contributor

    Here's a screenshot of a table showing a comparison of Get-Content calls with and without -Raw, and with and without -Encoding Byte, run against ASCII and UTF-8 files that are either empty, contain only LF, contain a single-line of text with no LF, contain a single-line of text with LF, contain multiple lines of text with no trailing LF, and contain multiple lines of text with a trailing LF.

    image

    What this shows is the following:

    1. As Get-Content stands today, looking only at the output, you can accurately identify exactly what the file the output came from contains as long as you are using either -Raw or -Encoding Byte. If you are not using -Raw or -Encoding Byte, you can still identify what the file the output came from contains, with the exception of knowing what type of newlines were used and with the exception of knowing whether or not there was a trailing newline (unless the file contained nothing other than a newline, in which case you can tell the file contained exactly that).
    2. If Get-Content were changed to return an empty string from an empty file when -Raw is used and when -Encoding is not Byte, you would still be able to make the same determination.
    3. If Get-Content were changed to return an empty string from an empty file when -Raw is not used and when -Encoding is not Byte, you would not be able to determine from the content if the original file ended with a newline or not, but that doesn't matter much because with the exception of files that don't contain any content or files that contain only a newline, you already cannot identify whether or not a file ended with a newline by looking at the content when you invoke Get-Content this way.

    All that said, I see arguments for both sides when it comes to not using -Raw, but I still suspect this falls under the "breaking change without enough tangible benefit or reason, therefore rejected" category.

  7. SteveL-MSFT commented on Jun 3, 2017

    @SteveL-MSFT
    Member

    Cc @PowerShell/area-language

  8. mklement0 commented on Jun 4, 2017

    @mklement0
    ContributorAuthor

    Kirk Munro (@KirkMunro) Thank you for your thorough analysis.

    I still suspect this falls under the "breaking change without enough tangible benefit or reason, therefore rejected" category.

    That may well be the outcome, but I want us to have clarity on what the right solution would be if we didn't have to worry about backward compatibility, so we can apply that pattern in the future:

    • Any call that is designed to return a collection (array) of items in principle should return a null collection ([System.Management.Automation.Internal.AutomationNull]::Value) when no items are being returned.

      • In the case at hand, this applies to Get-Content _without -Raw, irrespective of what -Encoding value if any, and including Byte, is used.
    • Conversely, any call that by design returns a scalar should, if there's nothing to output, return the empty version of that scalar ('' in the case of [string]), as appropriate, falling back to (scalar) $null.

    I see arguments for both sides when it comes to not using -Raw

    I see one historical argument for returning '' when not using -Raw: The historical ambiguity with respect to whether a newline is a separator or a terminator, and the fact that a trailing newline is in practice considered an optional terminator.

    Therefore, you could argue that an empty file ('', when interpreted as a string) and a file containing just <newline> are both a collection of one, empty line, which PowerShell then unwraps to the empty-string scalar.

    My personal preference is to not perpetuate this historical ambiguity with respect to a truly empty file, in favor of overall consistency, but I do realize that with non-empty files (ones with actual content, aside from a Unicode signature ("BOM")), it must be preserved.

  9. added
    Resolution-No Activity
    on Feb 25, 2024
  10. microsoft-github-policy-service commented on Feb 25, 2024

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  11. fourpastmidnight commented on Oct 20, 2025

    @fourpastmidnight

    This is a follow-up to #3866.

    Note: Even if the conclusion will be that Get-Content's behavior mustn't change for the sake of backward compatibility, the underlying scalar-vs.-collection design question is worth getting clarity on, for future reference.

    The reasoning for expecting Get-Content -Raw to always return a string scalar is:

    • -Raw explicitly asks for the file contents to be returned as a single string.

    • By contrast, without -Raw - irrespective of the -Encoding requested (including -Byte) - Get-Content inherently returns a collection of items (even though PowerShell unwraps a single-item collection to a scalar for convenience):

      • an array of lines - by default and with any -Encoding value other than Byte
      • an array of bytes with -Encoding Byte

    Therefore, with an empty (zero-byte) file:

    • -Raw should return the empty string ('')
    • Not using -Raw should return a null collection ("nothing", which is technically the [System.Management.Automation.Internal.AutomationNull]::Value singleton).

    Note: The competing viewpoint from the linked issue is to conceive of a zero-byte file that is read with neither -Raw nor -Encoding Byte as a special case of a one-line file and therefore return the empty string in that case, rather than a null collection.

    Steps to reproduce

    Create empty (zero-byte) file.

    New-Item -Type File zero.txt

    Read it as a collection of lines, and count the line.

    Get-Content zero.txt | Measure-Object | % Count

    Read it as a single string, and count that string (i.e., test if there actually IS a string).

    Get-Content -Raw zero.txt | Measure-Object | % Count

    Explicitly compare the single string read to ''

    '' -eq (Get-Content -Raw zero.txt)

    Expected behavior

    0 # Get-Content zero.txt returns a null collection
    1 # Get-Content -Raw zero.txt should return string scalar ''
    True # Get-Content -Raw zero.txt *should return ''

    Actual behavior

    0 # OK: Get-Content zero.txt returns a null collection
    0 # !! Get-Content -Raw zero.txt also returns a null collection
    False # !! Get-Content -Raw zero.txt does NOT return ''

    Environment data

    PowerShell Core v6.0.0-beta (v6.0.0-beta.1)

    I 100% agree with this analysis. This is EXTREMELY CONFUSING behavior given the documentation. I've been coding in PowerShell quite extensively for 5 years now, and everytime I think I've seen it all, PowerShell throws yet another zinger at me! I wasted literally HOURS on this in my build pipeline all because I trusted the documentation with respect to the -Raw parameter.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-No ActivityWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions