0

I am trying to bulk-add users into my Active Directory, but I am getting expression errors right at the start of the script. I am not a script buff at all, so I am really out of ideas from the get go.

$Users = Import-Csv ".\UsersFile.csv"
foreach ($User in $Users)
{
        -OrganizationalUnit $User.OU 
        -SamAccountName $User.UserName 
        -userPassword $User.Password 
        -GivenName $User.First 
        -Initials $USer.Initial 
        -sn $User.Last 
        -Displayname $User.DisplayName
        -Description $User.Description
        -Physicaldeliveryofficename $User.Office 
        -TelephoneNumber $User.Tel
        -Mail $User.mail 
        -streetaddress $User.Street 
        -postOfficeBox $User.Postbus
        -l $User.Location 
        -st $User.Provincie 
        -postalCode $User.Postcode 
        -c $User.Land 
        -deparment $User.Department
        -Company $User.Organisatie
        -Manager $User.Manager
        -Password $User.Password -ResetPasswordOnNextLogon $false 
}

The error log.

Missing expression after unary operator '-'. At C:\Users\Administrator\Desktop\CreateUserBulk.ps1:4 char:10 + - <<<< OrganizationalUnit $User.OU ` + CategoryInfo : ParserError: (-:String) [], Parseexception + FullyQualifiedErrorID : MissingExpressionAfterOperator


After trying the link (From serv) and editing the CSV and script accordingly, getting a lot more errors now with this.

Import-Csv : Cannot open file "C:\Users\administrator\UsersFile.csv". At C:\Users\administrator\Desktop\Untitled3.ps1:2 char:20 + $Users = Import-Csv <<<< -Delimiter ";" -Path ".\UsersFile.csv" + CategoryInfo : OpenError: (:) [Import-Csv], FileNotFoundException + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand You cannot call a method on a null-valued expression. At C:\Users\administrator\Desktop\Untitled3.ps1:9 char:53 + $FirstLetterFirstname = $UserFirstname.substring <<<< (0,1) + CategoryInfo : InvalidOperation: (substring:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is null. At C:\Users\administrator\Desktop\Untitled3.ps1:11 char:195 + New-ADUser -Name $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -DisplayName $Detailedname -GivenName $user.firstname -Surname $user.name -AccountPassword (ConvertTo-SecureString <<<< $Password -AsPlainText -Force) -Ena bled $true -Path $OU + CategoryInfo : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertToSe cureStringCommand


EDIT2: Fixed, I changed the source link to it's fullness and now it works!

4
  • and what is the delimiter in your csv source file? Commented Nov 17, 2013 at 11:33
  • Added, the delimiter is a semicolon Commented Nov 17, 2013 at 11:38
  • The first error is easy: The path to the csv is incorrect. The second error says: the variable $password is null. Commented Nov 17, 2013 at 12:51
  • Glad I could help ;) It's always the little things. Commented Nov 17, 2013 at 12:52

1 Answer 1

0

You are not creating a new AD user in your script. So -OrganizationalUnit should be undefined and throw an error, excpet you declare them as a variable first.

Import-Module ActiveDirectory 
$Users = Import-Csv -Delimiter ";" -Path ".\UsersFile.csv"  
foreach ($User in $Users)  
{  
    $FirstLetterFirstname = $User.firstname.substring(0,1)     

    New-ADUser -Name $User.firstname + " " + $User.name 
    -SamAccountName $FirstLetterFirstName + $User.name 
    //... and so on
} 

You can declare all variables first in the manner of $FirstletterFirstName and execute the New-ADUser command at the end of each loop, which makes it easier to read and modify later on.

The important part stays: Adding a new user is executed through the New-ADUser command which you are missing. You can also add the New-ADUser to the top of your loop, which should make your query work if there are no other syntax errors / Spelling errors in your code

//EDIT: You can find a working example at http://gallery.technet.microsoft.com/scriptcenter/ed20b349-9758-4c70-adc0-19c5acfcae45

and the TechNet article for New-ADUser: http://technet.microsoft.com/en-us/library/ee617253.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

I tried the example script, I altered it and the CSV accordingly to test it. But it's giving me even more errors than before. Edited my first post...
This is awkward. I was at the same position you were at some time and used this very script (and still use it) to insert dummy data into my Sharepoint dev machine if I need to. My bet is, the errors come from a faulty New-ADUser command you are trying to issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.