natalie.solero
Goto Top

Powershell GUI creating local users

Hi guys,

im trying to create a gui for creating local user accounts. the gui is just running fine but i have a problem creating the account. i need to convert the password to a securestring. whats the code for that and where i have to put it in?

$PSDefaultParameterValues['*:Encoding'] = 'ascii'  
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

#####################################################################################################################################################

#create form
$form             = New-Object System.Windows.Forms.Form
$form.Width       = 500
$form.Height      = 400
$form.MaximizeBox = $false
$form.TopMost     = $true

#####################################################################################################################################################

$objLabel = New-Object System.Windows.Forms.label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(130,15)
$objLabel.BackColor = "Transparent"  
$objLabel.ForeColor = "Black"  
$objLabel.Text = "Username"  
$Form.Controls.Add($objLabel)

#textbox with choosen user name
$txtBox          = New-Object System.Windows.Forms.TextBox
$txtBox.Location = New-Object System.Drawing.Point (180, 20)
$txtBox.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox)

#####################################################################################################################################################

$objLabel2 = New-Object System.Windows.Forms.label
$objLabel2.Location = New-Object System.Drawing.Size(10,50)
$objLabel2.Size = New-Object System.Drawing.Size(130,15)
$objLabel2.BackColor = "Transparent"  
$objLabel2.ForeColor = "Black"  
$objLabel2.Text = "Password"  
$Form.Controls.Add($objLabel2)

#textbox with choosen password 
$txtBox2          = New-Object Windows.Forms.MaskedTextBox
$txtBox2.PasswordChar = '*'    
$txtBox2.Location = New-Object System.Drawing.Point (180, 50)
$txtBox2.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox2)


#####################################################################################################################################################

#create checkbox1
$checkBox          = New-Object System.Windows.Forms.CheckBox
$checkBox.Location = New-Object System.Drawing.Point (10, 100)
$checkBox.Size     = New-Object System.Drawing.Size(350,30)
$checkBox.Text     = "PasswordNeverExpires"  
$form.Controls.Add($checkBox)


#create checkbox2
$checkBox2          = New-Object System.Windows.Forms.CheckBox
$checkBox2.Location = New-Object System.Drawing.Point (10, 150)
$checkBox2.Size     = New-Object System.Drawing.Size(350,30)
$checkBox2.Text     = "UserMayChangePassword"  
$form.Controls.Add($checkBox2)

#create checkbox2
$checkBox3          = New-Object System.Windows.Forms.CheckBox
$checkBox3.Location = New-Object System.Drawing.Point (10, 200)
$checkBox3.Size     = New-Object System.Drawing.Size(350,30)
$checkBox3.Text     = "AccountNeverExpires"  
$form.Controls.Add($checkBox3)




#create user button
$Button          = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,250)
$Button.Size     = New-Object System.Drawing.Size(150,50)
$Button.Text     = "create user"  
$Button.Add_Click({
if(($checkBox.Checked -eq $false) -and ($checkBox2.Checked -eq $false) -and ($checkBox3.Checked -eq $false)) {
    [System.Windows.Forms.Messagebox]::Show("No CheckBox checked")  
}    
                 
#checkbox1 action
if ($checkBox.Checked -eq $true) {
                      $adminName          = $txtbox.text 
                      $securePassword     = $textbox2.Text 
                      $newUser = New-LocalUser -Name $adminName -Password $securePassword -Description $adminName -FullName $adminName #-ErrorAction Stop
                      $newUser | Set-LocalUser -PasswordNeverExpires $true -UserMayChangePassword $false -AccountNeverExpires #-ErrorAction Stop
                      Add-LocalGroupMember -Group "Administrators" -Member $adminName #-ErrorAction Stop  




        if(-not $?) {[System.Windows.Forms.MessageBox]::Show( "no success",'','OK',"Error")}  
    else {[System.Windows.Forms.MessageBox]::Show( "success",'','OK',"Information")}   
                                 }

#checkbox2 action
if ($checkBox2.Checked -eq $true) {
        if(-not $?) {[System.Windows.Forms.MessageBox]::Show( "no success",'','OK',"Error")}  
    else {[System.Windows.Forms.MessageBox]::Show( "success",'','OK',"Information")}                                     
                                }  

#checkbox3 action
if ($checkBox3.Checked -eq $true) {
        if(-not $?) {[System.Windows.Forms.MessageBox]::Show( "no success",'','OK',"Error")}  
    else {[System.Windows.Forms.MessageBox]::Show( "success",'','OK',"Information")}                                     
                                }  


})
$form.Controls.Add($Button)


#end
[void]$form.ShowDialog()

Content-ID: 1770879202

Url: https://administrator.de/contentid/1770879202

Printed on: October 11, 2024 at 14:10 o'clock

MrCount
MrCount Jan 28, 2022, updated at May 16, 2023 at 12:51:10 (UTC)
Goto Top
Hi,

you can use ConvertTo-SecureString

$Secure_String_Pwd = ConvertTo-SecureString "P@ssW0rD!" -AsPlainText -Force  
natalie.solero
natalie.solero Jan 28, 2022, updated at May 16, 2023 at 12:51:10 (UTC)
Goto Top
@MrCount i know but i down't know where to add
$securePassword     = $textbox2.Text  | ConvertTo-SecureString -AsPlainText -Force
doesn't work
colinardo
colinardo Jan 28, 2022 updated at 10:22:40 (UTC)
Goto Top
Hi @natalie.solero,
there are multiple errors in your script, beginning from misspelled variables ($textbox2 does not exist) and other flaws, for example that you are not forcing the session to elevation, which is mandatory for creating local admin accounts.
This is a correction and should do what you expect from it.
I would strongly advise you to change your variable names to "speaking" one's instead, in the current form they do not show what they are intended for, this eliminates such dumb errors, and additionally saves our time face-wink.
$ErrorActionPreference = "Stop"  
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# restart elevated if needed
if(!(new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole(544)){
    start powershell -Verb runas -ArgumentList '-File',$MyInvocation.MyCommand.Definition  
    exit
}

#####################################################################################################################################################

#create form
$form             = New-Object System.Windows.Forms.Form
$form.Width       = 500
$form.Height      = 400
$form.MaximizeBox = $false
$form.TopMost     = $true

#####################################################################################################################################################

$objLabel = New-Object System.Windows.Forms.label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(130,15)
$objLabel.BackColor = "Transparent"  
$objLabel.ForeColor = "Black"  
$objLabel.Text = "Username"  
$Form.Controls.Add($objLabel)

#textbox with choosen user name
$txtBox          = New-Object System.Windows.Forms.TextBox
$txtBox.Location = New-Object System.Drawing.Point (180, 20)
$txtBox.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox)

#####################################################################################################################################################

$objLabel2 = New-Object System.Windows.Forms.label
$objLabel2.Location = New-Object System.Drawing.Size(10,50)
$objLabel2.Size = New-Object System.Drawing.Size(130,15)
$objLabel2.BackColor = "Transparent"  
$objLabel2.ForeColor = "Black"  
$objLabel2.Text = "Password"  
$Form.Controls.Add($objLabel2)

#textbox with choosen password 
$txtBox2          = New-Object Windows.Forms.MaskedTextBox
$txtBox2.PasswordChar = '*'    
$txtBox2.Location = New-Object System.Drawing.Point (180, 50)
$txtBox2.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox2)


#####################################################################################################################################################

#create checkbox1
$checkBox          = New-Object System.Windows.Forms.CheckBox
$checkBox.Location = New-Object System.Drawing.Point (10, 100)
$checkBox.Size     = New-Object System.Drawing.Size(350,30)
$checkBox.Text     = "PasswordNeverExpires"  
$form.Controls.Add($checkBox)


#create checkbox2
$checkBox2          = New-Object System.Windows.Forms.CheckBox
$checkBox2.Location = New-Object System.Drawing.Point (10, 150)
$checkBox2.Size     = New-Object System.Drawing.Size(350,30)
$checkBox2.Text     = "UserMayChangePassword"  
$form.Controls.Add($checkBox2)

#create checkbox2
$checkBox3          = New-Object System.Windows.Forms.CheckBox
$checkBox3.Location = New-Object System.Drawing.Point (10, 200)
$checkBox3.Size     = New-Object System.Drawing.Size(350,30)
$checkBox3.Text     = "AccountNeverExpires"  
$form.Controls.Add($checkBox3)

#create user button
$Button          = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,250)
$Button.Size     = New-Object System.Drawing.Size(150,50)
$Button.Text     = "create user"  
$Button.Add_Click({
    try{
        # define options to create user
        $useroptions = @{
            Name = $txtbox.Text
            Password = ConvertTo-SecureString $txtbox2.Text -AsPlainText -Force
            Description = $txtbox.Text
            Fullname = $txtbox.Text
            PasswordNeverExpires = $checkbox.Checked
            AccountNeverExpires = $checkbox3.Checked
            UserMayNotChangePassword = !$checkbox2.Checked
        }
        # create user and assign to administrators group
        New-LocalUser @useroptions | Add-LocalGroupMember -Group (Get-Localgroup | ? Sid -eq 'S-1-5-32-544')  
        [System.Windows.Forms.MessageBox]::Show("User has been created successfully.","User created",0,64)  
    }catch{
        [System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)  
    }

})
$form.Controls.Add($Button)

#end
[void]$form.ShowDialog()

Best regards
Uwe
natalie.solero
natalie.solero Jan 28, 2022 at 10:22:21 (UTC)
Goto Top
Hi @colinardo
thank you very much for your reply and for your suggestions. i will definitive will look into it. at the moment i just copy and paste your script but unfortunately the script immediately ends up with an error i can't see because the windows closes
colinardo
colinardo Jan 28, 2022 updated at 10:34:42 (UTC)
Goto Top
I tested it, and runs without problems.
but unfortunately the script immediately ends up with an error i can't see because the windows closes
To see the error, start an empty powershell console and start the script from there, then you can read the error. Alternatively start it like that from a batch cmd
powershell -EP Bypass -NoExit -File "C:\script.ps1"  
This also forces the console to stay open. This command also suppresses the executionpolicy which is assigned to the machine. So if you haven't tweaked the executionpolicy it is time to do so now in an elevated console with:
Set-Executionpolicy RemoteSigned

Please note, the account wich runs the script needs to have admin rights on the machine, otherwise this will not work!

screenshot

at the moment i just copy and paste
Better learn the basics from the link library you can finde here
Powershell Leitfaden für Anfänger
natalie.solero
natalie.solero Jan 28, 2022 updated at 11:36:16 (UTC)
Goto Top
Now it works . Thank you so much for that @colinardo
is it possible you can help me with another wish? i just modified your script for creating normal and admin accounts. that works fine.
i want to add a further checkbox with "no password" for creating normal user accounts without password.

why are your corners in the gui and messages round? is it win11 already?

this is what i have
$ErrorActionPreference = "Stop"  
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# restart elevated if needed
if(!(new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole(544)){
    start powershell -Verb runas -ArgumentList '-File',$MyInvocation.MyCommand.Definition  
    exit
}

#####################################################################################################################################################

#create form
$form             = New-Object System.Windows.Forms.Form
$form.Width       = 500
$form.Height      = 600
$form.MaximizeBox = $false
$form.TopMost     = $true

#####################################################################################################################################################

$objLabel = New-Object System.Windows.Forms.label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(130,15)
$objLabel.BackColor = "Transparent"  
$objLabel.ForeColor = "Black"  
$objLabel.Text = "Username"  
$Form.Controls.Add($objLabel)

#textbox with choosen user name
$txtBox          = New-Object System.Windows.Forms.TextBox
$txtBox.Location = New-Object System.Drawing.Point (180, 20)
$txtBox.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox)

#####################################################################################################################################################

$objLabel2 = New-Object System.Windows.Forms.label
$objLabel2.Location = New-Object System.Drawing.Size(10,50)
$objLabel2.Size = New-Object System.Drawing.Size(130,15)
$objLabel2.BackColor = "Transparent"  
$objLabel2.ForeColor = "Black"  
$objLabel2.Text = "Password"  
$Form.Controls.Add($objLabel2)

#textbox with choosen password 
$txtBox2          = New-Object Windows.Forms.MaskedTextBox
$txtBox2.PasswordChar = '*'    
$txtBox2.Location = New-Object System.Drawing.Point (180, 50)
$txtBox2.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox2)


#####################################################################################################################################################

#create checkbox1
$checkBox          = New-Object System.Windows.Forms.CheckBox
$checkBox.Location = New-Object System.Drawing.Point (10, 100)
$checkBox.Size     = New-Object System.Drawing.Size(350,30)
$checkBox.Text     = "PasswordNeverExpires"  
$form.Controls.Add($checkBox)


#create checkbox2
$checkBox2          = New-Object System.Windows.Forms.CheckBox
$checkBox2.Location = New-Object System.Drawing.Point (10, 150)
$checkBox2.Size     = New-Object System.Drawing.Size(350,30)
$checkBox2.Text     = "UserMayChangePassword"  
$form.Controls.Add($checkBox2)

#create checkbox3
$checkBox3          = New-Object System.Windows.Forms.CheckBox
$checkBox3.Location = New-Object System.Drawing.Point (10, 200)
$checkBox3.Size     = New-Object System.Drawing.Size(350,30)
$checkBox3.Text     = "AccountNeverExpires"  
$form.Controls.Add($checkBox3)

#create checkbox4
$checkBox4          = New-Object System.Windows.Forms.CheckBox
$checkBox4.Location = New-Object System.Drawing.Point (10, 250)
$checkBox4.Size     = New-Object System.Drawing.Size(350,30)
$checkBox4.Text     = "AdminAccount"  
$form.Controls.Add($checkBox4)

#create user button
$Button          = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,300)
$Button.Size     = New-Object System.Drawing.Size(150,50)
$Button.Text     = "create user"  
$Button.Add_Click({
if ($checkbox4.checked) {
    try{
        # define options to create user
        $useroptions = @{
            Name = $txtbox.Text
            Password = ConvertTo-SecureString $txtbox2.Text -AsPlainText -Force
            Description = $txtbox.Text
            Fullname = $txtbox.Text
            PasswordNeverExpires = $checkbox.Checked
            AccountNeverExpires = $checkbox3.Checked
            UserMayNotChangePassword = !$checkbox2.Checked
        }
        # create user and assign to administrators group
        New-LocalUser @useroptions | Add-LocalGroupMember -Group (Get-Localgroup | ? Sid -eq 'S-1-5-32-544')  
        [System.Windows.Forms.MessageBox]::Show("User has been created successfully.","User created",0,64)  
    }catch{
        [System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)  
    }
}else {
try{
        # define options to create user
        $useroptions = @{
            Name = $txtbox.Text
            Password = ConvertTo-SecureString $txtbox2.Text -AsPlainText -Force
            Description = $txtbox.Text
            Fullname = $txtbox.Text
            PasswordNeverExpires = $checkbox.Checked
            AccountNeverExpires = $checkbox3.Checked
            UserMayNotChangePassword = !$checkbox2.Checked
        }
        # create user and assign to users group
        New-LocalUser @useroptions | Add-LocalGroupMember -Group (Get-Localgroup | ? name -eq 'users')  
        [System.Windows.Forms.MessageBox]::Show("User has been created successfully.","User created",0,64)  
    }catch{
        [System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)  
    }

}
})
$form.Controls.Add($Button)

#end
[void]$form.ShowDialog()
natalie.solero
natalie.solero Jan 28, 2022 updated at 12:31:51 (UTC)
Goto Top
@colinardo maybe I didn't express it correctly. i just have added a checkbox like in the picture. if i mark the checkbox "noPassword" i want to create a local user without a password. i don't want to enter a password in the password textbox
1
colinardo
colinardo Jan 28, 2022, updated at Jan 29, 2022 at 13:42:23 (UTC)
Goto Top
i just have added a checkbox like in the picture.
Your posted code above did not have such a checkbox so i did not implement it :-P, thats your homework for today.

$ErrorActionPreference = "Stop"  
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# restart elevated if needed
if(!(new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole(544)){
    start powershell -Verb runas -ArgumentList '-File',$MyInvocation.MyCommand.Definition  
    exit
}

#####################################################################################################################################################

#create form
$form             = New-Object System.Windows.Forms.Form
$form.Width       = 500
$form.Height      = 600
$form.MaximizeBox = $false
$form.TopMost     = $true

#####################################################################################################################################################

$objLabel = New-Object System.Windows.Forms.label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(130,15)
$objLabel.BackColor = "Transparent"  
$objLabel.ForeColor = "Black"  
$objLabel.Text = "Username"  
$Form.Controls.Add($objLabel)

#textbox with choosen user name
$txtBox          = New-Object System.Windows.Forms.TextBox
$txtBox.Location = New-Object System.Drawing.Point (180, 20)
$txtBox.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox)

#####################################################################################################################################################

$objLabel2 = New-Object System.Windows.Forms.label
$objLabel2.Location = New-Object System.Drawing.Size(10,50)
$objLabel2.Size = New-Object System.Drawing.Size(130,15)
$objLabel2.BackColor = "Transparent"  
$objLabel2.ForeColor = "Black"  
$objLabel2.Text = "Password"  
$Form.Controls.Add($objLabel2)

#textbox with choosen password 
$txtBox2          = New-Object Windows.Forms.MaskedTextBox
$txtBox2.PasswordChar = '*'    
$txtBox2.Location = New-Object System.Drawing.Point (180, 50)
$txtBox2.Size     = New-Object System.Drawing.Size(280,100)
$form.Controls.Add($txtBox2)


#####################################################################################################################################################

#create checkbox1
$checkBox          = New-Object System.Windows.Forms.CheckBox
$checkBox.Location = New-Object System.Drawing.Point (10, 100)
$checkBox.Size     = New-Object System.Drawing.Size(350,30)
$checkBox.Text     = "PasswordNeverExpires"  
$form.Controls.Add($checkBox)


#create checkbox2
$checkBox2          = New-Object System.Windows.Forms.CheckBox
$checkBox2.Location = New-Object System.Drawing.Point (10, 150)
$checkBox2.Size     = New-Object System.Drawing.Size(350,30)
$checkBox2.Text     = "UserMayChangePassword"  
$form.Controls.Add($checkBox2)

#create checkbox3
$checkBox3          = New-Object System.Windows.Forms.CheckBox
$checkBox3.Location = New-Object System.Drawing.Point (10, 200)
$checkBox3.Size     = New-Object System.Drawing.Size(350,30)
$checkBox3.Text     = "AccountNeverExpires"  
$form.Controls.Add($checkBox3)

#create checkbox4
$checkBox4          = New-Object System.Windows.Forms.CheckBox
$checkBox4.Location = New-Object System.Drawing.Point (10, 250)
$checkBox4.Size     = New-Object System.Drawing.Size(350,30)
$checkBox4.Text     = "AdminAccount"  
$form.Controls.Add($checkBox4)

#create checkbox5
$checkBox5          = New-Object System.Windows.Forms.CheckBox
$checkBox5.Location = New-Object System.Drawing.Point (10, 300)
$checkBox5.Size     = New-Object System.Drawing.Size(350,30)
$checkBox5.Text     = "noPassword"  
$checkbox5.Add_Click({
	# disable/enable other controls depending on state of current checkbox
        $checkBox.Enabled = !$checkBox5.Checked
        $txtBox2.Enabled = !$checkBox5.Checked
        $checkbox4.Enabled = !$checkBox5.Checked
})

$form.Controls.Add($checkBox5)

#create user button
$Button          = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,350)
$Button.Size     = New-Object System.Drawing.Size(150,50)
$Button.Text     = "create user"  
$Button.Add_Click({
    # Admin or Users Group
    $group = @{$true='S-1-5-32-544';$false='S-1-5-32-545'}[$checkbox4.checked]  
    try{
        # define options to create user
        $useroptions = @{
            Name = $txtbox.Text
            Description = $txtbox.Text
            Fullname = $txtbox.Text
            AccountNeverExpires = $checkbox3.Checked
            UserMayNotChangePassword = !$checkbox2.Checked
        }
	# if the "noPassword" checkbox is not checked  
        if (!$checkbox5.Checked){
            $useroptions.Password = ConvertTo-SecureString $txtbox2.Text -AsPlainText -Force
            $useroptions.PasswordNeverExpires = $checkbox.Checked
        }else{
	    # "noPassword" checkbox is checked  
            $useroptions.NoPassword = $true
            $group = 'S-1-5-32-545'  
        }
        # 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)  
    }catch{
        [System.Windows.Forms.MessageBox]::Show("Error creating new user account:`n $($_.Exception.Message)","Exception",0,48)  
    }


})
$form.Controls.Add($Button)

#end
[void]$form.ShowDialog()
MrCount
MrCount Jan 28, 2022 at 15:26:04 (UTC)
Goto Top
However, it would make sense if the "AdminAccount" and "noPassword" checkboxes were mutually exclusive....
natalie.solero
natalie.solero Feb 15, 2022 at 06:18:27 (UTC)
Goto Top
Hey guys, sorry for the late reply. i wasn't good the last days.
@colinardo
i have changed line 92 to
$checkBox.Enabled = $checkBox5.Checked
and added
#create checkbox6
$checkBox6          = New-Object System.Windows.Forms.CheckBox
$checkBox6.Location = New-Object System.Drawing.Point (400, 100)
$checkBox6.Size     = New-Object System.Drawing.Size(350,30)
$checkBox6.Text     = "don't change password"  
$form.Controls.Add($checkBox6)
because i want that the user isn't able to change the password after login and the account never expires like in the picture below
what do i need to add?
1
2