
#Created By Alberto Igbinedion 
#Script To Create Users and MailEnable Users
# Create SpreadSheet with Row Containing <<<<<<>>>>>>> ExternalEmailAddress <<<<<<>>>>>> FirstName <<<<<<< LastName >>>>>>>
#  The Below Gotten Command from the  Exchange Admin Centre GUI Created The Whole Script
# New-MailUser -Name 'zzmicky zzmouse' -DisplayName 'zzmicky zzmouse' -ExternalEmailAddress 'SMTP:zzmicky.zzmouse@ons.gov.uk' -FirstName 'zzmicky' -LastName 'zzmouse' -UserPrincipalName 'zzmouz@Ons.Statistics.gov.uk' -Password [System.Security.SecureString] -Alias 'zzmicky.zzmouse' -Initials $null -OrganizationalUnit '63f55999-d4cd-433d-a20c-9fce3f3e797a' -ResetPasswordOnNextLogon:$true 
     
# Import Users
$NewUser = Import-Csv "C:\Scripts\Input\NewUsers.csv"

ForEach ($user In $NewUser)

{

# Define Variables using data from spreadsheet
$ExternalEmailAddress	= $User.ExternalEmailAddress.Trim()
$Firstname	    = $User.FirstName.Trim()
$Lastname	= $User.Lastname.Trim()

$Password = $User.Password.Trim()
#$Password = "P@ssword1" | ConvertTo-SecureString -AsPlainText -Force

# User SpreadSheet Variable to Define More Variable
[string]$Name = ($Lastname.substring(0,7))+($Firstname.substring(2,1))
[String]$DisplayName	=  "$Lastname, $Firstname"                                          #The Double "" is required to Evaluate and include the ","   
[String]$UserPrincipalName = "$Name@Statistics.gov.uk"
$OrganizationalUnit = "Ons.Statistics.gov.uk/ONS/ExchangeObjects"
$Alias = $ExternalEmailAddress.Split("@")[0]  

# Utilize Variables, Call the Variable into the New-Mailuser Command #Command To Create and MailEnable User.
 New-MailUser -Name $Name -DisplayName $DisplayName -FirstName $Firstname -LastName $Lastname `
 -UserPrincipalName $UserPrincipalName `
 -Password (ConvertTo-SecureString $Password -AsPlainText -Force) `
 -Alias $Alias `
 -Initials $null `
 -OrganizationalUnit $OrganizationalUnit `
 -ResetPasswordOnNextLogon:$False `
 -ExternalEmailAddress $ExternalEmailAddress


 }


