Skip to content

Add parameter -Delimiter to Set-Content, Out-File, Out-String to complement Get-Content -Delimiter #3855

@mklement0

Description

@mklement0

Get-Content -Delimiter can be used to read text records with an arbitrary (literally matching) terminator into an array of strings, as an alternative to reading line by line.

To complement this functionality, the general-purpose text-outputting cmdlets should support the same parameter for creating such files:

  • Set-Content / Add-Content
  • Out-File
  • Out-String

(ConvertTo-Csv and Export-Csv already have a -Delimiter parameter, but there it is a field separator (line-internal), and a newline is implied as the record separator.)

Note:

  • -Delimiter '' would effectively behave the same way as -NoNewline (which is currently missing from Out-String - see Out-String cmdlet should support -NoNewline too #3684).

  • -Delimiter "`n" on Windows and -Delimiter "`r`n" on Unix would then allow creating text files with the respective other platform's newline sequence on demand - which would also cover Let us specify EOL when using out-file #2145.

  • The term delimiter is problematic and already ambiguous in terms of what is currently implemented:

    • In the context of *-Csv* cmdlets, -Delimiter refers to a separator - something placed between elements (fields), but not after the last.

    • In the context of Get-Content, -Delimiter refers to an (optional) terminator, something (expected to be placed after every element (record), including the last.
      Get-Content -Delimiter treats input that differs only with respect to a trailing delimiter identically - to signal an empty trailing element two trailing delimiters are needed.

    • This Get-Content -Delimiter behavior implies that Set-Content, Out-File, ... should also treat -Delimiter to mean terminator, and add a trailing delimiter instance to the last record.

Desired behavior

'one', 'two' | Set-Content t.txt -Delimiter '@'
get-content t.txt
'---'
get-content t.txt -Delimiter '@' | % { "[$_]" }
one@two@
---
[one]
[two]

Environment Data

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-Won't FixThe issue won't be fixed, possibly due to compatibility reason.WG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions