Disclaimer
This is my personal blog. The opinions and views I express are my own. The information I provide is on an as-is basis. I make no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this blog and will not be liable for any errors, omissions, or delays in this information or any losses, injuries or damages arising from its use.

Create Multiple Mailboxes with CSV input (Powershell)

** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK **

The script below will take input from the link csv file and create mailboxes on your Exchange 2010 Server. You can run it from the server itself or any machine with Exchange Management Tools installed.

I have it setup to run from the root of the c:\ but you can change the $csvPath and $logpath to whatever folder structure. You will also need to change the $userDB and mailbox names to match you infrastructure.

I tested this script with Exchange 2010, Windows 7, Windows Vista, Windows Server 2003, Windows Server 2K8 and 2K8 R2.

mailbox-example-csv

<#
	** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK **	

    .SYNOPSIS
	    Create new mailboxes in Exchange 2010 and the corresponding user in Active Directory.

	.DESCRIPTION
	    Use the New-Mailbox cmdlet to create mailboxes. The script will prompt you for the default password for all users. 
        All accounts are set to prompt the user to change the password at first logon. It then prompts for what OU you want to put the users in, 
        change the OU variable AD paths and variable names to correspond to your AD structure.  
        Once the users are created a log file is written to the $logPath.

    .REQUIREMENTS
        1.	If you run this script from somewhere other than Exchange server you will need Exchange Management tools installed.
        2.	The mailbox.csv file filled out.
        3.	The proper Exchange RBAC role to create mailboxes.
        4.  The CSV file will need the following columns; Display Name, sAMAccountName, UserPrincipalName, First Name, Last Name

    .NOTES
        Tested with Exchange 2010, Windows 7, Windows Vista, Windows Server 2003, Windows Server 2K8 and 2K8 R2

	.AUTHOR
		David Hall | https://www.signalwarrant.com/

	.LINK

#>

# Supply the password for all the newly created accounts
$password = read-host "Enter Default Password for all accounts created" -AsSecureString
$ou = ""

# Define these as you wish 
$csvPath = "C:\mailbox.csv"
$userDB = "Mailbox1297499996"
$logPath = "c:\log.txt"

# When you change the OU and associated variable names be sure to also change
# the cooresponding variables below in the OU selection code block
$hq = "mydomain.com/HQ/Users"
$miami = "mydomain.com/Miami/Users"

# OU Selection Error Message
$errorMessage = "*** INVALID ENTRY ***"

# OU Selection message
$selectionChoice = Write-Host -foregroundcolor Cyan "`n Choose from the following OUs: `n" `
"1 = HQ `n 2 = Miami`n" `

# OU input message
$ouSelection = Read-Host "Which OU"

# *** Start OU Selection Code Block

$ou = $ouSelection

if(!($ou)) {
	Throw " NO OPTION SELECTED "
	Exit
}

Switch($ou) {
	1 {$ou = $HQ}
	2 {$ou = $Miami}
	default {
    $errorMessage
    Exit
    }
}
# *** End OU Selection Code Block

Import-CSV "$csvPath" | ForEach {
New-Mailbox `
-Password $password `
-Name $_.'Display Name' `
-Alias $_.'sAMAccountName' `
-OrganizationalUnit $ou `
-sAMAccountName $_.'sAMAccountName' `
-FirstName $_.'First Name'`
-LastName $_.'Last Name'`
-DisplayName $_.'Display Name' `
-UserPrincipalName $_.'UserPrincipalName' `
-Database $userDB `
-ResetPasswordOnNextLogon $true
} | out-file $logPath -append

Write-Host -foregroundcolor Cyan "Users created, view the log at $logPath"

Exit

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.