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 Mail Enabled Users in Bulk with Password verification using PowerShell

This is a simple script that will read the contents of a CSV file and create a new Active Directory User as well as a new Exchange Mailboxes for each object (row in the spreadsheet). You can select which Organizational Unit you want to put the Users in as well as which Exchange Database each users mailbox is created in.

The Spreadsheet is here.

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

# 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 and 2013, Windows 7, Windows Server 2008 / 2008R2 and 2012R2

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

# LINK
# 

# Get the Current Date
$currdate = get-date -format 'yyyyMMdd'

# Define these as you wish 
$csvPath = "$env:HOMEDRIVE\scripts\mailbox.csv"
$logPath = "$env:HOMEDRIVE\scripts\" + 'newMailbox_' + $currdate + '.log'

# Turns on Verbose Messages, they are set to silentlyContinue by default
$VerbosePreference = 'Continue'

##########################################################################
# ConvertTo-PlainText
# Usage   : Decrypts the text generated by "Read-Host -asSecureString"
# Receive : Encrypted string
# Returns : String in plain text 

Function ConvertTo-PlainText {
  param
  (
    [Parameter(Mandatory=$true)][securestring]
    $secure
  )
    $marshal = [Runtime.InteropServices.Marshal] 
    $marshal::PtrToStringAuto($marshal::SecureStringToBSTR($secure)) 
}

##########################################################################

##############################################################################
# get-Password
# Usage   : Prompts for a password twice, compares the answers, and
#         : if they match, the result is returned
#         : If they do NOT match the user is reprompted
# Receive : String for Message, String for Color
# Returns : String answer in plain text 
Function get-Password{
  param
  (
    [Parameter(Mandatory=$true)][string]$PromptMessage,
    [string]$Confirm = 'Y'
  )
  $a = '1'
  $b = '2'

  while($a -ne $b){
    Write-Host -NoNewline -ForegroundColor Cyan "$PromptMessage"
    $PWD1 = Read-Host -asSecureString
    $a = ConvertTo-PlainText -secure ($PWD1)

    if($Confirm -eq 'Y'){
      Write-Host -NoNewline -ForegroundColor Cyan "[CONFIRM] $PromptMessage"
      $PWD2 = Read-Host -asSecureString
      $b = ConvertTo-PlainText -secure ($PWD2)
    }else{
      $PWD2 = $PWD1
      $b = $a
    }

    if($a -ne $b){
      Write-Warning -Message "`n Passwords Do Not Match - Please Try Again ..."
    }

    if($a -eq '' -or $b -eq ''){
      Write-Warning -Message 'Password Cannot be BLANK - Please Try Again ...'
    }
  }
    return $PWD2
}

##############################################################################

$password = get-password -PromptMessage "Default Password:"

Import-Csv -Path "$csvPath" | ForEach-Object {
  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 $_.'MailboxDB' `
  -ResetPasswordOnNextLogon $true
} | out-file -FilePath $logPath -Append

Write-Verbose -Message "Users created, view the log at $logPath"

 

Leave a Reply

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