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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.