powershellautomationautocomplete

PowerShell Automation Choice Description has variants with same starting letters, how to add more unique letters?


PowerShell Automation Choice Description has variants with same starting letters, how to add more unique letters? or maybe is there any autocomplete functionality? (currently im on PSh 7.4 version)

[P] Pictures
[P] Photos

[Pi] Pictures
[Ph] Photos

Solution

  • PromptForChoice only supports single-key hotkeys, but you can control which one by inserting a leading & immediately before the corresponding character in the choice description label:

    $caption = "A monumentous decision"
    $message = "Photos or Pictures?"
    
    $choices = @(
        '&Pictures',
        'P&hotos'
    )
    
    $Host.UI.PromptForChoice($caption, $message, $choices, 0)
    
    A monumentous decision
    Photos or Pictures?
    [P] Pictures  [H] Photos  [?] Help (default is "P")
    

    That's all well and good, but what if you want to use a hotkey whose character representation shouldn't be part of the rendered label?

    In this case you can use the backspace character (ASCII 0x08) to hide a trailing hotkey:

    $BS = [char]0x8
    $choices = @(
        '&Pictures',
        "Photos&F${BS}"
    )
    
    A monumentous decision
    Photos or Pictures?
    [P] Pictures  [F] Photos  [?] Help (default is "P"):