Failed to create user VISHNUSOMAN - The object cannot be added because the parent is not on the list of possible superiors
PS Script
<#
.SYNOPSIS
Add-NewUsers.ps1
.DESCRIPTION
Create Active Directory users from CSV file.
.LINK
alitajran.com/create-active-directory-users-from-csv-with-powershell
.NOTES
Written by: ALI TAJRAN
Website: alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 03/12/2020 - Initial version
V2.00, 02/07/2024 - Added try/catch and changed to splatting
#>
Import active directory module for running AD cmdlets
Import-Module ActiveDirectory
Store the data from NewUsersFinal.csv in the $ADUsers variable
Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {
try {
# Define the parameters using a hashtable
$UserParams = @{
SamAccountName = $User.username
UserPrincipalName = “$($User.username)@$UPN”
Name = “$($User.firstname) $($User.lastname)”
GivenName = $User.firstname
Surname = $User.lastname
Initial = $User.initials
Enabled = $True
DisplayName = “$($User.firstname) $($User.lastname)”
Path = $User.ou #This field refers to the OU the user account is to be created in
City = $User.city
PostalCode = $User.zipcode
Country = $User.country
Company = $User.company
State = $User.state
StreetAddress = $User.streetaddress
OfficePhone = $User.telephone
EmailAddress = $User.email
Title = $User.jobtitle
Department = $User.department
AccountPassword = (ConvertTo-secureString $User.password -AsPlainText -Force)
ChangePasswordAtLogon = $True
}
# Check to see if the user already exists in AD
if (Get-ADUser -Filter "SamAccountName -eq '$($User.username)'") {
# Give a warning if user exists
Write-Host "A user with username $($User.username) already exists in Active Directory." -ForegroundColor Yellow
}
else {
# User does not exist then proceed to create the new user account
# Account will be created in the OU provided by the $User.ou variable read from the CSV file
New-ADUser @UserParams
# If user is created, show message.
Write-Host "The user $($User.username) is created." -ForegroundColor Green
}
}
catch {
# Handle any errors that occur during account creation
Write-Host "Failed to create user $($User.username) - $_" -ForegroundColor Red
}
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Are you talking about the formatting? Please carefully (re-) read the explanation I posted. You don’t need a new topic. You may try a new reply to this thread instead.
.DESCRIPTION
Create Active Directory users from CSV file.
.LINK
alitajran.com/create-active-directory-users-from-csv-with-powershell
.NOTES
Written by: ALI TAJRAN
Website: alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 03/12/2020 - Initial version
V2.00, 02/07/2024 - Added try/catch and changed to splatting
#>
Import active directory module for running AD cmdlets
Import-Module ActiveDirectory
Store the data from NewUsersFinal.csv in the $ADUsers variable
Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {
try {
# Define the parameters using a hashtable
$UserParams = @{
SamAccountName = $User.username
UserPrincipalName = “$($User.username)@$UPN”
Name = “$($User.firstname) $($User.lastname)”
GivenName = $User.firstname
Surname = $User.lastname
Initial = $User.initials
Enabled = $True
DisplayName = “$($User.firstname) $($User.lastname)”
Path = $User.ou #This field refers to the OU the user account is to be created in
City = $User.city
PostalCode = $User.zipcode
Country = $User.country
Company = $User.company
State = $User.state
StreetAddress = $User.streetaddress
OfficePhone = $User.telephone
EmailAddress = $User.email
Title = $User.jobtitle
Department = $User.department
AccountPassword = (ConvertTo-secureString $User.password -AsPlainText -Force)
ChangePasswordAtLogon = $True
}
# Check to see if the user already exists in AD
if (Get-ADUser -Filter "SamAccountName -eq '$($User.username)'") {
# Give a warning if user exists
Write-Host "A user with username $($User.username) already exists in Active Directory." -ForegroundColor Yellow
}
else {
# User does not exist then proceed to create the new user account
# Account will be created in the OU provided by the $User.ou variable read from the CSV file
New-ADUser @UserParams
# If user is created, show message.
Write-Host "The user $($User.username) is created." -ForegroundColor Green
}
}
catch {
# Handle any errors that occur during account creation
Write-Host "Failed to create user $($User.username) - $_" -ForegroundColor Red
}
@Olaf instructions were pretty straight forward. For the sake of moving this along, I’ve formatted the code so it’s readable here:
<#
.SYNOPSIS
Add-NewUsers.ps1
.DESCRIPTION
Create Active Directory users from CSV file.
.LINK
alitajran.com/create-active-directory-users-from-csv-with-powershell
.NOTES
Written by: ALI TAJRAN
Website: alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 03/12/2020 - Initial version
V2.00, 02/07/2024 - Added try/catch and changed to splatting
#>
#Import active directory module for running AD cmdlets
Import-Module ActiveDirectory
#Store the data from NewUsersFinal.csv in the $ADUsers variable
$ADUsers = Import-Csv "C:\temp\NewUsersFinal.csv" -Delimiter ","
#Define UPN
$UPN = "millenniumhotels.com"
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {
try {
# Define the parameters using a hashtable
$UserParams = @{
SamAccountName = $User.username
UserPrincipalName = "$($User.username)@$UPN"
Name = "$($User.firstname) $($User.lastname)"
GivenName = $User.firstname
Surname = $User.lastname
Initial = $User.initials
Enabled = $True
DisplayName = "$($User.firstname) $($User.lastname)"
Path = $User.ou #This field refers to the OU the user account is to be created in
City = $User.city
PostalCode = $User.zipcode
Country = $User.country
Company = $User.company
State = $User.state
StreetAddress = $User.streetaddress
OfficePhone = $User.telephone
EmailAddress = $User.email
Title = $User.jobtitle
Department = $User.department
AccountPassword = (ConvertTo-secureString $User.password -AsPlainText -Force)
ChangePasswordAtLogon = $True
}
# Check to see if the user already exists in AD
if (Get-ADUser -Filter "SamAccountName -eq '$($User.username)'") {
# Give a warning if user exists
Write-Host "A user with username $($User.username) already exists in Active Directory." -ForegroundColor Yellow
}
else {
# User does not exist then proceed to create the new user account
# Account will be created in the OU provided by the $User.ou variable read from the CSV file
New-ADUser @UserParams
# If user is created, show message.
Write-Host "The user $($User.username) is created." -ForegroundColor Green
}
} catch {
# Handle any errors that occur during account creation
Write-Host "Failed to create user $($User.username) - $_" -ForegroundColor Red
}
}
For the most part this looks fine, though I would change your check if the user already exists to this:
if (Get-ADUser -Identity $($User.username)) {
#...
There’s no reason to filter for the specific user when you can user the Identity parameter and be specific.
Past that, from Google searching your error text there are plenty of mentions of that error. The most likely culprit is the specification of the OU to create the user in. This post illustrates an example of that.
Since you know the user that the error happened on, open your CSV, find your row for “VISHNUSOMAN” and see what is in their “OU” column.
So basically, OP has googled, copied something from the internet, ran into an error, and is asking for help now here.
The issue is almost certainly with the value being passed to -Path in the New-ADUser part of this script. It’s likely a valid object, but not an OU, hence the error. OP needs to provide the proper OU value in the CSV, it’s probably incorrect (like a group or something). This isn’t inherently a PS code issue, as much as just bad data from the CSV probably.