Powershell Custom GUI input box for passing values to Variables

Hi Readers,

Here is the custom button that you can use in your script for getting input from users.

This will make your script user friendly .

So below is the example , that creates a button as below:

Capture

Creating function so as to return the values that is being input in the form.

function button ($title,$mailbx, $WF, $TF) {

###################Load Assembly for creating form & button######

[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)

#####Define the form size & placement

$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 150;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;

$textLabel1.Text = $mailbx;

##############Define text label2

$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 50;

$textLabel2.Text = $WF;

##############Define text label3

$textLabel3 = New-Object “System.Windows.Forms.Label”;
$textLabel3.Left = 25;
$textLabel3.Top = 85;

$textLabel3.Text = $TF;

############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;

############Define text box2 for input

$textBox2 = New-Object “System.Windows.Forms.TextBox”;
$textBox2.Left = 150;
$textBox2.Top = 50;
$textBox2.width = 200;

############Define text box3 for input

$textBox3 = New-Object “System.Windows.Forms.TextBox”;
$textBox3.Left = 150;
$textBox3.Top = 90;
$textBox3.width = 200;

#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
$textBox2.Text = $defaultValue;
$textBox3.Text = $defaultValue;

#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Ok”;

############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$form.Close();};

$button.Add_Click($eventHandler) ;

#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textLabel3);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$form.Controls.Add($textBox3);
$ret = $form.ShowDialog();

#################return values

return $textBox1.Text, $textBox2.Text, $textBox3.Text
}

$return= button “Enter Folders” “Enter mailbox” “Working Folder” “Target Folder”

Below variables will get the values that had been entered by the user

$return[0]
$return[1]
$return[2]

So you can use this code to design as many values /variables as you want, I have done it for three values in above example.

Regards

Sukhija Vikas

PowerShell Fast Track

31 thoughts on “Powershell Custom GUI input box for passing values to Variables

  1. Hi Sukhija,

    Thanks for this blog.I’m just getting started with Powershell and found this very easy to follow. I have been looking for a project to work on and as active directory doesn’t display the member count in a group i have decided to have a go at creating a tool that displays the member count of an AD group for my team to use.

    This command; (Get-ADGroup “AD GROUP NAME ” -Properties *).member.count, Displays the member count. I just need some help on passing the content from a GUI text box to the command and then displaying the output.

    I feel as if i may be a bit head of myself, but its in my head now and after reading this blog i’m thinking it must be on cards.

    Any help would be appreciated

    Si

  2. Thanks a lot Vikas ! This is great !

    Can you please suggest a way to mask the input in one of the input box? Something like ***************. I want the user to enter password in one of the boxes

  3. Hello Sukhija,
    Thank you for posting this! I have modified your script and added it to an existing (very lengthy script) to work with new user creations. It works like a charm! I have on question though, when I try to add a ListBox, it seems to “break” the script where it will not return any of the text entered in the TextBox entries. If I add a listbox using New-Object “System.Windows.Forms.Listbox”; it just returns the numerical value located in between the brackets [] at the end of your script for the $return values of the text boxes. I’m guessing the syntax/formatting of the FUNCTION and RETURN items if I use a ListBox. Just thought I’d check. Thanks again for posting your script, as it is already saving a TON of time and effort for our scripting needs!

  4. When I use this script and cancel out by clicking on the red cross, the value that I put in the textbox is still used in the rest of my code and added to my exchange user. How can I do a clean exit/cancel with a button or the red cross?

  5. Add exit button. some thing like that:
    $closeButton.Location = New-Object System.Drawing.Size(X,Y)
    $closeButton.Size = New-Object System.Drawing.Size(X,Y)
    $closeButton.Text = “exit”
    $closeButton.Add_Click({
    exit})
    $objForm.Controls.Add($closeButton)

    • Hi Vikas

      Thank you so much for your script, it works so well.
      I tried your cancel button but it doesn’t seam to work.
      I always get a bad Windows error message.

      Do you know how can pressing the “Cancel” button close the script and end its execution ?

      I can give you my script if you want to check it back.

  6. I am trying to figure out how I would add the values to an email in the message body. I have configured the email function but having trouble figuring out how to add the values. Any suggestions?

  7. Your second query got in to spam..
    Yes that can also be done, you have to explain me from where you want to insert variables.. Just share the script , I will take a look..

  8. Hello
    This is just what i was looking for thanks.
    but only it is not working here. when i us your exempel script i get al erros that he cant make or find or now what te command are.

    what am i missing?

  9. Hi Mr. Sukhija. This post has been very helpful for me in creating the tool I wanted. I have some questions about it. I only have two fields instead of three in the script that I created and I’m having issues using the values that are created to by the second box. So the first field is “username” and the second field is “password”. I want to use the value created in the password field for ” $NewPassword”. $return works when I use it like this $NewPassword = $return. But I need for $Username = to work. I also would like to have a prompt come up that will say Verify user and close if you click No and come up with a prompt after that and say Verify User and when Yes i clicked I want it to proceed to the script. Let me know if you need any other details. Thanks so much for you help.

  10. Hi Vikas, I am trying to fetch the hardware info of multiple server by entering the server name one by one into the input box. But when i enter the second variable(Second server name) it is throwing the details of first variable (First server name). I am not able to delete the first variable. Can you please assist me with this ?

  11. Hi Vikas,
    thank you for this Code. Can you help me how to add a Checkbox to this form? Thank you

    Best regards,
    Torsten

  12. I saved your code in a file called Input.ps1 but it wouldn’t run in a PowerShell window. (I saved it to c:\users\robert.logan\desktop and changed the directory of the PowerShell windows to the same folder.) In that PowerShell command I typed “./Input.ps1” (no quotes) to run it. Will your code work in PowerShell 5.0?

  13. Hi Vikas.

    Thanks for taking the time to write this excellent post.

    I’m trying to build a script to query the IP addresses of a given host.
    I used your example and wrote the code below.
    The problem is that every time I click on “Listar IPs” the ListBox does not get the z variable output correctly.
    Just print’s this:
    “System.Object[]”

    If I remove the line ” $z = & $z ” it then prints the entire command, not the output of the command…
    Any idea of what am I doing wrong?

    Cheers,
    Pedro

    • My script:

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

      $form = New-Object System.Windows.Forms.Form
      $form.Text = “Pesquisa de IP via WMI”
      $form.Size = New-Object System.Drawing.Size(350,250)
      $form.StartPosition = “CenterScreen”

      $label = New-Object System.Windows.Forms.Label
      $label.Location = New-Object System.Drawing.Point(10,20)
      $label.Size = New-Object System.Drawing.Size(100,20)
      $label.Text = “Nome da máquina:”
      $form.Controls.Add($label)

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

      $textBoxResults = New-Object System.Windows.Forms.ListBox
      $textBoxResults.Location = New-Object System.Drawing.Point(10,60)
      $textBoxResults.Size = New-Object System.Drawing.Size(300,100)
      $form.Controls.Add($textBoxResults)

      $f=
      {
      $x = $textBox.Text
      $z=
      {
      Get-WmiObject win32_networkadapterconfiguration -filter “ipenabled = ‘True'” -ComputerName $x |
      Select PSComputername,
      @{Name = “IPAddress”;Expression = {
      [regex]$rx = “(\d{1,3}(\.?)){4}”
      $rx.matches($_.IPAddress).Value}},MACAddress
      }
      $z = & $z
      $textBoxResults.Items.Add($z)

      }

      $ListIPButton = New-Object System.Windows.Forms.Button
      $ListIPButton.Location = New-Object System.Drawing.Point(200,19)
      $ListIPButton.Size = New-Object System.Drawing.Size(75,23)
      $ListIPButton.Text = “Listar IPs”
      $form.AcceptButton = $ListIPButton
      $form.Controls.Add($ListIPButton)

      $ListIPButton.add_Click($f)

      $OKButton = New-Object System.Windows.Forms.Button
      $OKButton.Location = New-Object System.Drawing.Point(75,160)
      $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,160)
      $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)

      $form.Topmost = $True

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

  14. I want to pass TextBox.Text value how can I pass it in this code (AT THIS LINE
    netsh advfirewall firewall add rule dir=in action=block protocol=TCP localport=$TextBox.Text name=”Block_TCP-$TextBox.Text” | Out-GridView)

    Full code is below I want to add firewall rule

    [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
    [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
    [void] [System.Windows.Forms.Application]::EnableVisualStyles()

    function Get-NetworkShell
    {
    netsh advfirewall firewall add rule dir=in action=block protocol=TCP localport=$TextBox.Text name=”Block_TCP-$TextBox.Text” | Out-GridView
    }

    $Form = New-Object system.Windows.Forms.Form
    $Form.Size = New-Object System.Drawing.Size(400,200)
    #You can use the below method as well
    #$Form.Width = 400
    #$Form.Height = 200
    $form.MaximizeBox = $false
    $Form.StartPosition = “CenterScreen”
    $Form.FormBorderStyle = ‘Fixed3D’
    $Form.Text = “Port Blocking”

    $TextBox = New-Object System.Windows.Forms.TextBox
    $TextBox.Text = “”
    $TextBox.AutoSize = $true
    $TextBox.Location = New-Object System.Drawing.Size(140,45)
    $TextBox.Size = New-Object System.Drawing.Size(120,30)
    $Font = New-Object System.Drawing.Font(“Arial”,15,[System.Drawing.FontStyle]::Bold)
    $form.Font = $Font
    $Form.Controls.Add($TextBox)

    #$formIcon = New-Object system.drawing.icon (“$env:USERPROFILE\desktop\Blog\v.ico”)
    #$form.Icon = $formicon

    $Okbutton = New-Object System.Windows.Forms.Button
    $Okbutton.Location = New-Object System.Drawing.Size(140,80)
    $Okbutton.Size = New-Object System.Drawing.Size(120,30)
    $Okbutton.Text = “BLOCK”
    $Okbutton.Add_Click({networkshell})
    $Form.Controls.Add($Okbutton)

    $Form.ShowDialog()

  15. Pingback: Form button to run the rest of my script - How to Code .NET

Leave a comment