Need Assistance

Hi Everyone,

I have tried to export AD users list ( from csv file ) but I’m getting the below error , anyone can advise me right path,.

– Failed to create user VISHNUSOMAN - The object cannot be added because the parent is not on the list of possible superiors –

regards

Oliver,
Welcome to the forum. :wave:t3:

99 % of the users coming here are needing assistance. So the subject of your question should give a hint about what your question will be. :point_up:t3:

You may change the category of your question as well. It looks more like you’re asking for “PowerShell Help”. :wink:

Do you create users while exporting a list of them? :thinking:

In general:

  • Please provide the code you used (formatted as code please).
  • Please provide the complete error message (formatted as code please)

here is the error

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

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

}

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.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Please go back, edit your post once again and fix the formatting of your code and error mesages.

i have tried with new Topic window… but it’s remain same

?? What do you mean?

i have tried with new Topic window , but there it’s appearing the same … no difference…

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.

<#
.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 = “exoip.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
}

}Preformatted text

I don’t know anymore what to say. :man_shrugging:t3:

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

1 Like

This is not the OP’s code. They copied from: Create Active Directory Users from CSV with PowerShell - ALI TAJRAN, specifically: alitajran.com/wp-content/uploads/scripts/Add-NewUsers.ps1

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.

1 Like

good eye @dotnVo , I didn’t even check their username and just assumed they were Ali Tajran :rofl:
I think i’m seeing the full picture now.