Skip to content

Add a parameter to Read-Host to allow prefilling the edit buffer #14013

Open

Description

Summary of the new feature/enhancement

It would improve the end-user experience if a Read-Host call were allowed to pre-fill the edit buffer with a default value that the user can either accept as-is or modify:

# WISHFUL THINKING
$var = Read-Host -Prompt 'Enter a path' -DefaultValue '/path/to/default/location'

The user would then see:

Enter a path: /path/to/default/location

with the cursor at the end of the value, allowing them to press Enter to accept the default value as-is, or modify the value as needed.

Proposed technical implementation details

If PSReadLine's PSConsoleHostReadLine function were to be enhanced to become a general-purpose prompting function, as proposed in issue #881, the implementation of -DefaultValue could build on that.

This could also open the door for additional enhancements, such as history recall from a custom history file specified via another new parameter.

Activity

  1. added
    Issue-Enhancement
    on Nov 9, 2020
  2. iSazonov commented on Nov 9, 2020

    @iSazonov
    Collaborator

    I think it would be better to implement this in .Net Runtime. I opened an issue for this there.

  3. mklement0 commented on Nov 9, 2020

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov), you've already opened an issue there? If so, please link to it.

    I also wonder what support we need for hosts other than the console (terminal), which would definitely have to be implemented on the PowerShell side.

  4. iSazonov commented on Nov 10, 2020

    @iSazonov
    Collaborator
  5. mklement0 commented on Nov 11, 2020

    @mklement0
    ContributorAuthor

    Thanks, Ilya (@iSazonov), but I see nothing about the ability to pre-fill (pre-populate) the edit buffer in the linked issue, and the WinAPI's ReadConsole function also doesn't seem to support it,

  6. iSazonov commented on Nov 12, 2020

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) The function uses the input buffer as edit buffer and it can be pre-populated.

  7. mklement0 commented on Nov 12, 2020

    @mklement0
    ContributorAuthor

    Thanks, Ilya (@iSazonov); since I'm not familiar with the ReadConsole function, let me see if my understanding is correct (happy to hide this comment once we have clarity):

    • You're saying that ReadConsole's lpBuffer parameter, despite being documented as _Out_ and only receiving data, can de facto also be used to pre-populate the buffer?

    • In other words: the documentation should be amended to note that, right?

  8. iSazonov commented on Nov 12, 2020

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) Please look our internal static string ReadConsole() method and how it utilizes InitialChars.

  9. mklement0 commented on Nov 12, 2020

    @mklement0
    ContributorAuthor

    Thanks, Ilya (@iSazonov). I see that you can specify an offset into the buffer, so that ReadConsole copies input starting there, but does that mean that the function is still capable of modifying the content before that offset based on user actions (which is crucial for the functionality I'm looking for)?

    (P.S.: There's a grammatical error in one of the variable names: charsReaded should be charsRead).

  10. iSazonov commented on Nov 12, 2020

    @iSazonov
    Collaborator

    but does that mean that the function is still capable of modifying the content before that offset based on user actions (which is crucial for the functionality I'm looking for)?

    It is my understanding how the code works: we always set the pre-populated buffer.
    Also we can edit an input line on Windows :-)

  11. eabase commented on Jun 1, 2023

    @eabase

    Ilya (@iSazonov)

    ...we always set the pre-populated buffer. Also we can edit an input line on Windows.

    So does this mean that this issue is redundant, fixed, or something else?

  12. mklement0 commented on Jun 1, 2023

    @mklement0
    ContributorAuthor

    eabase, whatever lower-level features may or may not be available to implement this feature request (I'm still unclear on that part), it hasn't been implemented.

    The first step toward that would be for the cmdlets working group to decide on whether the feature is worth implementing.

    The issue has been labeled accordingly ("WG-Cmdlets-Utility"), but, unfortunately, there's no telling when this decision will be made.

    I presume that the level of community interest helps with prioritizing issues - though I don't know if there's a tool-assisted process for that (such as sorting by the number of thumbs-up votes).

    As of this writing, there are 31 feature requests awaiting triage by the cmdlets working group, the oldest dating back to Feb 2021:

    https://github.com/PowerShell/PowerShell/issues?q=is%3Aopen+label%3AWG-Cmdlets-Utility+label%3Aneeds-triage+label%3Aissue-enhancement+-label%3Aup-for-grabs+

    Note:

    • The query explicitly excludes issues that also have an "Up-for-Grabs" label, given that this label implies that an issue has been triaged; such issues should no longer have the "Needs-Triage" label, but I guess there's no automated process to remove the latter.

    • The "In-PR" label by itself does not imply that the issue has been triaged, given that it is automatically assigned when an issue is referenced from a PR and given that a PR may be a user's unilateral initiative.

    GitHub
    PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.
  13. mklement0 commented on Jun 1, 2023

    @mklement0
    ContributorAuthor

    Come to think of it, Ilya (@iSazonov):

    There are many feature requests - this one among them - that do NOT have the "Needs-Triage" label, yet clearly haven't been triaged.

    As of this writing, there are 62 such issues (with certain labels that do imply triage having taking place excluded):

    https://github.com/PowerShell/PowerShell/issues?page=3&q=is%3Aopen+label%3AWG-Cmdlets-Utility+label%3Aissue-enhancement+-label%3Aneeds-triage+-label%3Aup-for-grabs+-label%3A%22waiting+-+dotnetcore%22++-label%3A%22hacktoberfest-accepted%22

    Does this mean that the working groups will not pay attention to untriaged items that lack the "Neeeds-Triage" label, or is the "WG-*" label alone enough (in the absence of additional labels that imply completed triage)?

    If the "Needs-Triage" label is required for the working groups to pay attention to and triage items, then all the issues returned by the query above should be labeled with it.

  14. hdave127 commented on Oct 31, 2023

    @hdave127

    I did a workaround by using Set-Clipboard, maybe this solution works for somebody else too.
    $defaultConnString = "Server=$hostname\$sqlInstance;Database=Demo; User Id=######;Password=######;"
    Set-Clipboard -Value $defaultConnString
    $connString = Read-Host "ConnectionString is copied to Clipboard, paste and edit"

  15. 7 remaining items

  16. added
    WG-Reviewed
    on Sep 4, 2024
  17. iSazonov commented on Sep 5, 2024

    @iSazonov
    Collaborator

    if the user hits Enter then the prompt input is the default value, otherwise any characters typed becomes the value.

    How could we input empty value?

  18. 0x0309 commented on Sep 5, 2024

    @0x0309

    if the user hits Enter then the prompt input is the default value, otherwise any characters typed becomes the value.

    I thought the whole point was to be able to edit the default without having to re-type the entire value just to change one character...

  19. eabase commented on Sep 6, 2024

    @eabase

    if the user hits Enter then the prompt input is the default value, otherwise any characters typed becomes the value.

    How could we input empty value?

    That's a good point, and perhaps adding a switch like -SpaceIsEmpty?

  20. added
    Resolution-No Activity
    on Mar 6, 2025
  21. eabase commented on Mar 9, 2025

    @eabase

    Let's bump again in Sept. 2025?

  22. removed
    Resolution-No Activity
    on Mar 9, 2025
  23. phosjlusky commented on Sep 4, 2025

    @phosjlusky

    What are people using as a workaround since Read-Host doesn't handle prefilling?

    I'm sure there's something better than this:

    $Value = bash.exe -c "read -e -p 'Enter a value: ' -i 'defaultvalue' ; echo \`$REPLY"
  24. eabase commented on Sep 4, 2025

    @eabase

    Let's bump again in Sept. 2025?

    BUMP!

  25. added
    Resolution-No Activity
    on Mar 4, 2026
  26. phosjlusky commented on Mar 4, 2026

    @phosjlusky

    Has to be a better way than $Value = bash.exe -c "read -e -p 'Enter a value: ' -i 'defaultvalue' ; echo \`$REPLY"

  27. removed
    Resolution-No Activity
    on Mar 4, 2026
  28. added
    In-PR
    on Nov 20, 2026
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

    In-PRIssue-EnhancementUp-for-GrabsWG-CmdletsWG-Reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions