2

Is it possible using the System.Windows.Forms.TextBox (or any other method) to enter in text that can then be converted to securestring? I would like to be able to take a value entered, convert it to securestring and write that to a file which could then be called on if needed and the securestring converted back should we need to identify the value.

I've looked into something similar to this, but since I am using the TextBox forms, I don't want to rely on Read-Host

$secstr = Read-Host -AsSecureString "Enter your text"

$secstr | ConvertFrom-SecureString | out-file C:\temp\test.txt

$secstr = get-content c:\temp\test.txt | ConvertTo-SecureString -AsPlaintText -Force

Essentially, I want to have a text box use masked/password characters (which I can do with $TextBox.PasswordChar = "*" and then take that input and dump it into a securestring text file. Then, I could use another script to call on that file and display the text in plain text for the end user should they need to know that current value.

3 Answers 3

6

Use a MaskedTextBox instead of a regular TextBox if you want to embed this in a custom GUI:

Add-Type -Assembly 'System.Windows.Forms'

$form = New-Object Windows.Forms.Form

$password = New-Object Windows.Forms.MaskedTextBox
$password.PasswordChar = '*'
$password.Top  = 100
$password.Left = 80

$form.Controls.Add($password)

$form.ShowDialog()

[source]

and then convert the text to a secure string:

$secstr = $password.Text | ConvertTo-SecureString -AsPlaintText -Force

If you just want to prompt for credentials you could use Get-Credential, which already stores the entered password as a secure string:

PS C:\> $cred = Get-Credential

Cmdlet Get-Credential an der Befehlspipelineposition 1
Geben Sie Werte für die folgenden Parameter an:
Credential
PS C:\> $cred.Password
System.Security.SecureString
Sign up to request clarification or add additional context in comments.

Comments

2

An additional way is to use a plain TextBox and as Ansgar mentioned use the PasswordChar attribute:

$textbox = New-Object System.Windows.Forms.Textbox
$textbox.Size = '75,23'
$textbox.PasswordChar = '*'
$form.Controls.Add($textbox)

1 Comment

This is better than the accepted answer, because there is no need for the MaskedTextBox in this case. In terms of a text box, the MaskedTextBox provides a way for custom input validation logic and has nothing to do with masking (obscuring) values
0

You need to create a Form and then add the password input box This is how I would do

Add-Type -Assembly Name System. Drawing
Add-Type -Assembly Name Presentation Framework
Add-Type -Assembly Name System.Windows.Forms

$Form = New-Object Windows.Forms.Form
$Form.Text = 'Password'
$Form.Size = New-Object System.Drawing.Size(300,230) 
$Form.StartPosition = 'CenterScreen'

Following will create OK button on the form

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

Do the same for Cancel button if you need it.

Create a Label before Text box as follows

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10, 40)
$label.size = New-Object System.Drawing.Size(120,20)
$label.Text = 'Password' 
$form.Controls.Add($label)

Following will Create the Masked Text box for password entry.

$Textbox = New-Object System.Windows.Forms.MaskedTextBox
$Textbox.Location = New-Object System.Drawing.Point(10,40)
$Textbox.PasswordChar = "*"
$Form.Controls.Add($Textbox)
$Form.ShowDialog()

1 Comment

Thank you Dennis for making the code presentable. I was struggling with that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.