Script - PowerShell

Good afternoon,

I have the following doubt, I am creating a script to create a user in my Active Directory automatically with PowerShell ISE.

In the following script I want these two users to have the same password without having to type it twice, that is, when I am asked to enter the password for each user.

I want that at the moment of placing the password, I take it for these two users that I am creating as an example.

Someone who can guide me please.

Best regards.

$user = New-ADUser "Roberto"
$pwd = Read-Host "Ingresar Contraseña" -AsSecureString
$pwd = Set-ADAccountPassword Roberto -NewPassword $pwd
Enable-ADAccount Roberto
$user = New-ADUser "Rocio"
$pwd = Read-Host "Ingresar Contraseña" -AsSecureString
$pwd = Set-ADAccountPassword Rocio -NewPassword $pwd
Enable-ADAccount Rocio
LVL 6
R@f@r P@NC3RVirtualization SpecialistAsked:
Who is Participating?
 
sirbountyCommented:
Are you trying to create the OU?  If so - that's a one-time event.  Just run this outside of your script (otherwise it will work once, then fail every subsequent run)
  $ouPath = 'full path to the ou'  # i.e. "OU=Calidad,DC=domain,DC=com"
  $ou =  New-ADOrganizationalUnit $ouPath

At some point you may want to create a variable containing all of the account attributes.  Try something like this:

$pwd = Read-Host "Ingresar Contraseña" -AsSecureString
$ouPath = 'OU=Calidad,DC=domain,DC=com'
foreach ($username in @('Johana','David')) {
  $user_attr = @{
  SamAccountName = $username
  AccountPassword = $pwd
  Enabled = $true
  Path = $ouPath 
  }
  New-ADUser @user_attr
}

Open in new window

0
 
sirbountyCommented:
You were reassigning your $pwd variable...try this:
$pwd = Read-Host "Ingresar Contraseña" -AsSecureString

$user = New-ADUser "Roberto" 
Set-ADAccountPassword Roberto -NewPassword $pwd
Enable-ADAccount Roberto
$user = New-ADUser "Rocio"
Set-ADAccountPassword Rocio -NewPassword $pwd
Enable-ADAccount Rocio

Open in new window

1
 
sirbountyCommented:
And perhaps more efficient as a loop...
$pwd = Read-Host "Ingresar Contraseña" -AsSecureString

foreach ($username in @('Roberto','Rocio')) {
  $user = New-ADUser $username
  Set-ADAccountPassword $username -NewPassword $pwd
  Enable-ADAccount $username
}

Open in new window

0
 
R@f@r P@NC3RVirtualization SpecialistAuthor Commented:
Good afternoon,

Thanks for clarifying my doubt, the script did work and I apply the same password to all users.

But now I'm trying to add a certain number of users to a new organizational unit but they give me certain errors, the users create them but outside the organizational unit, I would like to see if you can help me with this new doubt.

Attach the script to powershell.


$pwd = Read-Host "Ingresar Contraseña" -AsSecureString
foreach ($username in @('Johana','David')) {
foreach ($ouname in @('Calidad')) {
  $user = New-ADUser $username
  $ou =  New-ADOrganizationalUnit $ouname
  Set-ADAccountPassword $username -NewPassword $pwd
  Enable-ADAccount $username
}
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.