OK, I think I got it now, would you do something else with the form?
function Load-Form {
$Form.Controls.Add($GBAccountProperties)
$Form.Controls.Add($TBUserName)
$Form.Controls.Add($GBUserName)
$Form.Controls.Add($TBPassword)
$Form.Controls.Add($TBPassword2)
$Form.Controls.Add($GBPassword)
$Form.Controls.Add($Button)
$Form.Controls.Add($Label)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
}
$Button.add_click({
if (-not ($TBPassword.Text -ceq $TBPassword2.Text)) {
[System.Windows.Forms.MessageBox]::Show("Passwords didn’t match. Please try again.")
}
else{
$Button.DialogResult = [System.Windows.Forms.DialogResult]::OK
}
})
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(540, 240)
$Form.StartPosition = "CenterScreen"
$Form.SizeGripStyle = "Hide"
$Form.Text = "Enter Local Account"
$Form.ControlBox = $false
$Form.TopMost = $true
# TextBox - UserName
$TBUserName = New-Object System.Windows.Forms.TextBox
$TBUserName.Location = New-Object System.Drawing.Size(25,30)
$TBUserName.Size = New-Object System.Drawing.Size(215,50)
$TBUserName.TabIndex = "1"
# TextBox - Password
$TBPassword = New-Object System.Windows.Forms.TextBox
$TBPassword.Location = New-Object System.Drawing.Size(25,90)
$TBPassword.Size = New-Object System.Drawing.Size(215,50)
$TBPassword.TabIndex = "2"
$TBPassword.PasswordChar = '*'
$TBPassword2 = New-Object System.Windows.Forms.TextBox
$TBPassword2.Location = New-Object System.Drawing.Size(25, 120)
$TBPassword2.Size = New-Object System.Drawing.Size(215,50)
$TBPassword2.TabIndex = "2"
$TBPassword2.PasswordChar = '*'
# GroupBox - Username
$GBUserName = New-Object System.Windows.Forms.GroupBox
$GBUserName.Location = New-Object System.Drawing.Size(20,10)
$GBUserName.Size = New-Object System.Drawing.Size(225,50)
$GBUserName.Font = [System.Drawing.Font]::new("Corbel", 9, [System.Drawing.FontStyle]::Regular)
$GBUserName.Text = "User Name:"
# GroupBox - Password
$GBPassword = New-Object System.Windows.Forms.GroupBox
$GBPassword.Location = New-Object System.Drawing.Size(20,70)
$GBPassword.Size = New-Object System.Drawing.Size(225,80)
$GBPassword.Font = [System.Drawing.Font]::new("Corbel", 9, [System.Drawing.FontStyle]::Regular)
$GBPassword.Text = "Password:"
# GroupBox - Account Properties
$GBAccountProperties = New-Object System.Windows.Forms.GroupBox
$GBAccountProperties.Location = New-Object System.Drawing.Size(253,10)
$GBAccountProperties.Size = New-Object System.Drawing.Size(250,150)
$GBAccountProperties.Name = 'groupbox3'
$GBAccountProperties.TabIndex = 3
$GBAccountProperties.TabStop = $False
$GBAccountProperties.Text = 'Account Properties'
# CheckBox1 - Account Never Expires
$checkBox1 = New-Object System.Windows.Forms.CheckBox
$checkBox1.Location = New-Object System.Drawing.Size(262,27)
$checkBox1.Size = New-Object System.Drawing.Size(185, 24)
$checkBox1.Name = 'checkBox1'
$checkBox1.TabIndex = 0
$checkBox1.Text = 'Account Never Expires'
$checkBox1.UseVisualStyleBackColor = $True
$form.Controls.Add($checkBox1)
# CheckBox2 - AdminAccount
$checkBox2 = New-Object System.Windows.Forms.CheckBox
$checkBox2.Location = New-Object System.Drawing.Size (262,54)
$checkBox2.Size = New-Object System.Drawing.Size(185,24)
$checkBox2.Text = "AdminAccount"
$checkBox2.Name = 'checkBox2'
$checkBox2.TabIndex = 0
$checkBox2.UseVisualStyleBackColor = $True
$form.Controls.Add($checkBox2)
# Checkbox3 - Password Never Expires
$checkBox3 = New-Object System.Windows.Forms.CheckBox
$checkBox3.Location = New-Object System.Drawing.Size(262, 79)
$checkBox3.Size = New-Object System.Drawing.Size(185, 24)
$checkBox3.Name = 'checkBox3'
$checkBox3.TabIndex = 0
$checkBox3.UseVisualStyleBackColor = $True
$checkBox3.Text = 'Password Never Expires'
$form.Controls.Add($checkBox3)
# Checkbox4 - You Can Change the Password
$checkBox4 = New-Object System.Windows.Forms.CheckBox
$checkBox4.Location = New-Object System.Drawing.Size(262, 104)
$checkBox4.Size = New-Object System.Drawing.Size(185, 24)
$checkBox4.Name = 'checkBox4'
$checkBox4.TabIndex = 0
$checkBox4.UseVisualStyleBackColor = $True
$checkBox4.Text = 'You Can Change the Password'
$checkBox4.UseVisualStyleBackColor = $True
$form.Controls.Add($checkBox4)
# Checkbox5 - No Password
$checkBox5 = New-Object System.Windows.Forms.CheckBox
$checkBox5.Location = New-Object System.Drawing.Size(262, 129)
$checkBox5.Size = New-Object System.Drawing.Size(185, 24)
$checkBox5.Name = 'checkBox5'
$checkBox5.TabIndex = 0
$checkBox5.Text = 'no Password'
$checkBox5.UseVisualStyleBackColor = $True
$form.Controls.Add($checkBox5)
$checkbox5.Add_Click({
# Disable/Enable Other Controls Depending On State Of Current Checkbox
$checkBox1.Enabled = !$checkBox5.Checked
$TBPassword.Enabled = !$checkBox5.Checked
$checkbox2.Enabled = !$checkBox5.Checked
})
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(13, 165)
$Button.Size = New-Object System.Drawing.Size(492, 28)
$Button.Name = 'Button'
$Button.TabIndex = 4
$Button.Text = 'Create Account'
$Button.UseVisualStyleBackColor = $True
$Button.Add_Click({
# Admin or Users Group
$group = @{$true='S-1-5-32-544';$false='S-1-5-32-545'}[$checkbox2.checked]
try{
# Define Options To Create User
$useroptions = @{
Name = $TBUserName.Text
Description = $TBUserName.Text
Fullname = $TBUserName.Text
AccountNeverExpires = $checkbox1.Checked
UserMayNotChangePassword = !$checkbox4.Checked
}
# If The "noPassword" Checkbox Is Not Checked
if (!$checkbox5.Checked){
$useroptions.Password = ConvertTo-SecureString $TBPassword.Text -AsPlainText -Force
$useroptions.PasswordNeverExpires = $checkbox1.Checked
}
else
{
# "noPassword" Checkbox Is Checked
$useroptions.NoPassword = $true
$group = 'S-1-5-32-545'
}
# Check So The Password Match
if (-not ($TBPassword.Text -ceq $TBPassword2.Text)) {
[System.Windows.Forms.MessageBox]::Show("Passwords didn’t match. Please try again.")
return
}
# Create User And Assign To Administrators Group
New-LocalUser @useroptions | Add-LocalGroupMember -Group (Get-Localgroup | ? Sid -eq $group)
[System.Windows.Forms.MessageBox]::Show("User has been created successfully.","User created",0,64)
$Form.Close()
}catch{
[System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)
return
}
})
Load-Form