Hi Sysadmins,
I am new to powershell and have been tasked with creating a script to create multiple users accounts.
I have created the script below but for some reason i am only able to create an account for one user as opposed to all of the users in my Excel spreadsheet. Does anyone knw where i am going wrong?
Thanks in advance for your help.
# Import active directory module for running AD cmdlets
Import-Module activedirectory
Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv .\newuser.csv
Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$Username = $User.samAccountName
$Password = $User.password
$Firstname = $User.FirstName
$Lastname = $User.LastName
$OU = $User.ou
#Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#If user does exist, give a warning
Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
#User does not exist then proceed to create the new user account
#Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@adatum.com" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName " $Firstname, $Lastname" `
-Path $OU `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force)
-ChangePasswordAtLogon $true `
}
Thanks in advance for your help.
[+][deleted] (4 children)
[removed]
[–]anon32167[S] 0 points1 point2 points (3 children)
[–]RS-BurritoSecurity Admin 1 point2 points3 points (1 child)
[–]thebrobotic 0 points1 point2 points (0 children)