PowerShell 3.0 Script creates AD user and Exchange Mailbox.

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 .

Advertisement
This entry was posted in Exchange 2010 SP2, Exchange Cmdlets, Exchange Management Roles, Exchange Management Shell, Microsoft Exchange, PowerShell, PowerShell 3.0, WIndows 8.1, Windows Server 2012, Windows Server 2012 R2 and tagged , , , . Bookmark the permalink.

2 Responses to PowerShell 3.0 Script creates AD user and Exchange Mailbox.

  1. 95Eldon says:

    Hello blogger, i must say you have high quality posts
    here. Your page should go viral. You need initial traffic only.
    How to get it? Search for; Mertiso’s tips go viral

  2. 86Concepcion says:

    I see you don’t monetize your page, don’t waste your traffic,
    you can earn extra cash every month because you’ve got
    hi quality content. If you want to know how to make
    extra $$$, search for: best adsense alternative Wrastain’s tools

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s