# return value from GUI form

**URL:** <https://forums.powershell.org/t/return-value-from-gui-form/15517>\
**Category:** PowerShell Help\
**Created:** [January 21, 2021, 10:28pm UTC](https://forums.powershell.org/t/return-value-from-gui-form/15517 "2021-01-21T22:28:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wim](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wim/32/1168_2.png) [@wim](https://forums.powershell.org/u/wim)\
**Post date:** [January 21, 2021, 10:28pm UTC](https://forums.powershell.org/t/return-value-from-gui-form/15517/1 "2021-01-21T22:28:52Z")

</div>

I’m creating a module for user-management (dutch: “gebruikersbeheer”). Part that is “ready” can you find on [https://github.com/wimternet/Gebruikersbeheer](https://github.com/wimternet/Gebruikersbeheer)

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 (https://github.com/wimternet)
    .Synopsis
		Formulier om een nieuw wachtwoord in te stellen
	.EXAMPLE
		Formulier aanroepen

		New-GBFormPassword
#>
function New-GBFormPassword
{
    Begin
    {
        Write-Verbose -Message 'Begin called New-GBFormPassword'

        # Formulier mogelijk maken
        Add-Type -AssemblyName System.Windows.Forms
        [System.Windows.Forms.Application]::EnableVisualStyles()
    }
    Process
    {
        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
        $frmFormPassword.controls.AddRange(@($lblUser,$btnSave,$btnCancel))

        # Events
        $btnSave.Add_Click({
            New-GBPassword -Lowercase 5 -Secure $false | Out-Default
            $frmFormPassword.Close()
        })

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

        # Show form
        $frmFormPassword.ShowDialog()
    }
    End
    {
        Write-Verbose -Message 'End called New-GBFormPassword'
    }
}
```

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [January 22, 2021, 3:14am UTC](https://forums.powershell.org/t/return-value-from-gui-form/15517/2 "2021-01-22T03:14:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wim](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wim/32/1168_2.png) [@wim](https://forums.powershell.org/u/wim)\
**Post date:** [January 23, 2021, 3:42am UTC](https://forums.powershell.org/t/return-value-from-gui-form/15517/3 "2021-01-23T03:42:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [January 24, 2021, 2:02am UTC](https://forums.powershell.org/t/return-value-from-gui-form/15517/4 "2021-01-24T02:02:46Z")

</div>

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.

[https://mikefrobbins.com/2017/06/08/what-is-this-module-scope-in-powershell-that-you-speak-of/](https://mikefrobbins.com/2017/06/08/what-is-this-module-scope-in-powershell-that-you-speak-of/)

&nbsp;

---

<div class="post-metadata">

**Author:** ![wim](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wim/32/1168_2.png) [@wim](https://forums.powershell.org/u/wim)\
**Post date:** [January 24, 2021, 8:05am UTC](https://forums.powershell.org/t/return-value-from-gui-form/15517/5 "2021-01-24T08:05:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/return-value-from-gui-form/15517/6 "2024-05-16T20:27:57Z")

</div>

Closed on May 16, 2024, 8:27pm UTC


