add_users.ps1

#
# add_users.ps1
# 
# Written by Carol Wapshere, September 2008
#
# Use to create one or more user accounts, including mailbox, home folder, profile folder
# and required AD settings.
#
# Instructions:
#
#   1. Modify the add_users.csv file to include information about the users to add.
#      The file must have a header row and must include the following fields:
#            dn,UID,displayName,surname,givenName,password,mail,alias,description,UPN
#
#   2. Open the Exchange Management Shell
#
#   3. CD to C:\scripts and run add_users.ps1.
cat add_users.csv > unicode.csv # make utf8
import-csv unicode.csv | foreach {
write-host
write-host
write-host "Creating account for " $_.displayName
$secureString = ConvertTo-SecureString $_.password -AsPlainText –Force
New-Mailbox -Name $_.displayName -displayName $_.displayName -Alias $_.alias -OrganizationalUnit 'bbhfgg.local/BBH/Users' -UserPrincipalName $_.UPN -SamAccountName $_.UID -FirstName $_.givenName -Initials '' -LastName $_.surname -Database 'MEXBBH01\First Storage Group\Mailbox Database' -password $secureString -ResetPasswordOnNextLogon $false
$ldapstring = "LDAP://" + $_.dn
write-host $ldapstring
$user = [ADSI]$ldapstring
$homeRoot = "\\filbbh01\Home$\"
$profileRoot = "\\filbbh01\Profile$\"
$homeShare = "\\filbbh01\" + $_.UID
$homeDir = $homeRoot + $_.UID
$profileDir = $profileRoot + $_.UID
write-host "Creating folder " $homeDir
New-Item -path $homeRoot -name $_.UID -type directory
$caclsCmd = "cacls " + $homeDir + " /g " + $_.UID + ":C backup:R"
write-host $caclsCmd
invoke-expression $caclsCmd
write-host "Creating folder " $profileDir
New-Item -path $profileRoot -name $_.UID -type directory
$caclsCmd = "cacls " + $profileDir + " /g " + $_.UID + ":C backup:R"
write-host $caclsCmd
invoke-expression $caclsCmd
write-host
write-host "Setting AD attributes..."
write-host "    description: " $_.description
$user.Description = $_.description
write-host "    homeDirectory: " $homeShare
$user.Put("homeDirectory", $homeShare)
write-host "    homeDrive  P:"
$user.Put("homeDrive", "P:")
write-host "    profilePath: " $profileDir
$user.Put("profilePath", $profileDir)
$user.SetInfo()
} | out-file -filepath "c:\scripts\add_users.log" -encoding "UNICODE"