User accounts in Active directory can be a pain (and boring) to create… especially if you have a high staff turnover or you need to add extra bits to user accounts once they’ve been provisioned.
For example, when we create a user account in our domain environment i need to:
- Format the Display name in a certain way. eg, “Smith Jane (CLPCT)”
- Format the username (Jane.Smith)
- As we are part of a large AD forest, i sometimes need to choose a different domain suffix in the UPN.
- The account needs to be added to the correct OU to pick up group policy
- The mailbox needs to be placed in a particular database in exchange depending on the domain and the users name\position
- Custom attributes sometimes need to be added
- Add the user to one or more common AD security groups
- Depending on the domain, add a profile path based on the users login name
The list goes on and on. With so many steps involved, its easy to miss some and cause problems later down the line.
I decided to create an easy way to create the accounts by entering the bare minimum about the user then selecting the rest of the info from menus and letting powershell do all the work for me.
To make this happen i used:
Before doing any of this, i suggest you TEST on a TEST domain, or a TEST OU…. TEST!
When installing powerGUI, be sure to select AD and Exchange from the powerpacks when you get to the installation feature screen. Also, when installing the Quest activeroles cmdlets, select the option to change powershell to allow unsigned code to run otherwise you’ll hit problems when you come to run your scripts.
Now that bits done we can start on some code…
All my code does is have the commands to create an exchange mailbox and modify an AD user account ready filled with variables, then have multiselection menus and prompts to popualte them. here we go…
First things first. We need to work out what commands we will be using and what values we can apply. Creating an exchange account will also create an account in active directory, so I use the New-Mailbox command. typing the command into a new powerGUI window will show you all the available values you can apply when creating the account. Some user object attributes arent available with that command so we will need to use Set-QADUser after the exchange command to add the bits its missing.
So here’s the command I use for the new mailbox:
New-Mailbox -DomainController $dc -name $name -userprincipalname $upn -Alias $username -OrganizationalUnit $ou -FirstName $firstname -LastName $LastName -Password $password -ResetPasswordOnNextLogon $true -Database $maildatabase -DisplayName $displayname
And the extra values you cant specify in the New-Mailbox command:
Set-QADUser -Identity $upn -ProfilePath $profilepath
All the values starting with $ are variables ive set before the commands run. To set the variables ive added some prompts, along with some “write-host” commands to list all the options.
Here I prompt for the basics:
$firstname = read-host -prompt “Enter First Name”
$lastname = read-host -prompt “Enter Last Name”
$trust = read-host -prompt “LCFT or CLPCT or ICO”
$password = read-host -assecurestring -prompt “Please enter a Password”
Then from the Values above, i can construct some of the other values based on how we format them in our domain.
$name=$lastname+” “+$firstname
$username=$firstname+”.”+$lastname
$displayname=$name+” (“+$trust+”)”
if ($trust -eq “CLPCT”) {
$domain = “@centrallancashire.nhs.uk”
$attrib = ” ”
}
if ($trust -eq “LCFT”) {
$domain = “@lancashirecare.nhs.uk”
$attrib = “TCS-LCFT-CL”
}
if ($trust -eq “ICO”) {
$domain = “@centrallancashire.nhs.uk”
$attrib = “TCS-ICO”
}
$upn=$username+$domain
Here’s the options for the $maildatabase value.
Write-Host “”
Write-Host -foregroundcolor Green “Please Pick a Mailbox Database”
Write-Host “”
Write-Host “0 – TCS User”
Write-Host “1 – StandardUsersA-C”
Write-Host “2 – StandardUsersD-F”
Write-Host “3 – StandardUsersG-I”
Write-Host “4 – StandardUsersJ-L”
Write-Host “5 – StandardUsersM-O”
Write-Host “6 – StandardUsersP-R”
Write-Host “7 – StandardUsersS-U”
Write-Host “8 – StandardUsersV-W”
Write-Host “9 – StandardUsersX-Z”
Write-Host “”
$mailnumber = read-host -prompt “Please Choose a number”
if ($mailnumber -eq “0″) {
$maildatabase = “MAIL\VIPUsers\VIPUsers”
}
if ($mailnumber -eq “1″) {
$maildatabase = “MAIL\StandardUsersA-C\StandardUsersA-C”
}
if ($mailnumber -eq “2″) {
$maildatabase = “MAIL\StandardUsersD-F\StandardUsersD-F”
}
if ($mailnumber -eq “3″) {
$maildatabase = “MAIL\StandardUsersG-I\StandardUsersG-I”
}
if ($mailnumber -eq “4″) {
$maildatabase = “-MAIL\StandardUsersJ-L\StandardUsersJ-L”
}
if ($mailnumber -eq “5″) {
$maildatabase = “MAIL\StandardUsersM-O\StandardUsersM-O”
}
if ($mailnumber -eq “6″) {
$maildatabase = “-MAIL\StandardUsersP-R\StandardUsersP-R”
}
if ($mailnumber -eq “7″) {
$maildatabase = “MAIL\StandardUsersS-U\StandardUsersS-U”
}
if ($mailnumber -eq “8″) {
$maildatabase = “MAIL\StandardUsersV-W\StandardUsersV-W”
}
if ($mailnumber -eq “9″) {
$maildatabase = “MAIL\StandardUsersX-Z\StandardUsersX-Z”
}
MAIL in this instance is the name of our Exchange mailbox server. The rest is the path the the mailbox databases. You’ll be able to pick your own values out of exchange. The code to populate the $ou variable is very similar.
To run the script from a shortcut, you’ll need to create a shortcut that calls powershell.exe then references the location of the ps1 file. So change the target to something like:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file “C:\location of ps1 file”
You can use unc paths if your wanting to share the script with other people
My PS1 file is below. you’ll need to go through and change the values to suit your own domain. Any questions, post a comment.
Powershell File