PowerShell: Create Active Directory Users In Bulk (Update 1.3)

For years I’ve had a AD user import VBS script as part of my toolkit, I thought it was about time to update it to PowerShell as well as add a few new features.

My AD User Creation script can be downloaded from the Microsoft TechNet Script Center.

Features and Requirements

This script requires the Active Directory PowerShell module be installed, which is a part of the Remote Server Administration Tools (RSAT) for client editions of Windows. This script has been tested running on Windows Server 2016 and Windows Server 2012 R2 Domain Controller’s and on a Windows 10 client.

This script will create users based on basic information provided by a CSV file. All other options are added via command line options. Options include:

  • Organisation Unit in which to create the users.
  • The UPN that users should have
  • Home Drive location
  • Home Drive Letter
  • Membership of an Active Directory Group
  • Account Expiry Date

The -file Parameter

As with my other scripts, it can optionally generate a log and email the log if configured to do so, and I’ve implemented options via command line switches. When running the script via Schedule Tasks or the command prompt be sure to use the -file parameter before specifying the script, so you can use “double quotes” for the command line switches that need them, if you do not use -file, then you should use ‘single quotes’.

CSV File Formatting

The first line of the CSV file should be the field names so the script (and you) knows what each field is for.

The CSV file should have the following structure: Firstname,Lastname,SAM,Password

Command Line Options

Here are the command line options available to you, with some examples of how to use them.

Command Line Switch Mandatory Description Example
-csv Yes Location and name of the csv file containing the user accounts to be created. E:\scripts\new-users.csv
-ou Yes Enter the DN (Distinguished Name) of the OU that you wish to put the new users in. The DN must be surrounded by “double quotes.” “ou=Imported_Accounts,dc=contoso,dc=com”
-upn Yes Enter the string you wish to use for the users UPN. contoso.com
-homeletter No* Enter the drive letter to use for the Home Drive. *This setting must be set along with the -homepath. H:
-homepath No* Enter the unc path to use for the location of the home drive. The users folder will be created automatically. *This setting must be set along with the -homeletter. \\filesrv01\share
-group No Enter the DN (Distinguished Name) of a group that the users should be added to, if required. “cn=Marketing,ou=UserGroups,dc=contoso,dc=com”
-expire No Enter the date the accounts should expire if required. The date format should be the same as the local systems format. 31/12/2018
-l No Location to store the optional log file. The name of the log file is automatically generated. E:\scripts\log
-sendto No The email address to send the log file to. me@contoso.com
-from No* The email address that the log file should be sent from.

*This switch isn’t mandatory but is required if you wish to email the log file.

-smtp No* SMTP server address to use for the email functionality.

*This switch isn’t mandatory but is required if you wish to email the log file.






-user No* The username of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.

-pwd No* The password of the account to use for SMTP authentication.

*This switch isn’t mandatory but may be required depending on your SMTP server.

-usessl No* Add this option if you wish to use SSL with the configured SMTP server.

Tip: If you wish to send email to outlook.com or office365.com you will need this.

*This switch isn’t mandatory but may be required depending on the configuration of the SMTP server.


Change Log

13/09/2017 1.3

  • Added check for existence of user before attempting to create user.
  • Improved logging to handle the above change.

22/07/2017 1.2

  • Improved code commenting for documentation purposes.
  • Added authentication and SSL options for e-mail notification.

PowerShell Code

# -------------------------------------------
# Script: Create-Accounts-CSV_v1-3.ps1
# Version: 1.3
# Author: Mike Galvin twitter.com/digressive
# Date: 13/09/2017
# -------------------------------------------


## If users list csv file exists then run the script
If (Test-Path $userslist)

    ## If logging is configured, start log
    If ($LogPath)
        $LogFile = ("AD-Account-Creation-{0:yyyy-MM-dd-HH-mm-ss}.log" -f (Get-Date))
        $Log = "$LogPath\$LogFile"

    If (Test-Path $UsersList)
        ## Start Log
        If ($LogPath)
            Start-Transcript $Log

    $UserCsv = Import-Csv -Path "$UsersList"

    ForEach ($User in $UserCsv)
        $DisplayName = $User.Firstname + " " + $User.Lastname
        $UserFirstName = $User.Firstname
        $UserLastName = $User.Lastname
        $Sam = $User.SAM
        $Upn = $Sam + "@$AdUpn"
        $Description = $DisplayName
        $Password = $User.Password

        $UserExist = Get-ADUser -filter "SamAccountName -eq '$Sam'"

        If ($UserExist -eq $null)
            New-ADUser -Name $Sam -DisplayName "$DisplayName" -SamAccountName $Sam -UserPrincipalName $Upn -GivenName "$UserFirstName" -Surname "$UserLastName" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OrganisationalUnit" -ChangePasswordAtLogon $true –PasswordNeverExpires $false -AccountExpirationDate $AdExpire -Verbose
            If ($HomeDrive)
                Set-ADUser $Sam -HomeDirectory $HomeUnc\$Sam -HomeDrive $HomeDrive -Verbose

            If ($AdGroup)
                Add-ADGroupMember "$AdGroup" $Sam -Verbose

            Write-Host "User with SAM:$Sam already exists"

    ## If log was configured stop the log
    If ($LogPath)

        ## If email was configured, set the variables for the email subject and body
        If ($SmtpServer)
            $MailSubject = "AD Account Creation Log"
            $MailBody = Get-Content -Path $Log | Out-String

            ## If an email password was configured, create a variable with the username and password
            If ($SmtpPwd)
                $SmtpCreds = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($SmtpPwd | ConvertTo-SecureString -AsPlainText -Force)

                ## If ssl was configured, send the email with ssl
                If ($UseSsl)
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -UseSsl -Credential $SmtpCreds

                ## If ssl wasn't configured, send the email without ssl
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer -Credential $SmtpCreds

            ## If an email username and password were not configured, send the email without authentication
                Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -SmtpServer $SmtpServer

    Write-Host ""
    Write-Host "There's no user list to work with."
    Write-Host ""

## End

As always I hope this helps in some way. If you’d like to get in touch with me please leave a comment or tweet me.


Follow Mike on Twitter: @Digressive

3 thoughts on “PowerShell: Create Active Directory Users In Bulk (Update 1.3)

  1. Good Job with the Script!
    But it dont want to work for me, maybe because of the Windowsupdate last Weekend.
    Trying to get it to work with Windows Server 2012r2 but geting this error every time:

    AUSFÜHRLICH: Ausführen des Vorgangs “New” für das Ziel “CN=srogers0203,Teilnehmer”.
    New-ADUser : Die Syntax des Objektnamens ist ungültig
    In C:\Users\Administrator\Desktop\Unbenannt2.ps1:64 Zeichen:9
    + New-ADUser -Name $sam -DisplayName “$displayname” -SamAccountName $sam – …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (CN=srogers0203,Teilnehmer:String) [New-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser


    • Hi Georg, I’ll try to help. I don’t think it’s due to a Windows Update, as I last tested it on the weekend. I could do with a bit more information but based on what you’ve posted it looks like that the script is stopping at the point where it creates the user. Does the CSV file have any extra characters that might be stopping it from completing, or perhaps the CSV file doesn’t have the correct formatting. The CSV file should be laid out like this: Firstname,Lastname,SAM,Password.

      Hope that helps, but let me know more information if you can.


  2. Pingback: PowerShell: Create Active Directory Users From CSV File — Stick To The Script! – Computer Services

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s