Skip to content

On top of #19989, add -Format parameter to Write-Host #20121

Closed

Description

@iRon7

Summary of the new feature / enhancement

Add -Format parameter (or something similar) to Write-Host on top of #19989
Coming from: Powershell Get-ChildItem progress indication

Wishful thinking:

Get-ChildItem -Recurse | Write-Host -PassThru -Format "Processing: $_" | Measure-Object -Property Length -Sum

Activity

  1. added
    Issue-Enhancement
    Needs-Triage
    on Aug 16, 2023
  2. mklement0 commented on Aug 16, 2023

    @mklement0
    Contributor

    This too sounds like it could be addressed with Dominik Kowalski (@dkaszews)' yet-to-be-fully-fleshed-out Tee-Object -Process { ... } proposal:

  3. dkaszews commented on Aug 16, 2023

    @dkaszews
    Contributor

    I don't get, how would it differ from just Write-Host "Processing: $_"?

  4. iRon7 commented on Aug 16, 2023

    @iRon7
    Author

    Dominik Kowalski (@dkaszews),

    I don't get, how would it differ from just Write-Host "Processing: $_"?

    You can't put that in the middle of a pipeline as it accepts only an inputobject from the pipeline or a parameter and it writes by default to the information stream, e.g.:

    1,2,3 | Write-Host -PassThru -Format "Processing: $_" | Set-Content .\test.txt
  5. dkaszews commented on Aug 16, 2023

    @dkaszews
    Contributor

    But you're talking about Write-Host -PassThru which is another proposal. What I'm asking is: if Write-Host -PassThru exists, how will 1,2,3 | Write-Host -PassThru -Format "Processing: $_" | Set-Content .\test.txt differ from 1,2,3 | Write-Host -PassThru "Processing: $_" | Set-Content .\test.txt.

    If this -Format switch would be required to properly anchor $_, I think you'd be better of with any of the following for this specific usecase, as they achieve the same exact thing and are either already possible or IMO easier to implement:

    ... | %{ Write-Host "Processing: $_"; $_ } | ...
    ... | %{ Write-Host "Processing: $_" -PassThru } | ...
    # Alternative proposal
    ... | %{ Write-Host "Processing: $_" } -PassThru | ...
  6. iRon7 commented on Aug 17, 2023

    @iRon7
    Author

    Dominik Kowalski (@dkaszews),

    how will 1,2,3 | Write-Host -PassThru -Format "Processing: $_" | Set-Content .\test.txt differ from ...

    Just to be clear:

    • Just 1,2,3 | Write-Host "Processing: $_" (currently) produces an error:
      Write-Host: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
      (meaning that you need to create an additional parameter if you want to use the value-from-pipeline)
    • Write-Host doesn't currently output to the success stream but directly to the information stream (host)
    • For all (different) implications, the -PassThru parameter causes a cmdlet that doesn't output to the success stream

    For your suggestions that embed a cmdlet in a ScriptBlock ({ ... }), that is one of the reasons for my initial purpose as this would involve a complete new pipeline with the overhead of all (Begin, End) processing blocks.
    This would not be always necessarily, e.g. something like this:

    ... | Foreach-Object {  $_ | Export-Csv -Path .\Log.csv; $_ } | ...

    or in this add-on purpose:

    ... | Foreach-Object { Write-Host -Format "Processing: $_"; $_ } | ...

    Could have a nicer syntax and presumably preforms better if it could be written as (follow up issue #20133):

    ... | Export-Csv -Path .\Log.csv -Reprocess | ...

    or in this add-on purpose (which still might be useful at the end of the pipeline):

    ... | Write-Host -Format "Processing: $_" -Reprocess | ...
  7. mklement0 commented on Aug 18, 2023

    @mklement0
    Contributor

    iRon7, I understand your intent, but as an aside: "Processing: $_" could never work, because it would be expanded up front, at which point $_ has no meaningful value, so perhaps you meant 'Processing: $_' (single-quoting), or, more idiomatically, { "Processing: $_" }.

  8. iRon7 commented on Aug 18, 2023

    @iRon7
    Author

    Michael Klement (@mklement0),

    so perhaps you meant 'Processing: $_' (single-quoting),

    Yes, you're right.
    Anyways, I will close this "add-on" propose as it is now covered by my new updated purpose #20133;

  9. removed
    Needs-Triage
    on Aug 18, 2023
  10. mklement0 commented on Aug 19, 2023

    @mklement0
    Contributor

    Sounds good, iRon7; on a quick meta note: the words you're looking for are "proposal" (as a noun) and "propose" (as a verb).

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-Enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions