Bulk Upload New Users to Active Directory with Powershell RRS feed

  • Question

    1. I am trying to bulk upload new users to the Active Directory. This will be the first part of getting the users into their O365 accounts.

      I have created a CSV file with the users and their information.  I only need a few columns of information imported.

      The columns (as I have them listed) are Lastname, Firstname, Gradyear, StudentID, Password, OU

      These colums will appear in the Active Directory in the following fields:

      Lastname, Firstname, Office, Description, Password  All new users will go into one newly created OU.

      I am assigning passwords that I don't want changed and don't want to expire

      The script I am trying to use should import the csv file "testupload5.csv".

      This is what I have done from my computer:

    1. installed Microsoft Online Services Sign in Assistant for IT

    2. Installed Windows Azure Active Directory Module for Windows Powershell

    3. Opened Widows Azure AD as administrator

    4. entered Set-Executionpolicy -executionpolicy unrestricted -scope localmachine -force

    5. Import-Module MSOnline

    6. $Credential=get-credential  (logged in as O365 Admin when prompted)

    7. Connect-MsolService -Credential $credential

    8. My script is named c:\sampescript.ps1

    Here is the script: (this particular csv file is only a group of 5 users as a test)

    $Users = Import-Csv -Path "C:\testupload5.csv"            
    foreach ($User in $Users)            
        $Displayname = $User.Firstname + " " + $User.Lastname            
        $UserFirstname = $User.Firstname            
        $UserLastname = $User.Lastname            
        $OU = "$User.OU"            
        $Description = $User.StudentID
        $Office = $User.Gradyear             
        $Password = $User.Password            
        New-ADUser -Name "$Displayname" -DisplayName "$Displayname" - GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true -server msma-dc01            

    It's obvious that I am missing something because it is not working.  It's like I am not connected to the right location.  Active Directory is on a server and I am doing this from my PC. 

    Thanks in advance for those who can offer assistance.

    Friday, May 13, 2016 7:22 PM

All replies

  • In Azure AD you use New-msoluser not new-aduser, other than that your script looks good.  

    See link:

    I've had issues with the securestring command you're using and have in the past just simply not specified the password and then output the random password 

    See the second example on this page.

    Hope this helps!

    • Proposed as answer by Nebercoronius Saturday, May 14, 2016 4:16 AM
    • Unproposed as answer by Nebercoronius Wednesday, May 18, 2016 8:31 PM
    Friday, May 13, 2016 7:52 PM
  • Apparently I am more confused than I thought I was and have been mixing o365 and our Active Directory.  I want to import a CSV file with the following Column Names which corresponds to the "General" tab into our AD.

    • lastname
    • firstname
    • Office
    • Description
    • Password

    We have an OU called Students and under that I want to put this CSV file under an OU called "Class of 2020". I am only importing to Active Directory and not to o365.  I am assigning the passwords and don't want them to expire or to be changed.  Can I assume that I do need "New-ADUser"?  I now assume I will "Import-Module ActiveDirectory" instead of "MSOnline". 

    I really got myself into a pickle here.

    Wednesday, May 18, 2016 8:35 PM
  • So what's the end goal?  Are you just trying to get the accounts created in AD?  Are you trying to get them created and provisioned for O365 at the same time?  Do you have ADFS and dirsync setup already? 
    Friday, May 20, 2016 6:55 PM
  • Just trying to get them created in AD.  This is a group of incoming students.  Our staff takes a new laptop, that the student will receive, and logs in as the student to perform certain functions that can't be done from the original imaging.
    Friday, May 20, 2016 7:37 PM
  • Hi mrdmc618,

    If I understand correctly your requirement. You are looking steps to create bulk users in On-Premises Active Directory.

    If that is true, then you don't need to connect to Azure AD module and use msoluser, please follow below procedure to create user locally:

    Import-module activedirectory

    $Users = Import-Csv -Path "C:\testupload5.csv"           

     foreach ($User in $Users)            


         $Displayname = $User.Firstname + " " + $User.Lastname           

         $UserFirstname = $User.Firstname           

         $UserLastname = $User.Lastname           

         $OU = "$User.OU"           

         $Description = $User.StudentID

         $Office = $User.Gradyear            

         $Password = $User.Password           

         New-ADUser -Name "$Displayname" -DisplayName "$Displayname" - GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true -server msma-dc01           


    Note: for correct parameter please refer below article and run the above PowerShell Scrip by running your PowerShell with Administrator.

    Creates a new Active Directory user.

    However, if you are looking suggestion to add user in Office 365 (Azure AD). In that case you do not require to make any script. Office 365 provide sample CSV file to fill your user details and can import that CSV back in portal to create users in Office 365 (Azure AD). Please refer below links for more details:

    Add several users at the same time to Office 365 - Admin Help

    Sample CSV File for Adding Users to Office 365

    Please share your feedback, if above details was helpful to you and answered your query..



    Saturday, May 21, 2016 4:24 PM
  • The error I received was as follows:

    New-ADUser : A positional parameter cannot be found that accepts the argument '-'. At c:\523script.ps1:23 char:16

    +   New-ADUser -Name "$Displayname" -DisplayName "$Displayname" - GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true -server msma-dc01

         + CategoryInfo            :InvalidArgument: <:> [New-ADUser], ParameterBindingException

         + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.NewADUser

    Monday, May 23, 2016 9:14 PM
  • Can you please share your script and I will check and get back to you..
    Monday, June 6, 2016 7:26 AM