Share via

creating account using powershell

oladele Amoo Peters 1 Reputation point
2022-05-11T09:46:46.85+00:00

"How can i get $password to set auto generated password rather than static"

$username = "John009"
$password = "password"
$computers = Get-Content c:\serverlist.txt

OR $computers = Import-CSV c:\serverlist.csv | select Server

Foreach ($computer in $computers) {
$users = $null
$computer = [ADSI]“WinNT://$computer”
Try {
$users = $computer.psbase.children | Select-Object -expand name
if ($users -like $username) {
Write-Host "$username already exists"
} Else {
$user_obj = $computer.Create(“user”, “$username”)
$user_obj.SetPassword($password)
$user_obj.SetInfo()
$user_obj.Put(“description”, “$username”)
$user_obj.SetInfo()
$user_obj.psbase.invokeset(“AccountDisabled”, “False”)
$user_obj.SetInfo()
$users = $computer.psbase.children | Select-Object -expand name
if ($users -like $username) {
Write-Host "$username has been created on $($computer.name)"
} Else {
Write-Host "$username has not been created on $($computer.name)"
}
}
} Catch {
Write-Host "Error creating $username on $($computer.path): $($Error[0].Exception.Message)"
}
}

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Server | User experience | Other
0 comments No comments

1 answer

Sort by: Most helpful
  1. Newbie Jones 1,411 Reputation points
    2022-05-11T13:03:36.4+00:00

    Sounds like you want a password generator.

    A quick google search and this is the first result.

    https://arminreiter.com/2021/07/3-ways-to-generate-passwords-in-powershell

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.