Powershell: Yes/No output from user GUI Input

PowerShelly 61 Reputation points
2021-07-07T14:57:47.717+00:00

below, i'm getting user input to be added to ps1 script. i also then would like to run it on a remote computer without logging on if possible.

# inputbox
$Lname = "What is your last name?"
$Result1  = [Microsoft.VisualBasic.Interaction]::InputBox($Lname,"Last name")
$Fname = "What is your first name?"
$Result2  = [Microsoft.VisualBasic.Interaction]::InputBox($Fname,"First name")
$email = "Please Enter user's email"
$Result3  = [Microsoft.VisualBasic.Interaction]::InputBox($email,"Valid email")
$dept = "Please Enter Dept#"
$Result4  = [Microsoft.VisualBasic.Interaction]::InputBox($dept,"Department number")

need help with below, rather than user input, i want user to select, Male/Female or yes/no, what's a better option if any?

$Result5=[System.Windows.Forms.MessageBox]::Show("Is this new member?","status",4)
if ($result5 -eq "Yes")
{
.\d:\members\addmember.ps1 –Lname $result1 -Fname $result2  –email $result3 –dept $result4  -new $result5"
}
elseif ($result5 -eq "No")
{
.\d:\members\addmember.ps1 –Lname $result1 -Fname $result2  –email $result3 –dept $result4"
}
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-07-08T05:04:31.683+00:00

    Hi,

    You may try the radio buttons from Windows Forms.
    https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.radiobutton

    $Form = New-Object System.Windows.Forms.Form  
    $Form.Text = "Gender"  
    $Form.Size = New-Object System.Drawing.Size(300,200)  
    $Form.StartPosition = "CenterScreen"  
      
    $Label = New-Object system.Windows.Forms.Label  
    $Label.Text = "Please select user's gender"  
    $Label.AutoSize = $true  
    $Label.width = 25  
    $Label.height = 10  
    $Label.location = New-Object System.Drawing.Point(20,20)  
    $Form.Controls.Add($Label)  
      
    $RadioButtonMale = New-Object System.Windows.Forms.RadioButton  
    $RadioButtonMale.Location = New-Object System.Drawing.Point(50,50)  
    $RadioButtonMale.Size = New-Object System.Drawing.Size(75,25)  
    $RadioButtonMale.Text = "Male"  
    $Form.Controls.Add($RadioButtonMale)  
      
    $RadioButtonFemale  = New-Object System.Windows.Forms.RadioButton  
    $RadioButtonFemale.Location = New-Object System.Drawing.Point(50,80)  
    $RadioButtonFemale.Size = New-Object System.Drawing.Size(75,25)  
    $RadioButtonFemale.Text = "Female"  
    $Form.Controls.Add($RadioButtonFemale)  
      
    $ButtonOK = New-Object System.Windows.Forms.Button  
    $ButtonOK.Location = New-Object System.Drawing.Point(75,120)  
    $ButtonOK.Size = New-Object System.Drawing.Size(75,25)  
    $ButtonOK.Text = "OK"  
    $ButtonOK.DialogResult = [System.Windows.Forms.DialogResult]::OK  
    $form.AcceptButton = $ButtonOK  
    $Form.Controls.Add($ButtonOK)  
      
    $ButtonCancel = New-Object System.Windows.Forms.Button  
    $ButtonCancel.Location = New-Object System.Drawing.Point(150,120)  
    $ButtonCancel.Size = New-Object System.Drawing.Size(75,25)  
    $ButtonCancel.Text = "Cancel"  
    $ButtonCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel  
    $Form.CancelButton = $ButtonCancel  
    $Form.Controls.Add($ButtonCancel)  
      
    [void]$Form.ShowDialog()  
    if($RadioButtonMale.Checked){$result6 = "Male"}  
    elseif($RadioButtonFemale.Checked){$result6 = "Female"}  
    $result6  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. PowerShelly 61 Reputation points
    2021-07-23T17:47:22.897+00:00

    Thanks this helps. So many ways of coding.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.