return value from GUI form

I’m creating a module for user-management (dutch: “gebruikersbeheer”). Part that is “ready” can you find on

Now I’m trying to create a GUI-layer starting with a form for password creation. The default return value of the form is “ok” or “cancel”. How can I change it to the password that is created (string, securestring)? Or do I need to use a file for passing it to the main script/other function?

Form-function untill now:

	Written by wimternet (
		Formulier om een nieuw wachtwoord in te stellen
		Formulier aanroepen

function New-GBFormPassword
        Write-Verbose -Message 'Begin called New-GBFormPassword'

        # Formulier mogelijk maken
        Add-Type -AssemblyName System.Windows.Forms
        Write-Verbose -Message 'Process called New-GBFormPassword'

        # Form
        $frmFormPassword                     = New-Object system.Windows.Forms.Form
        $frmFormPassword.ClientSize          = '700,400'
        $frmFormPassword.text                = "Gebruikersbeheer - Wachtwoord"
        $frmFormPassword.TopMost             = $false

        # Label
        $lblUser                         = New-Object system.Windows.Forms.Label
        $lblUser.text                    = "Welkom $env:USERNAME"
        $lblUser.AutoSize                = $true
        $lblUser.width                   = 25
        $lblUser.height                  = 10
        $lblUser.location                = New-Object System.Drawing.Point(30,20)
        $lblUser.Font                    = 'Microsoft Sans Serif,10'

        # Button
        $btnSave                         = New-Object system.Windows.Forms.Button
        $btnSave.text                    = "Opslaan"
        $btnSave.width                   = 65
        $btnSave.height                  = 30
        $btnSave.location                = New-Object System.Drawing.Point(600,345)
        $btnSave.Font                    = 'Microsoft Sans Serif,10'

        $btnCancel                       = New-Object system.Windows.Forms.Button
        $btnCancel.text                  = "Annuleren"
        $btnCancel.width                 = 80
        $btnCancel.height                = 30
        $btnCancel.location              = New-Object System.Drawing.Point(500,345)
        $btnCancel.Font                  = 'Microsoft Sans Serif,10'

        # Add controls to form

        # Events
            New-GBPassword -Lowercase 5 -Secure $false | Out-Default

        # Assign the Accept and Cancel options in the form to the corresponding buttons
        $frmFormPassword.CancelButton = $btnCancel

        # Show form
        Write-Verbose -Message 'End called New-GBFormPassword'

What control are you using for the user to type in the password? You can use a text box and set the UseSystemPasswordChar to $true. Once you have the text box in place you can access the text property of that object in your click method of $btnsave object.

The GUI will be used by an admin. The admin doesn’t type the password, but the requirements for it. Afterwards line 58 (New-GBPassword) will generate a new password. For now I’ve typed the number of lowercase and fill in the secure-option. The new generated password(s) need to return to the previous function/script.

My problem isn’t from textbox to button, but from button to the function/script that called the GUI.

To ensure I’ve understood:

You have a module and the module contains (at least) 3 functions:

  1. A function that calls the GUI function
  2. A GUI function that takes some user input and calls a function to generate a password
  3. A password generating function that creates a password based on the input in step two and passes the password back to the function in step 1.
I think the correct way to do this would be to use a variable with Script scope. In a module, a variable with Script scope is accessible to all functions in the module.

In New-GBPassword you can assign your password to to $Script:password and the variable $password will be accessible in the function that called the GUI.


Indeed, there are at least “3” functions:

  1. "New-GBPassword -Form $true" or "New-GBFormPassword"
  2. "New-GBFormPassword" itself
  3. "New-GBPassword -Lowercase 5 -Uppercase 1 -Number 1 -Special 2"
Thanks for the solution. I'll change the code to use a variable with script scope.