New-ADUser From CSV

Welcome Forums General PowerShell Q&A New-ADUser From CSV

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
4 years, 5 months ago.

  • Author
    Posts
  • #15654

    Participant
    Points: 0
    Rank: Member

    this is my code

    Import-Csv C:\Users\Administrator\Desktop\ComputersUsers.csv |foreach {New-ADUser -GivenName $_.GivenName -Surname $_.Surname -DisplayName "$_.DisplayName" -SamAccountName $_.SamAccountName -UserPrincipalName $_.UserPrincipalName -AccountPassword(ConvertTo-SecureString -AsPlainText "1234" -Force) -Enabled $true -ChangePasswordAtLogon $false -Name "$_.DisplayName" -Path "$_.OU" }

    this is my CSV
    GivenName,Surname,DisplayName,SamAccountName,UserPrincipalName,accountpassword,OU
    Namal,Computers1,Namal Computers 1,Computers1,Computers1@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers2,Namal Computers 2,Computers2,Computers2@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers3,Namal Computers 3,Computers3,Computers3@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers4,Namal Computers 4,Computers4,Computers4@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers5,Namal Computers 5,Computers5,Computers5@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers6,Namal Computers 6,Computers6,Computers6@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers7,Namal Computers 7,Computers7,Computers7@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers8,Namal Computers 8,Computers8,Computers8@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers9,Namal Computers 9,Computers9,Computers9@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers10,Namal Computers 10,Computers10,Computers10@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers11,Namal Computers 11,Computers11,Computers11@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers12,Namal Computers 12,Computers12,Computers12@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers13,Namal Computers 13,Computers13,Computers13@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers14,Namal Computers 14,Computers14,Computers14@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers15,Namal Computers 15,Computers15,Computers15@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers16,Namal Computers 16,Computers16,Computers16@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""

    Why is not working correctly?

    ERROR:

    New-ADUser : The object name has bad syntax
    At C:\PowershellScript\New-Aduser from csc.ps1:2 char:72
    + Import-Csv C:\Users\Administrator\Desktop\ComputersUsers.csv |foreach {New-ADUse ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (CN=@{GivenName\...s,DC=local"}.OU:String) [New-ADUser], ADException
    + FullyQualifiedErrorId : The object name has bad syntax,Microsoft.ActiveDirectory.Management.Commands.NewADUser

    Thank you for your help

    Itamar.

  • #15655

    Member
    Points: 0
    Rank: Member

    You've got quotation marks embedded in your CSV file for the OU field. I haven't tested your code, but that seems to be the most likely cause of a problem, at first glance. Try this, to see if it fixes the problem:

    -Path $_.OU.Trim('"')
    
  • #15656

    Participant
    Points: 0
    Rank: Member

    First TNX.
    I test my code and delete PATH from my script doesn't help.
    also with ur code doesn't help

    any suggestion ?

  • #15659

    Moderator
    Points: 24
    Team Member
    Rank: Member

    Please attach your script and the CSV. I would like to run this against my test AD today evening.

    Thanks
    Daniel

  • #15661

    Participant
    Points: 0
    Rank: Member

    I attach the file.
    tnx.

  • #15790

    Participant
    Points: 0
    Rank: Member

    Thank i will try it tomorrow

    Itamar

  • #15789

    Moderator
    Points: 24
    Team Member
    Rank: Member

    Hello Itamar,

    Apologies for not getting back to you earlier on this. Unfortunately something else came up on Wednesday evening which prevented me to look into this and Thursday / Friday were already planned out.

    The good news I can explain why it does not work for you and provide solutions.

    1. The cue is actually in the error message. The distinguished name for the user account is not correct (CN=@{GivenName\…s,DC=local”}.OU:String)

    Why? You've used double quotes to expand "$_.DisplayName" and "$_.OU" as strings but did not use the correct format for these expressions.

    What happens is that PowerShell will expand the $_ object into the string and append .DisplayName and .OU. You can actually see this easily if you leverage the -WhatIf switch for New-ADUser.

    Example:
    Import-Csv C:\Users\Administrator\Desktop\ComputersUsers.csv |foreach {New-ADUser .... -WhatIf }

    Output:
    What if: Performing the operation "New" on target "CN=@{GivenName\=Namal\; Surname\=Computers1\; DisplayName\=Namal Computers 1\; SamAccountName\=Computers1\; UserPrincipalName\=Comp
    uters1@students.local\; accountpassword\=1234\; OU\=\"OU\=ComputersRoom\,OU\=Namal\,OU\=Users\,OU\=StudentsLocal\,DC\=students\,DC\=local\"}.DisplayName,@{GivenName=Namal; Surname=Co
    mputers1; DisplayName=Namal Computers 1; SamAccountName=Computers1; UserPrincipalName=Computers1@students.local; accountpassword=1234; OU="OU=ComputersRoom,OU=Namal,OU=Users,OU=Stude
    ntsLocal,DC=students,DC=local"}.OU".
    ....

    Solution 1:
    $_.DisplayName and $_.OU need to be handled as expression in the quoted string.

    -Name "$( $_.DisplayName )" -Path "$( $_.OU )"

    Solution 2:
    Just omit the double quotes and assign directly which I would prefer.

    -Name $_.DisplayName -Path $_.OU

    2. The other problem is your OU column in the CSV input file. You've specified triple quotes to escape the commas in the distinguished name of the OU path.

    """OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""

    Solution 1:
    Clean up the input file and only use one double quote at both ends of the OU path.
    "OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"

    Solution 2:
    Remove the double quotes you've imported while you assigned it to the -Path parameter.

    Example 1:
    -Path "$( $_.OU.Trim('"') )"

    Example 2:
    -Path $_.OU.Trim('"')

    I hope my answer helps you to get back on track.

    Best,
    Daniel

The topic ‘New-ADUser From CSV’ is closed to new replies.