How to create UI with multiple text boxes

I am a very new user of powershell. I am reading through some of the documentation and I need to develop a script that will prompt a user for some information, such as an ID and Password. I found an example of some code that is building a nice looking window, but it only has one text box and I need to have two. I have tried to modify the code, but I am not having any success. Here is the code I am using. Can someone let me know what I might be doing incorrectly. I also need to mask the password field so that characters are not displayed. I have not figured this piece out yet, but if you have a quick answer, I would appreciate it.

 

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = ‘Resource Load’
$form.Size = New-Object System.Drawing.Size(350,350)
$form.StartPosition = ‘CenterScreen’

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,260)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = ‘OK’
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,260)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = ‘Cancel’
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = ‘Enter your credentials:’
$form.Controls.Add($label)

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,40)
$label2.Size = New-Object System.Drawing.Size(280,40)
$label2.Text = ‘Please enter your ID’
$form.Controls.Add($label2)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,80)
$textBox.Size = New-Object System.Drawing.Size(260,80)
$form.Controls.Add($textBox)

$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(10,110)
$label3.Size = New-Object System.Drawing.Size(280,110)
$label3.Text = ‘Please enter your password’
$form.Controls.Add($label3)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,160)
$textBox.Size = New-Object System.Drawing.Size(260,160)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $textBox.Text
$x
$y = $textBox2.Text
$y
}

 

 

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,160)
$textBox.Size = New-Object System.Drawing.Size(260,160)
$form.Controls.Add($textBox)

First problem is that your second textbox variable name has the same name as your first textbox. I’m guessing you’ve tried it as $textBox2 because it’s referenced later in the script.

$label3.Size = New-Object System.Drawing.Size(280,110)

The second problem is that the size of this label is obscuring the second text box. It’s starting at 110px, finishing at 220px and you’re trying to show the textbox at 160px. Make the label smaller in height and then the textbox won’t be obscured.

To hide the password, set the PasswordChar property of the textbox control:

$textBox2.PasswordChar = '*'

That obviously doesn’t stop the password being stored in plain text though so you should really also consider whether this is the most secure (and appropriate) solution for your requirements.

 

cookb4ueat34,

If you plan on making more UIs for PowerShell, I would recommend PowerShell Pro Tools for VSCode or Visual Studio if you have that. It lets you build forms with a GUI and creates nice clean code. It’s a little pricey ($99 one time or $9.99 a month), but there is a free trial if you want to test it out. This is not an advertisement and I don’t have any affiliate with them, but I do use it regularly.

@Mike R. Thank you for the heads up on this tool. At this time, I do not plan to make more UI’s than what I am working on now, but will keep this in mind if this changes down the road.

@Matt Bloomfield. Thank you for this information. Using this, I was able to get the adjustments applied where I can now see the second text box and the password masking worked great! Thank you so much!