Quickly Create Test Accounts

When looking to verify a process like mailbox migrations, sometimes a significant number of test accounts are required. You could go about it in the old fashioned (manual) way of creating each account one by one, or…

You can use PowerShell!

There are a few things required before beginning.

1. Create a .csv file with a few basic headers and some values.

This list is what will be used to create all the accounts needed. For example:

1-CSV

2. Open Notepad.exe and add this information in. 

$ErrorActionPreference = “SilentlyContinue”

function Select-FileDialog
{
 param([string]$Title,[string]$Directory,[string]$Filter=”CSV Files (*.csv)|*.csv”)
 [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) | Out-Null
 $objForm = New-Object System.Windows.Forms.OpenFileDialog
 $objForm.InitialDirectory = $Directory
 $objForm.Filter = $Filter
 $objForm.Title = $Title
 $objForm.ShowHelp = $true
 
 $Show = $objForm.ShowDialog()
 
 If ($Show -eq “OK”)
 {
  Return $objForm.FileName
 }
 Else
 {
  Exit
 }
}

$FileName = Select-FileDialog -Title “Import an CSV file” -Directory “c:\”

$ExchangeUsersOU = “OU=TestUsers”

$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()  
$DomainDN = (([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).Domains | ? {$_.Name -eq $domain}).GetDirectoryEntry().distinguishedName
$final = “LDAP://$DomainDN”
$DomainPath = [ADSI]”$final”
$cOU = $DomainPath.Create(“OrganizationalUnit”,$ExchangeUsersOU)
$cOU.SetInfo()

$UserInformation = Import-Csv $FileName

$OUPath = “LDAP://$ExchangeUsersOU,$DomainDN”
$UserPath = [ADSI]”$OUPath”
Write-Host “—————————————————————“
Write-Host “Creating LAB Users”
Write-Host “Version 1.1”
Write-Host “—————————————————————“

Foreach ($User in $UserInformation){
 
 $CN = $User.samAccountName
 $SN = $User.Surname
 $Given = $User.givenName
 $samAccountName = $User.samAccountName
 $Display = $User.DisplayName
 
 $LABUser = $UserPath.Create(“User”,”CN=$CN”)
 Write-Host “Creating User: $User.samAccountName”
 $LABUser.Put(“samAccountName”,$samAccountName)
 $LABUser.Put(“sn”,$SN)
 $LABUser.Put(“givenName”,$Given)
 $LABUser.Put(“displayName”,$Display)
 $LABUser.Put(“mail”,”$samAccountName@$domain”)
 $LABUser.Put(“description”, “Lab User – created via Script”)
 $LABUser.Put(“userPrincipalName”,”$samAccountName@$domain”)
 $LABUser.SetInfo()
 
 $Pwrd = $User.Password
 
 $LABUser.psbase.invoke(“setPassword”,$Pwrd)
 $LABUser.psbase.invokeSet(“AccountDisabled”,$False)
 $LABUser.psbase.CommitChanges()

}
Write-Host “Script Completed”

3.  When finished, save as “NEWLABUSERS.ps1”

This script will create new accounts in Active Directory based on the .CSV file in step 1.  The accounts will be created in an OU called “OU=TestUsers”.

4.   To create corresponding Exchange mailboxes for these test users, Run the following cmdlet from the Exchange Management Shell;

Get-User -OrganizationalUnit “OU=TestUsers,DC=domain,DC=com” -Filter ‘RecipientType -eq “user”‘ | ForEach-Object { $email = $_.FirstName +”.”+ $_.LastName +”@domain.com“; Enable-Mailbox -Identity $_.SamAccountName -DisplayName $_.DisplayName -Alias $_.SamAccountName -PrimarySmtpAddress $email -Database “ExchMBXServer\SG1\database“}

*NOTE: on each variable that has a strikethrough above, change the variable to ones within your organization.  “OU=TestUsers,DC=contoso,DC=com” and “@contoso.com” and “ExchMBX01\First Storage Group\defaultdatabase”

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s