Generate random password , rather than $password = “Password” to the following script

#Define variables

$computers = Get-Content C:\scripts\Computers1.txt

$username = “user0707”

$password = “Password”

$fullname = “Local Administrator”

$local_security_group = “Administrators”

$description = “Description”

Foreach ($computer in $computers) {

$users = $null

$comp = [ADSI]”WinNT://$computer”

#Check if username exists

Try {

$users = $comp.psbase.children | Select-Object -expand name

if ($users -like $username) {

Write-Host “$username already exists on $computer”

} else {

#Create the account

$user = $comp.Create(“User”,”$username”)





#Set password to never expire

#And set user cannot change password





#Add the account to the local admins group

$group = [ADSI]”WinNT://$computer/$local_security_group,group”


#Validate whether user account has been created or not

$users = $comp.psbase.children | Select-Object -expand name

if ($users -like $username) {

Write-Host “$username has been created on $computer”

} else {

Write-Host “$username has not been created on $computer”




Catch {

Write-Host “Error creating $username on $($computer.path): $($Error[0].Exception.Message)”



Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

How to format code on

As to your question, consider using the GeneratePassword() method:

Add-Type -AssemblyName System.Web
$password = [System.Web.Security.Membership]::GeneratePassword(16,3)