Tuesday, May 12, 2015

Active Directory 2012 Tip: how to import users in bulk from a CSV

Here's a sample PowerShell script for importing users in bulk into Active Directory (AD).  It configures these AD fields:
  • SamAccountName
  • UserPrincipalName
  • GivenName
  • Initials
  • Surname
  • DisplayName
  • Name
  • Description
  • Company
  • StreetAddress
  • City
  • State
  • PostalCode
  • OfficePhone
  • Fax
  • EmailAddress
  • AccountPassword
  • ChangePasswordAtLogon
  • PasswordNeverExpires
  • Enabled
  • PassThru
And then here's the script that I use to perform the import:
import-csv c:\temp\100.csv | foreach-object {New-ADUser -SamAccountName $_.SAMAccountName -UserPrincipalName $_.SAMAccountName -GivenName $_.givenName -Initials $_.Initials -Surname $_.Surname -DisplayName $_.DisplayName -Name ($_.givenName + $_.Initials + $_.Surname) -Description $_.Description -Company $_.Company -StreetAddress $_.StreetAddress -City $_.City -State $_.State -PostalCode $_.PostalCode -OfficePhone $_.OfficePhone -Fax $_.Fax -EmailAddress $_.EmailAddress -AccountPassword (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force) -ChangePasswordAtLogon $False -PasswordNeverExpires $True -Enabled $True -PassThru -WhatIf}

No comments: