Help with creating text form with 2 questions

Hi all,

I create a script that creates a window where you need to select what group you want to create dl/security and after they choose you need to enter the dl name and is working fine but I want to add one more line that will ask for display name but the script skip this part can you assist, please?

$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 = 'Please enter a group name:'
$form.Controls.Add($label)

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



$form.Topmost = $true

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



if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
   $Name = $textBox.Text
   $Name
  
}     if ($RadioButton1.Checked){


$DLPath = "OU=DL,OU=Alex,DC=alex,DC=local"
New-DistributionGroup -Name $Name -DisplayName $name1  -PrimarySmtpAddress $Name@alex.com  -OrganizationalUnit $DLPath -SamAccountName $Name
}

I am not seeing a spot wher $name1 is defined anywhere in the code.

this is what i want to do add as a text box but don’t know where can you assist?

Using the code you already had written, I just copied the info for Textbox and created the lines for Textbox2.

$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 = 'Please enter a group name:'
$form.Controls.Add($label)

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

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

$form.Topmost = $true

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



if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
   $Name = $textBox.Text
   $Name
   $Name1 = $textBox2.Text
   $Name1
  
}     if ($RadioButton1.Checked){


$DLPath = "OU=DL,OU=Alex,DC=alex,DC=local"
New-DistributionGroup -Name $Name -DisplayName $name1  -PrimarySmtpAddress $Name@alex.com  -OrganizationalUnit $DLPath -SamAccountName $Name
}

See how this works for you.

thank you but I have in my script that its asks first question: “Please enter a group name”

and then i want after the answer that its will ask a new question

any way to add this?

You can follow the same principle that I did for the text box with the label for the second question. It is just a matter of duplicating the code and updating your variables to match.

should I create a new form?

You should not have to create a new form. Try updating your script with the following:

$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 = 'Please enter a group name:'
$form.Controls.Add($label)

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

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(30,20)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Please enter a group name:'
$form.Controls.Add($label2)

$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(30,40)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)

$form.Topmost = $true

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



if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
   $Name = $textBox.Text
   $Name
   $Name1 = $textBox2.Text
   $Name1
  
}     if ($RadioButton1.Checked){


$DLPath = "OU=DL,OU=Alex,DC=alex,DC=local"
New-DistributionGroup -Name $Name -DisplayName $name1  -PrimarySmtpAddress $Name@alex.com  -OrganizationalUnit $DLPath -SamAccountName $Name
}

tried that still its not asking for the second parameter :face_holding_back_tears:
any one else?

What are you seeing when you run the code?

the script is not asking for this data


$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,20)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Please enter the Display Name:'
$form.Controls.Add($label2)```

Alex,

What happens when you run this code here?

$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 = 'Please enter a group name:'
$form.Controls.Add($label)

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

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(30,20)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Please enter the display name you would like:'
$form.Controls.Add($label2)

$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(30,40)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)

$form.Topmost = $true

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



if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
   $Name = $textBox.Text
   $Name
   $Name1 = $textBox2.Text
   $Name1
  
}     if ($RadioButton1.Checked){


$DLPath = "OU=DL,OU=Alex,DC=alex,DC=local"
New-DistributionGroup -Name $Name -DisplayName $name1  -PrimarySmtpAddress $Name@alex.com  -OrganizationalUnit $DLPath -SamAccountName $Name
}

Please attach a screenshot of what you are seeing so we can help you further.


the script is not asking to enter the second label
this is my full script:



# A function to create the form 
function Groups_Form{
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    
    # Set the size of your form
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 500
    $Form.height = 300
    $Form.Text = "Groups Creation"
 
    # Set the font of the text to be used within the form
    $Font = New-Object System.Drawing.Font("Times New Roman",12)
    $Form.Font = $Font

    # Create a group that will contain your radio buttons
    $MyGroupBox = New-Object System.Windows.Forms.GroupBox
    $MyGroupBox.Location = '40,30'
    $MyGroupBox.size = '400,110'
    $MyGroupBox.text = "What type of group you want to create?"
    
    # Create the collection of radio buttons
    $RadioButton1 = New-Object System.Windows.Forms.RadioButton
    $RadioButton1.Location = '20,40'
    $RadioButton1.size = '350,20'
    $RadioButton1.Checked = $true 
    $RadioButton1.Text = "DL"
 
    $RadioButton2 = New-Object System.Windows.Forms.RadioButton
    $RadioButton2.Location = '20,70'
    $RadioButton2.size = '350,20'
    $RadioButton2.Checked = $false
    $RadioButton2.Text = "Secuirty"
 
 
    # Add an OK button
    $OKButton = new-object System.Windows.Forms.Button
    $OKButton.Location = '130,200'
    $OKButton.Size = '100,40' 
    $OKButton.Text = 'OK'
    $OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
 
    #Add a cancel button
    $CancelButton = new-object System.Windows.Forms.Button
    $CancelButton.Location = '255,200'
    $CancelButton.Size = '100,40'
    $CancelButton.Text = "Cancel"
    $CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
 
    # Add all the GroupBox controls on one line
    $MyGroupBox.Controls.AddRange(@($Radiobutton1,$RadioButton2))
 
    # Add all the Form controls on one line 
    $form.Controls.AddRange(@($MyGroupBox,$OKButton,$CancelButton))
 
 
    
    # Assign the Accept and Cancel options in the form to the corresponding buttons
    $form.AcceptButton = $OKButton
    $form.CancelButton = $CancelButton
 
    # Activate the form
    $form.Add_Shown({$form.Activate()})    
    
    # Get the results from the button click
    $dialogResult = $form.ShowDialog()

 if ($Form.DialogResult -eq 'Cancel') {
    [void][System.Windows.Forms.MessageBox]::Show("New group creation cancelled!")
}
    # If the OK button is selected
    if ($dialogResult -eq "OK"){
        
        # Check the current state of each radio button and respond accordingly
   $form = New-Object System.Windows.Forms.Form
$form.Text = 'Enter a Group Name'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$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,120)
$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 = 'Please enter a group name:'
$form.Controls.Add($label)

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

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(30,20)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Please enter the display name you would like:'
$form.Controls.Add($label2)

$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(30,40)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)

$form.Topmost = $true

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



if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
   $Name = $textBox.Text
   $Name
   $Name1 = $textBox2.Text
   $Name1
  
}     if ($RadioButton1.Checked){


$DLPath = "OU=DL,OU=Alex,DC=alex,DC=local"
New-DistributionGroup -Name $Name -DisplayName $Name1  -PrimarySmtpAddress $Name@alex.com  -OrganizationalUnit $DLPath -SamAccountName $Name
}

        elseif ($RadioButton2.Checked){
$Securitypath = "OU=Groups,OU=Alex,DC=alex,DC=local"
New-DistributionGroup -Name $Name   -PrimarySmtpAddress $Name@alex.com -Type Security -OrganizationalUnit $Securitypath  -SamAccountName $Name
}
       
    }
}
 
# Call the function
Groups_Form

Any one who can help?

Alex,

I determined what the issue was. It was only a matter of where the labels and textboxes were being drawn on the form. The below code reflects those simple changes.

# A function to create the form 
function Groups_Form{
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    
    # Set the size of your form
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 500
    $Form.height = 300
    $Form.Text = "Groups Creation"
 
    # Set the font of the text to be used within the form
    $Font = New-Object System.Drawing.Font("Times New Roman",12)
    $Form.Font = $Font

    # Create a group that will contain your radio buttons
    $MyGroupBox = New-Object System.Windows.Forms.GroupBox
    $MyGroupBox.Location = '40,30'
    $MyGroupBox.size = '400,110'
    $MyGroupBox.text = "What type of group you want to create?"
    
    # Create the collection of radio buttons
    $RadioButton1 = New-Object System.Windows.Forms.RadioButton
    $RadioButton1.Location = '20,40'
    $RadioButton1.size = '350,20'
    $RadioButton1.Checked = $true 
    $RadioButton1.Text = "DL"
 
    $RadioButton2 = New-Object System.Windows.Forms.RadioButton
    $RadioButton2.Location = '20,70'
    $RadioButton2.size = '350,20'
    $RadioButton2.Checked = $false
    $RadioButton2.Text = "Secuirty"
 
 
    # Add an OK button
    $OKButton = new-object System.Windows.Forms.Button
    $OKButton.Location = '130,200'
    $OKButton.Size = '100,40' 
    $OKButton.Text = 'OK'
    $OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
 
    #Add a cancel button
    $CancelButton = new-object System.Windows.Forms.Button
    $CancelButton.Location = '255,200'
    $CancelButton.Size = '100,40'
    $CancelButton.Text = "Cancel"
    $CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
 
    # Add all the GroupBox controls on one line
    $MyGroupBox.Controls.AddRange(@($Radiobutton1,$RadioButton2))
 
    # Add all the Form controls on one line 
    $form.Controls.AddRange(@($MyGroupBox,$OKButton,$CancelButton))
 
 
    
    # Assign the Accept and Cancel options in the form to the corresponding buttons
    $form.AcceptButton = $OKButton
    $form.CancelButton = $CancelButton
 
    # Activate the form
    $form.Add_Shown({$form.Activate()})    
    
    # Get the results from the button click
    $dialogResult = $form.ShowDialog()

 if ($Form.DialogResult -eq 'Cancel') {
    [void][System.Windows.Forms.MessageBox]::Show("New group creation cancelled!")
}
    # If the OK button is selected
    if ($dialogResult -eq "OK"){
        
        # Check the current state of each radio button and respond accordingly
   $form = New-Object System.Windows.Forms.Form
$form.Text = 'Enter a Group Name'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$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,120)
$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 = "Please enter a group name:"
$form.Controls.Add($label)

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

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,70)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = "Please enter the display name you would like:"
$form.Controls.Add($label2)

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

$form.Topmost = $true

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



if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
   $Name = $textBox.Text
   $Name
   $Name1 = $textBox2.Text
   $Name1
  
}     if ($RadioButton1.Checked){


$DLPath = "OU=DL,OU=Alex,DC=alex,DC=local"
New-DistributionGroup -Name $Name -DisplayName $Name1  -PrimarySmtpAddress $Name@alex.com  -OrganizationalUnit $DLPath -SamAccountName $Name
}

        elseif ($RadioButton2.Checked){
$Securitypath = "OU=Groups,OU=Alex,DC=alex,DC=local"
New-DistributionGroup -Name $Name   -PrimarySmtpAddress $Name@alex.com -Type Security -OrganizationalUnit $Securitypath  -SamAccountName $Name
}
       
    }
}
 
# Call the function
Groups_Form
1 Like

great thank you

its will work

i was looking for a way that i enter the name of the group click on “ok” and then will be next text form to enter display name is anyway to do that?

anyone else can help me?