none
Help on AD user account creation via CSV file RRS feed

  • Question

  • Hello I have a code through which i am trying to create multiple users via csv file, but every time i am getting an error "The object name has bad syntax" i have attached all , can some please tell me what i am doing wrong, is that CSV had and issue or code itself.

    Code

    -----------------

    $Users = Import-Csv -Path "C:\Scripts\New folder\Users.csv"            
    foreach ($User in $Users)            
    {            
        $Displayname = $User.Firstname + "" + $User.Lastname            
        $UserFirstname = $User.Firstname            
        $UserLastname = $User.Lastname            
        $OU = "$User.OU"            
        $SAM = $User.SAM            
        $UPN = $User.Firstname + "." + $User.Lastname + "@" + $User.Maildomain   
        $Password = $User.Password            
        New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true       

    --------------------------------------

    Error

    =======

    New-ADUser : The object name has bad syntax
    At C:\Scripts\New folder\Untitled2.ps1:12 char:5
    +     New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (CN=TestAP,@{Fir...d=Password@123}.OU:String) [New-ADUser], ADException
        + FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser

    CSV

    --------------

    FirstName,LastName,MailDomain,SAM,OU,Password
    Test,AP,devna.domain.local,TestAP,"OU=Test,DC=devna,DC=domain,DC=local",Password@123

    Tuesday, August 1, 2017 10:22 PM

Answers

  • This is the better way to do this and it will prevent and expose errors better.

    Import-Csv 'C:\Scripts\New folder\Users.csv' |
    	ForEach-Object{
    		$splat = @{
    			Name = '{0} {1}' -f $_.Firstname, $_.Lastname
    			SamAccountName = $_.SAM
    			Displayname = '{0} {1}' -f $_.Firstname,$_.Lastname
    			GivenName = $_.Firstname
    			SurName = $_.Lastname
    			Path = $_.OU
    			UserPrincipalName = '{0}.{1}@{2}' -f $_.Firstname,$_.Lastname, $_.Maildomain
    			AccountPassword = ConvertTo-SecureString $_.Password -AsPlainText -Force
    			Enabled = $true
    			ChangePasswordAtLogon = $false
    			PasswordNeverExpires = $true
        	}
    		New-ADUser @splat	
    	}

    You should always "splat" commands with a lot of parameters.

    Quoting this madly will almost never work.

    This will never work:    $OU = "$User.OU"    


    \_(ツ)_/



    • Edited by jrv Wednesday, August 2, 2017 12:04 AM
    • Marked as answer by Jugalkumar Wednesday, August 2, 2017 1:57 AM
    Tuesday, August 1, 2017 10:41 PM

All replies

  • This is the better way to do this and it will prevent and expose errors better.

    Import-Csv 'C:\Scripts\New folder\Users.csv' |
    	ForEach-Object{
    		$splat = @{
    			Name = '{0} {1}' -f $_.Firstname, $_.Lastname
    			SamAccountName = $_.SAM
    			Displayname = '{0} {1}' -f $_.Firstname,$_.Lastname
    			GivenName = $_.Firstname
    			SurName = $_.Lastname
    			Path = $_.OU
    			UserPrincipalName = '{0}.{1}@{2}' -f $_.Firstname,$_.Lastname, $_.Maildomain
    			AccountPassword = ConvertTo-SecureString $_.Password -AsPlainText -Force
    			Enabled = $true
    			ChangePasswordAtLogon = $false
    			PasswordNeverExpires = $true
        	}
    		New-ADUser @splat	
    	}

    You should always "splat" commands with a lot of parameters.

    Quoting this madly will almost never work.

    This will never work:    $OU = "$User.OU"    


    \_(ツ)_/



    • Edited by jrv Wednesday, August 2, 2017 12:04 AM
    • Marked as answer by Jugalkumar Wednesday, August 2, 2017 1:57 AM
    Tuesday, August 1, 2017 10:41 PM
  • Thanks but i do not know about splat, when i tried to run the above cmd as mentioned its given an error is any i need to change with csv file..

    New-ADUser : Parameter cannot be processed because the parameter name 'Password' is ambiguous. Possible matches include: 
    -PasswordNeverExpires -PasswordNotRequired.
    At line:16 char:14
    +         New-ADUser @splat
    +                    ~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException
        + FullyQualifiedErrorId : AmbiguousParameter,Microsoft.ActiveDirectory.Management.Commands.NewADUser

    Tuesday, August 1, 2017 11:43 PM
  • See.  Now you are getting a more helpful error message.  The next thing to do is look up how to correctly use the command and parameter.

    Help New-AdUser -Full

    This will tell you what is wrong.


    \_(ツ)_/



    • Edited by jrv Wednesday, August 2, 2017 12:03 AM
    Wednesday, August 2, 2017 12:02 AM
  • I fixed the example.  That is what I get for blindly using your code as a template.  That is how mistakes get started.


    \_(ツ)_/

    Wednesday, August 2, 2017 12:05 AM
  • Got that in the place of Accountpassword it was Password . IN Addition to this can we create a new mailbox of the new user as well.. with specific Database like database name or server name. ?
    Wednesday, August 2, 2017 1:48 AM
  • New-AdUser cannot create a mailbox.


    \_(ツ)_/

    Wednesday, August 2, 2017 1:50 AM
  • my bad. sorry thanks for your help.
    Wednesday, August 2, 2017 1:57 AM
  • Thanks just wanted to know while checking i found that User Logon Name its Coming like FirstName.LastName instead of FLastName. IN CSV UPN field i am entering FLASTNAME. Is there a way i can do the below change and get the output.

    Example - Sam Peter
    What is coming - Sam.Peter

    What it should be - SPeter

    So where should i change the code ?

    Thanks

    Wednesday, August 2, 2017 2:28 AM