Wie lege ich einen lokalen Benuterz mithilfe eines Powersehll Skript an?

Hierzu möchte ich ein Formular vwerdenden.

Folgendes habe ich erstellt:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'lokaler Benutzer'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$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)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Benutzername:'
$form.Controls.Add($label)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,70)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'vollständiger Name:'
$form.Controls.Add($label)
$Benutzername = New-Object System.Windows.Forms.TextBox
$Benutzername.Location = New-Object System.Drawing.Point(10,40)
$Benutzername.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($Benutzername)
$Name = New-Object System.Windows.Forms.TextBox
$Name.Location = New-Object System.Drawing.Point(10,90)
$Name.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($Name)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
   $x = $textBox.Text
   $x
}
New-LocalUser $Benutzername  -FullName $Name
(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
3 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Erzesel
1 year ago

Before you deal with the whole GUI-Schnickscnack, at least the basics should be sitting around creating a user profile

so the theory…

New-LocalUser -Name 'TestUser001' -NoPassword

… creates a new local use without a password… he also does. but it does not appear in the start menu. Unlike the Cmd bug: net user “TestUser001” /add Powershell does not automatically assign the user to a group, and since our new user does not belong to any group, our Start Menu/LoginScreen does not know how to handle it.

You can use the groups Get-LocalGroup view. Relevant are only administrators and users for you.

Add-LocalGroupMember -Group "Benutzer" -Member "TestUser001"

..and the new user appears in the Start menu/Register screen. Attention the groupsNames only work with a German Windows!!!! (how to make international is not important here first)

Now comes the next fall knit:

If you add the FullName, the parameter -nopasswort will be ignored and the new user must enter a password at the first login! You can handle this by simply creating the new user with an empty password.

New-LocalUser -Name "TestUser002" -FullName "Test Nutzer 2" -Password ([securestring]::new())
Add-LocalGroupMember -Group "Benutzer" -Member "TestUser002"

Töräääähäh….

Now we want to get rid of the two birds…

Remove-LocalUser -Name "TestUser001"
Remove-LocalUser -Name "TestUser002"

Do not forget the folders of users under C:\users\ to delete if you have logged in. otherwise Windows rebuilds its own folder names when the same username reappears

Remove-Item "c:\users\TestUser001" -Recurse -Force
Remove-Item "c:\users\TestUser002" -Recurse -Force

…to the core business.

Your GUI:

the whole New-Object System.Drawing…. Qatsch is Idiodie of people who have been writing/paste for 15 years… Powershell automatically recognizes the data type .Size/.Point … just assign the values as string

I also donate an elevator stick to work with Localuser you need admin rights

 #script selbständig als admin starten
if (!$([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544')){
  Start-Process powershell.exe -Verb RunAs -ArgumentList ('-executionpolicy bypass -file "{0}" -elevated' -f $pscommandpath)
  exit $LASTEXITCODE
}
#ab hier alles, was als Admin laufen soll...
'ich laufe als Admin'

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form
$form.Text = 'lokaler Benutzer'
$form.Size = '300,200'
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true

$label1 = New-Object System.Windows.Forms.Label
$label1.Location = '10,20'
$label1.Size = '280,20'
$label1.Text = 'Benutzername:'

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = '10,70'
$label2.Size = '280,20'
$label2.Text = 'vollstaendiger Name:'

$ProfileName = New-Object System.Windows.Forms.TextBox
$ProfileName.Location = '10,40'
$ProfileName.Size = '260,20'

$FullName = New-Object System.Windows.Forms.TextBox
$FullName.Location ='10,90'
$FullName.Size = '260,20'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = '75,120'
$okButton.Size = '75,23'
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = '150,120'
$cancelButton.Size = '75,23'
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton

#alle Subobjekte in einem Rutsch adden
$label1, $label2, $ProfileName, $FullName, $okButton, $cancelButton |%{$form.Controls.Add($_)}

$result = $form.ShowDialog()

 #das passiert nachdem der Dialog geschlossen wurde...
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{  #Parameter aus den Inputs des Dialogs übernehmen
  $params = @{
    Name    = $ProfileName.Text
    FullName  = $FullName.Text
    Password  = $([securestring]::new())
    Description = 'Schnuffeldischneuz'
  }
  $Null=New-LocalUser @params
  Add-LocalGroupMember -Group "Benutzer" -Member $params.Name
  Get-LocalUser $params.Name |Format-List *
}
pause

…works and because I’m testing everything I’m posting here… I’m gonna have to clean my computer with the Kärcher 😅😅😅


TheFamousSpy
1 year ago

Error message would have been useful.

You will need to enter a password or use the “nopassword” parameter

Erzesel
1 year ago
Reply to  TheFamousSpy

No, it’s not…

$textBox.Text is not defined… we also don’t get over.

In addition, New-LocalUser does not automatically assign a group binding, so the new user does exist, but only users of visible groups can be displayed in the login (administrator, user, guest). So a new user will not be displayed….

see above