Issue with New-ADUser Cmdlet

Welcome Forums General PowerShell Q&A Issue with New-ADUser Cmdlet

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

 
Participant
2 weeks, 4 days ago.

  • Author
    Posts
  • #112763

    Participant
    Points: 0
    Rank: Member

    Hi Friends,
    Here I'm again with a new question.
    I'm struggling with my script which add new users in bulk. I've referred many forum, websites & videos and they all use the exact method which I'm using but I'm getting this error after executing my script.
    Here's the error message which I receive:

    New-ADUser : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again..

    Below is my script which imports a csv file which have the data for my users and add certain parameters:
    =============================================================

    
    Import-Csv .\KuramaUsers.csv 
     
    ForEach-Object {
        $UserPrincipalName = $_.samAccountName + "@{kurama}.local" ;
        New-ADUser -SamAccountName $_.samAccountName `
        -UserPrincipalName $userPrincipalName `
        -Name $_."name" `
        -DisplayName $_.Name `
        -GivenName $_.Cn `
        -SurName $_.Sn `
        -EmailAddress $_.EmailAddress `
        -Title $_.Title `
        -Description $_.Description `
        -Department $_.Department `
        -Company $_.Company `
        -Path $_.Path `
        -Enabled $false `
        -PassThru `
    }
    code>

    Peace & Cheers,
    Sammy Boy.

  • #112768

    Participant
    Points: 0
    Rank: Member

    Hi Friends,
    Just to add, the word "code>" at the end is not present in my script. It's just that something which went wrong while posting.

    Also, the same goes with the brown color stripes with a dot in it, they're actually not present in my script. Not sure why it's visible in my post.

    I've also tried adding quotations like I shown for NAME which didn't help.

    Feel free to ask if you have any doubts with the raised concern.

    Peace & Cheers,
    Samson V.

  • #112771

    Participant
    Points: 0
    Rank: Member

    The problem is most likely in your CSV. The error is telling you that Name is Mandatory and you are giving it a NULL value. You can investigate like so:

    $csv = Import-Csv .\KuramaUsers.csv
    $csv | Where{!$_.Name}
    

    You also have name referenced differently that $_.Name, although it shouldn't matter I would just use $_.Name unless you had a space in the header (e.g. $_."My Name")

        -Name $_."name" `
        -DisplayName $_.Name `
    
  • #112777

    Participant
    Points: 0
    Rank: Member

    Thanks, Rob for the quick response..

    So, I used your method to Test the csv file which you gave.. So, the first line get the data properly as I tried looking the info of the variable and it shows the data for all the listed users in the CSV.

    PS C:\%FOLDERNAME%> $csv
    
    
    
    Name           : %STAFFNAME%
    
    samAccountName : %STAFF USERNAME%
    
    Cn             : %STAFF FIRSTNAME%
    
    Sn             : %STAFF LASTNAME%
    
    EmailAddress   : %STAFF EMAIL%
    
    Title          : %RELEVANT INFO%
    
    Department     : %RELEVANT INFO%
    
    Description    : %RELEVANT INFO%
    
    Company        : %RELEVANT INFO%
    
    Path           : "%DESIRED OU%"
    
    

    And when I run the out for "$csv | Where{!$_.Name}", it doesn't give any output. That is something strange for me.

    With regards to the $_.Name thingy, it was just to let you all know that I even tried that.  🙂

    Peace & Cheers,
    Samson V.

  • #112813

    Participant
    Points: 0
    Rank: Member

    Are those % values what you are sending? If the query is coming back with nothing, that is the expected results because none of the rows have a NULL value in Name. You should also use splatting over the line continuation:

    $csv = Import-CSV -Path .\KuramaUsers.csv 
    
    foreach ($user in $csv) {
        $params = @{
            SamAccountName    = $user.samAccountName 
            UserPrincipalName = '{0}@mydomain.local' -f $user.samAccountName
            Name              = $user.Name 
            DisplayName       = $user.Name 
            GivenName         = $user.Cn 
            SurName           = $user.Sn 
            EmailAddress      = $user.EmailAddress 
            Title             = $user.Title 
            Description       = $user.Description 
            Department        = $user.Department 
            Company           = $user.Company 
            Path              = $user.Path 
            Enabled           = $false 
            PassThru          = $true 
        }
    
        New-ADUser @params
    }
    
  • #112844

    Participant
    Points: 0
    Rank: Member

    Thanks, Rob for the help.

    I'm now getting the below error message which your provided method.

    New-ADUser : Directory object not found
    At line:21 char:5
    +     New-ADUser @params
    +     ~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (CN=%Username%...%DOMAIN%,DC=LOCAL:String) [New-ADUser], ADIdentityNotFoundEx 
       ception
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Micros 
       oft.ActiveDirectory.Management.Commands.NewADUser
    
    

    With regards to the % sign in my previous post, that was just to avoid personal information over the forum.

    I'm still unable to find the reason behind the failures.

    Peace & Cheers,
    Samson V.
     

  • #112931

    Participant
    Points: 20
    Rank: Member

    Seems like path is wrong. Please show us the path, a sample one. Change only the content, please don't add special character, do like CN=Users, OU=Example1, OU=example2...

  • #113044

    Participant
    Points: 0
    Rank: Member

    Hi KV,

    Even I did feel that the path is incorrect. So, I changed it & it was back to the initial error which I reported.

    With regards tot he special characters, there are none in the path. I edited the output with a % sign, tp avoid publishing sensitive data on the forum.

    Possibly, I'll start from scratch again & see how's the outcome. Thanks everyone for the inputs.

    Peace & Cheers,
    Samson V.

You must be logged in to reply to this topic.