Creating Multiple AD Users,mailbox, updating Attributes and member of Groups

Powershell Script Creating Multiple  Active directory users,email address,updating Attributes and adding user to multiple security and distribution groups.

Exchange 2016 and Active directory does not goes well with new user account creation. you cannot duplicate or copy  a user account to replicate attributes for a new joiner account. The Only Option is to create the account manually and update it. It is easy to create single user but it becomes time consuming and extremely difficult for creating multiple user accounts without any error.

I decided to use Powershell script which can automate the process without much manual work. This script helps to create a user account, email address, update the attributes and add user to member of security and distribution groups.

Download the Excel file, update the fields with necessary information and save to temp folder.

Copy the below powershell code to notepad and save it as .PS1 file.

# Creating Mailbox on Hybrid Model Exchange environment / updating Attributes / member of Groups

[Parameter( Mandatory=$false)]
$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$URL/PowerShell/ -Authentication Kerberos -Credential $UserCredential

Import-PSSession $Session

write-Host ‘connected to Exchange Server …….’ -NoNewline -ForegroundColor Green

$CSVLocation = “C:\temp\createUserMailbox.csv”

### Step 1 – User Creation
write-Host ‘Creating User Mailbox …….’ -NoNewline -ForegroundColor Green

Import-CSV $CSVLocation | ForEach-Object {
New-RemoteMailbox -Name $_.Name -FirstName $_.FirstName -Initials $_.Initials -Lastname $_.LastName -UserPrincipalName $_.UPN -OnPremisesOrganizationalUnit $_.OU -Password (ConvertTo-SecureString $_.password -AsPlainText -Force) -ResetPasswordOnNextLogon $false

write-Host ‘user account created …….’ -ForegroundColor Green

Start-Sleep -s 10

### Step 2 – user attributes update
# Import AD Module
Import-Module ActiveDirectory

write-Host ‘Starting to update AD Attributes…….’ -ForegroundColor Yellow
# Import CSV into variable $users

$users = Import-Csv -Path C:\temp\createUserMailbox.csv
# Loop through CSV and update users if the exist in CVS file

foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter “SamAccountName -eq ‘$($user.samaccountname)'” -Properties * -SearchBase “DC=ittechsolution,DC=com” |
Set-ADUser -Office $($user.Office) -OfficePhone $($user.officePhone) -MobilePhone $($user.MobilePhone) -fax $($user.fax) -Title $($user.Title) -Department $($user.Department) -Company $($user.Company) -Manager $($user.Manager) -StreetAddress($user.StreetAddress) -City $($user.City) -State $($user.State) -PostalCode $($user.PostalCode) -Replace @{IPPhone=$User.IPPhone} -add @{OtherTelephone=$user.OtherTelephone}

$users = Import-Csv -Path C:\temp\createUserMailbox.csv
foreach($user in $users){
#SAMAccountName should be in CSV
Set-ADUser $user.samaccountname -Country $
write-Host ‘AD Attributes updated …….’ -ForegroundColor Yellow

Adding user to groups ( security group and distribution group)

write-Host ‘Adding user to Groups …….’ -ForegroundColor Yellow
$Items = Import-Csv -Path C:\temp\createUserMailbox.csv
$Items | ForEach-Object {
$SamAccountName = $_.SamAccountName
$Groups = ($_.Groups).split(“,”)
foreach ($Group in $Groups)
“Adding $SamAccountName to the following group: $Group”
Add-ADGroupMember -Identity $Group $SamAccountName

Write-Host ‘done!’ -ForegroundColor Green

Hope this post has helped you, please do comment below and share it.


Post Comment

CommentLuv badge