Skip to content

Instantly share code, notes, and snippets.

@jfhbrook
Created October 6, 2019 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfhbrook/aa75cc45c7b9f0c79bfdffa2c83e025d to your computer and use it in GitHub Desktop.
Save jfhbrook/aa75cc45c7b9f0c79bfdffa2c83e025d to your computer and use it in GitHub Desktop.
password prompt in powershell on windows 10
# Simple and nasty password prompt
# Adapted from https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-6
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Credentials Time!'
$form.Size = New-Object System.Drawing.Size(240,130)
$form.StartPosition = 'CenterScreen'
$submit = New-Object System.Windows.Forms.Button
$submit.Location = New-Object System.Drawing.Point(35,60)
$submit.Size = New-Object System.Drawing.Size(75,23)
$submit.Text = 'mash go'
$submit.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $submit
$form.Controls.Add($submit)
$nvm = New-Object System.Windows.Forms.Button
$nvm.Location = New-Object System.Drawing.Point(120,60)
$nvm.Size = New-Object System.Drawing.Size(75,23)
$nvm.Text = 'nvm'
$nvm.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $nvm
$form.Controls.Add($nvm)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Password:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,30)
$textBox.Size = New-Object System.Drawing.Size(200,20)
$textBox.PasswordChar = '*'
$form.Controls.Add($textBox)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
$form.Activate()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$textBox.Text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment