Answer the question
In order to leave comments, you need to log in
What is the correct way to write a PowerShell script?
The bottom line is, you need to write a script so that users are created in Active Directory with it:
1. Number of users
2. User names
3. Password for users
I wrote such a script, I see an error in it, but I can’t do it, I ask for help
Import-Module ActiveDirectory
# Variables
$number= Read-Host "Enter the number of users"
$input = Read-Host 'Enter a name'
$pass = Read-Host 'Enter the password'
$count=1..$number
# Create OU
New -ADOrganizationalUnit -Name "DemoOffice" -Path “DC=demo,DC=lab”
New-ADOrganizationalUnit -Name "Users" -Path “OU=DemoOffice,DC=demo,DC=lab”
New-ADOrganizationalUnit -Name "Computers" -Path “OU=DemoOffice,DC=demo,DC=lab”
# Loop with users
foreach ($i in $count) {
$Users = @{
Name = "$input"
GivenName = " $input"
UserPrincipalName = "[email protected]"
Path = "OU=Users,OU=DemoOffice,DC=demo,DC=lab"
ChangePasswordAtLogon = $true
AccountPassword = "$pass" | ConvertTo-SecureString -AsPlainText -Force
Enabled = $true
}
New-ADUser @Users
}
Answer the question
In order to leave comments, you need to log in
Issue resolved, Thanks Eugene
Import-Module ActiveDirectory
# Create OU
New-ADOrganizationalUnit -Name "DemoOffice" -Path “DC=demo,DC=lab”
New-ADOrganizationalUnit -Name "Users" -Path “OU=DemoOffice,DC=demo, DC=lab”
New-ADOrganizationalUnit -Name "Computers" -Path “OU=DemoOffice,DC=demo,DC=lab”
$number = Read-Host "Enter number of users"
$count=1..$number
$users = foreach ($i in $count)
{
Read-Host "Enter username number $i"
}
$pass = Read-Host 'Enter the password'
# Loop with users
foreach ($user in $users) {
$Username = @{
Name = "$user"
GivenName = "$user"
UserPrincipalName = "[email protected]"
Path = "OU=Users,OU=DemoOffice,DC=demo,DC=lab"
ChangePasswordAtLogon = $true
AccountPassword = "$pass" | ConvertTo-SecureString -AsPlainText -Force
Enabled = $true
}
New-ADUser @Username
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question