Create 0365 accounts for new AD Users



Import-Module activedirectory

$CurrentDate = (Get-Date)
$When = ((Get-Date).AddHours(-3)).Date
$userarray = Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated,EmailAddress | Select SamAccountName,EmailAddress

foreach ($user in $userarray)
{
$Username = $user.SamAccountName
#$EmailAddress = $user.EmailAddress
if ($EmailAddress -ne $null)
{
Enable-RemoteMailbox -Identity $Username -RemoteRoutingAddress $Username"@365DOMAIN.mail.onmicrosoft.com"
write-host "$CurrentDate: ENABLED REMOTE MAILBOX FOR: $Username" | Out-File UserEmailCreation.csv -Append -Encoding Unicode
}
}