4

I know how to change the title of the main ISE window using something like

$Host.UI.RawUI.WindowTitle = "My Awesome New Window Title"

but I'm wondering how to get access to the title of the window that pops upon a Read-Host request like:

$strP4Pass = Read-Host "Please enter Perforce Password" -assecurestring

It pops with Windows PowerShell ISE - Input as the title - and yes, I know I have my Please enter Perforce Password prompt within the window - but I'd really like to be able to customize the title - any ideas?

rand0m1
  • 389
  • 1
  • 4
  • 13

2 Answers2

7

You can create a custom prompt with multiple choices using System.Management.Automation.Host.ChoiceDescription

$Yes = New-Object System.Management.Automation.Host.ChoiceDescription "Yes Please"
$No = New-Object System.Management.Automation.Host.ChoiceDescription "No, Thank you"
$YesNoChoices = [System.Management.Automation.Host.ChoiceDescription[]]($No,$Yes)
$Answer = $Host.UI.PromptForChoice("Caption goes here", "Message Goes here", $YesNoChoices, 1)

This will produce a similar UI that you get with -confirm or -whatif, but you can specify the responses you want. This will work in any PowerShell Host, ISE or PowerShell.exe.

Andy Schneider
  • 8,516
  • 6
  • 36
  • 52
  • To make this easier I have created a PowerShell module [get-MultipleChoiceQuestionAnswered](https://github.com/ChrisMagnuson/get-MultipleChoiceQuestionAnswered) so that I can write something like `$Priority = get-MultipleChoiceQuestionAnswered -Question "What priority level should this request have?" -Choices 1,2,3,4` – Chris Magnuson May 18 '15 at 13:58
2

This is a personal answer, but for me ISE is a WorkFrame to edit, and debug your scripts. I prefer PowerGUI for that.

It's NOT the final PowerShell interpreter to execute your scipts. So if you want to add UI to you code you can Have a look on how to integrate Windows Forms or WPF in you scripts. It also exists some modules to help you that way.

Here is a Microsoft serie about WPF

WPF & PowerShell – Part 1 ( Hello World & Welcome to the Week of WPF )

WPF & PowerShell – Part 2 (Exploring WPF (and the rest of .NET) with Scripts)

WPF & PowerShell -- Part 3 (Handling Events)

Have a look to WPK (WPF PowerShell Toolkit)

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • You make a good point - and I don't expect my end users to be using the ISE - but for my own testing, I use KeePass to automate a lot of the repetitive manual entry, and this will do wonders for my own ease of use. And I'll check out those resources, thanks for the directions! – rand0m1 May 07 '11 at 00:03