Monday, July 05, 2010

Multi-User quick create Powershell form for Exchange 2007

One of the more mind numbing tasks that Exchange administrators perform is creating new users and mailboxes. If you've ever been asked to create 5-10 users at the same time and are staring at a number of very repedative mouse clicks especially if you detests wizards then this script can help out. This is version 2 of this script i created a number of years ago that allowed creating just one user at at time this improves on this original idea by using a Datagrid instead so allowing you to enter multiple users and being able to create the users with just one click (well hopefully). Its also quite usefull in development enviroments where you might want to create a large number of users at once. The script itself is fairly simple in that it just wrappers a simple Winform around the create mailbox command and users a Datagrid for multiline input.

I've put a download of the code here the script itself looks like


Function createmailbox{
foreach($row in $dgDataGrid.rows){
if ($row.Cells[0].Value -ne $null){
$psSecurePasswordString = new-object System.Security.SecureString
foreach($char in $row.Cells[5].Value.ToCharArray())
$result = New-mailbox -UserPrincipalName $row.Cells[0].Value -alias $row.Cells[4].Value -database $MBhash1[$msMailStoreDrop.SelectedItem.ToString()] `
-Name $row.Cells[3].Value -OrganizationalUnit $OUhash1[$ouOuNameDrop.SelectedItem.ToString()] -password $psSecurePasswordString `
-FirstName $row.Cells[1].Value -LastName $row.Cells[2].Value -DisplayName $row.Cells[3].Value
if($result -ne $null){
[]::Show("Mailbox " + $result + " Created")
[]::Show("Error createing " + $row.Cells[0].Value + " check cmdline")



$OUhash1 = @{ }
$MBhash1 = @{ }

$form = new-object System.Windows.Forms.form
$form.Text = "Exchange 2007 Quick User Create Form"
$form.size = new-object System.Drawing.Size(800,800)

$msTable = New-Object System.Data.DataTable

$msTable.TableName = "GroupName"

# Add DataGrid View

$dgDataGrid = new-object
$dgDataGrid.Location = new-object System.Drawing.Size(10,10)
$dgDataGrid.size = new-object System.Drawing.Size(750,200)
$dgDataGrid.AutoSizeRowsMode = "AllHeaders"

$dgDataGrid.DataSource = $msTable

# Add OU Drop Down
$ouOuNameDrop = new-object System.Windows.Forms.ComboBox
$ouOuNameDrop.Location = new-object System.Drawing.Size(100,260)
$ouOuNameDrop.Size = new-object System.Drawing.Size(230,30)
$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.Filter = '(objectClass=organizationalUnit)'
$searcher1 = $searcher.FindAll()
foreach ($person in $searcher1){
[string]$ent = $person.Properties.canonicalname

# Add OU DropLable
$ouOuNamelableBox = new-object System.Windows.Forms.Label
$ouOuNamelableBox.Location = new-object System.Drawing.Size(10,260)
$ouOuNamelableBox.size = new-object System.Drawing.Size(100,20)
$ouOuNamelableBox.Text = "OU Name"

# Add Server Drop Down
$snServerNameDrop = new-object System.Windows.Forms.ComboBox
$snServerNameDrop.Location = new-object System.Drawing.Size(100,290)
$snServerNameDrop.Size = new-object System.Drawing.Size(130,30)
get-mailboxserver | ForEach-Object{$snServerNameDrop.Items.Add($_.Name)}
get-mailboxdatabase -Server $snServerNameDrop.SelectedItem.ToString()| ForEach-Object{$msMailStoreDrop.Items.Add($_.Name)
$MBhash1.add($_.Name,$_.ServerName + "\" + $_.StorageGroup.Name + "\" + $_.Name)

# Add Server DropLable
$snServerNamelableBox = new-object System.Windows.Forms.Label
$snServerNamelableBox.Location = new-object System.Drawing.Size(10,290)
$snServerNamelableBox.size = new-object System.Drawing.Size(100,20)
$snServerNamelableBox.Text = "ServerName"

# Add MailStore Drop Down
$msMailStoreDrop = new-object System.Windows.Forms.ComboBox
$msMailStoreDrop.Location = new-object System.Drawing.Size(100,320)
$msMailStoreDrop.Size = new-object System.Drawing.Size(130,30)

# Add MailStore DropLable
$msMailStorelableBox = new-object System.Windows.Forms.Label
$msMailStorelableBox.Location = new-object System.Drawing.Size(10,320)
$msMailStorelableBox.size = new-object System.Drawing.Size(100,20)
$msMailStorelableBox.Text = "Mail-Store"

# Add Create Button

$crButton = new-object System.Windows.Forms.Button
$crButton.Location = new-object System.Drawing.Size(10,360)
$crButton.Size = new-object System.Drawing.Size(150,23)
$crButton.Text = "Create Mailboxs"

$form.topmost = $true


YE MIN WIN said...

Anyway to copy and paste the filed from excel spreed sheet to data grid?
How to put the other attribute such as company field at data grid.

Thanks a lot.

Anonymous said...

Can this made to work in Exchange 2010.

Now it says it does not understand the database name as the script gives it in the form of server\database

(even thought ms documents say it sould work it does not. if i manually run new-mailbox I cannot get it to work when giving server name. Without works fine.)