This interactive script requires PowerShell 3.0 . It creates an Active Directory user and their corresponding mailbox using a remote session at the same time. It should work out of the box, but specific variables will have to be edited to reflect the Active Directory configuration of the organization in question.
function New-Employee {
Param ($firstName = (Read-Host "Enter FirstName"),
$lastName = (Read-Host "Enter LastName"),
$department = (Read-Host "Enter Department"),
$userPrincipalName =(Read-Host " Enter UserPrincipalName as FirstNameLastName@yourdomain.net"),
$name = "$firstName" +" " + "$lastName",
$sam = "$firstname" + "$lastname",
$alias = "$firstname" + "$lastname",
$initialpassword = (Read-Host " Enter Initial Password"),
$FromEmailAddress = (Read-Host " Enter From Email Address for Email Notification"),
$ToEmailAddress = (Read-Host " Enter To Email Address for Email Notification")
)
$initialgroups = 'Office','HeadOffice'
$whoami = whoami /upn
if ($department -eq "HR_OU") {
$OU = "OU=HR_OU,OU=Users-all,DC=yourdomain,dc=net"
New-ADUser -AccountPassword (convertto-securestring $initialpassword -asplaintext -force) -GivenName $firstname -SurName $lastname -UserPrincipalName $userprincipalname -Name $name -Enabled $true -Path $OU -Department $department -SamAccountName $Sam -ChangePasswordAtLogon $true
Add-ADPrincipalGroupMembership -Identity $sam -MemberOf $initialgroups
}Elseif
($department -eq "IT_OU") {
$OU = "OU=IT_OU,OU=Users-all,DC=yourdomain,dc=net"
New-ADUser -AccountPassword (convertto-securestring $initialpassword -asplaintext -force) -GivenName $firstname -SurName $lastname -UserPrincipalName $userprincipalname -Name $name -Enabled $true -Path $OU -Department $department -SamAccountName $Sam -ChangePasswordAtLogon $true
Add-ADPrincipalGroupMembership -Identity $sam -MemberOf $initialgroups
}Elseif
($department -eq "Marketing_OU") {
$OU = "OU=Marketing_OU,OU=Users-all,DC=yourdomain,dc=net"
New-ADUser -AccountPassword (convertto-securestring $initialpassword -asplaintext -force) -GivenName $firstname -SurName $lastname -UserPrincipalName $userprincipalname -Name $name -Enabled $true -Path $OU -Department $department -SamAccountName $Sam -ChangePasswordAtLogon $true
Add-ADPrincipalGroupMembership -Identity $sam -MemberOf $initialgroups
}Elseif
($department -eq "Production_OU") {
$OU = "OU=Production_OU,OU=Users-all,DC=yourdomain,dc=net"
New-ADUser -AccountPassword (convertto-securestring $initialpassword -asplaintext -force) -GivenName $firstname -SurName $lastname -UserPrincipalName $userprincipalname -Name $name -Enabled $true -Path $OU -Department $department -SamAccountName $Sam -ChangePasswordAtLogon $true
Add-ADPrincipalGroupMembership -Identity $sam -MemberOf $initialgroups
}Elseif
($department -eq "Accounting_OU") {
$OU = "OU=Accounting_OU,OU=Users-all,DC=yourdomain,dc=net"
New-ADUser -AccountPassword (convertto-securestring $initialpassword -asplaintext -force) -GivenName $firstname -SurName $lastname -UserPrincipalName $userprincipalname -Name $name -Enabled $true -Path $OU -Department $department -SamAccountName $Sam -ChangePasswordAtLogon $true
Add-ADPrincipalGroupMembership -Identity $sam -MemberOf $initialgroups
}
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchangeserver/powershell
Import-PSSession -Session $s -AllowClobber
Enable-Mailbox -identity $name -Alias $alias
Send-MailMessage -From $FromEmailAddress -to $ToEmailAddress -Subject "New User Created Notification" -Body " New User $name and mailbox have been created in the $OU Organizational Unit by $whoami. !!" -SmtpServer exch00.yourdomain.net
Write-Host "Active Directory User $name and their Mailbox have been created successfully in the $OU Organizational Unit by $whoami. !!"
}
New-Employee
I hope someone finds this useful. An update to this script is available at this link .
Leave a reply to 95Eldon Cancel reply