Am trying to upload users in an OU I created and I have .csv file with the fields below; firstname lastname username password ou
Below is the script am running and am getting this error when I run it: "The operation failed because UPN value provided for addition/modification is not unique forest-wide.."
Import-Module activedirectory
$ADUsers = Import-csv; .\ nameofcsv.csv
foreach ($User in $ADUsers) {
$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$OU = $User.ou
if (Get-ADUser -F {SamAccountName -eq $Username})
{
Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
#Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@domainname" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Lastname, $Firstname" `
-Path $OU `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force)
}
}