Monday, November 19, 2012

Create mailboxes from a file

cls
Remove-Variable * -ErrorAction SilentlyContinue
Import-Module ActiveDirectory

#Prompt for users password which will be changed on first logo
 
 

$password = Read-Host "Enter initial password for user. It will be changed at first logon." -AsSecureString

#get users from file
$users = Get-Content "c:\temp\users.txt"
foreach ($user in $users)

{    
$split = $user.Split(",", 2)
$first = $split[0];
$last = $split[1];
$display = $first + " " + $last
$name = $first + " " + $last
$upn = $first + "." + $last + "@test.com"

#get the first letter of the first name so we can create alias
$firstletter = $first.substring(0,1)
$alias = $firstletter + $last
   
#test if user exists
$testuser = Get-ADUser -server DC01 -LDAPFilter "(sAMAccountName=$alias)"

if ($testuser -eq $Null)
{ 
write-host "creating" $alias
New-Mailbox -UserPrincipalName $upn -Alias $alias -Name $name -Password $password -OrganizationalUnit HQ -FirstName $first -LastName $last -DisplayName $display -ResetPasswordOnNextLogon $true -DomainController DC01

##database is not necessary
Start-Sleep -s 5
Add-DistributionGroupMember -Identity "TestDL" -Member $alias
}Else {
write-host $name "already exists in AD"
}
}

No comments:

Post a Comment