Import Users to Active Directory from CSV File with Script Powershell

Thanks to http://vicryhc.wordpress.com/2013/04/12/import-users-to-active-directory-from-csv-file-with-script-powershell/

Im trying to Import Users to Active Directory Users and Computers.

Create File with Excel and save file with format *.CSV (Comma delimited)

image

Create Script Powershell and Save with format *.ps1

 

Import-Module ActiveDirectory

$inputFile = Import-CSV  .\Import-User.csv
$log = “.\LOG-Import.log”
$date = Get-Date

Function createUsers
{

“Created Following User ( on ” + $date + “)      :” | Out-File $log -Append
“————————————————-” | Out-File $log -Append
foreach($line in $inputFile)
{
$sam = $line.UserName

$exists = Get-ADUser -LDAPFilter “(sAMAccountName=$sam)”
if (!$exists)
{
$hm = “\\ad12-01\Home$\”+$line.UserName
$UPN = $line.UserPrincipalName+ “@dzikra.local”
$sam = $line.UserName
$fullN = $line.FullName
$pat = “OU=”+$line.OU2+”,”+”OU=”+$line.OU1+”,”+”DC=DZIKRA,DC=LOCAL”
$dn = $line.FullName
$gn = $line.FirstName
$sn = $line.LastName
$mail = $line.EmailAddress
$Desc = $line.Description
$hd = “O”

new-aduser -SamAccountName $sam -Name $fullN -AccountPassword (ConvertTo-SecureString -AsPlainText “P@ssw0rd” -Force) -Enabled $true -Path $pat -DisplayName $dn -GivenName $gn -Surname $sn -UserPrincipalName $UPN -Description $Desc -EmailAddress $mail -HomeDirectory $hm -HomeDrive $hd
}
Else
{
“SKIPPED – ALREADY EXISTS OR ERROR : ” + $sam | Out-File $log -Append
}
}
“————————————————-” + “`n” | Out-File $log -Append
}
CreateUsers

Log Import User if the user is existing.

image