Skip to main content

Scripts to Create users at Active directory from CSV file and create their Mailboxes on Exchange server 2010

 When you are deploying a new infrastructure and the company request to create about 300 users it's very difficult and time consuming to create all of them by using GUI but now we are learing you how to create any number of users with a few commands and a sheets that you already prepare them before visiting the site for deployment process.

It's very simple to do, Just write down the required users on a file like below:


Copy the below stream onto a txt file and rename the extension onto .CSV as "LabUsers.csv"
Code:
GivenName,Surname,samAccountName,DisplayName,Password
Mohamed,Fouad,Mohamed.Fouad,Mohamed Fouad,EgyEng.comPassword1
Mohamed,Taha,Mohamed.Taha,Mohamed Taha,EgyEng.comPassword1
Mohamed,Hassan,Mohamed.Hassan,Mohamed Hassan,EgyEng.comPassword1
Mohamed,Ahmed,Mohamed.Ahmed,Mohamed Ahmed,EgyEng.comPassword1
Mohamed,Ali,Mohamed.Ali,Mohamed Ali,EgyEng.comPassword1
Mohamed,Emad,Mohamed.Emad,Mohamed Emad,EgyEng.comPassword1
after completing the users list then you need the script to run for creating the users.
You can create a new Organization Unit (OU) which named ExchangeUsers
and after running the script you can move the users easily to desired OUs.


Just copy the below stream onto a txt file and rename the extension to .ps1

Code:
# Powershell Script which creates users from a sample CSV File into Active Directory
# Version 1.0
# Author: Mohamed Fouad
# http://www.EgyEng.com
# 
# Compatible with:
# Powershell 1.0 and 2.0
# Windows 2003
# Windows 2008
# Windows 2008 R2
#

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=ExchangeUsers"

$domain = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$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 ""
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 From Egypt Engineers-EgyEng.com")
    $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"
Now open the windows powershell and point to the location for the ps1 file and open it, it will ask you to open the "LabUsers.csv".
Open it and give it a time to complete in a minute.

Check the OU and you will find the created users in place.

Now, you want to create the mailboxes for these users in Exchange 2010.
So it's a very simple command to be run at your exchange Management shell after elevation.

One step remaining before doing your command is to create a new database on the exchange server with a name as example "EgyEng.comDB"

THEN you are ready for the command as below:

Code:
[PS] C:\Documents and Settings\EgyEngadmin>Get-User -OrganizationalUnit ExchangeUsers | Where-Object {$_.RecipientType -eq "user"} | Enable-Mailbox -Dat
abase "EgyEng.comDB"
Great, now you create domain users and their exchange mailboxes in two minutes!.

Note: This applies to Windows 2003,2008 , Exchange 2007, 2010.

have a nice Day!
Egypt Engineers
EgyEng.com.

Comments

Popular posts from this blog

Recreating a missing VMFS datastore partition in VMware vSphere 5.x and 6.x

    Symptoms A datastore has become inaccessible. A VMFS partition table is missing.   Purpose The partition table is required only during a rescan. This means that the datastore may become inaccessible on a host during a rescan if the VMFS partition was deleted after the last rescan. The partition table is physically located on the LUN, so all vSphere hosts that have access to this LUN can see the change has taken place. However, only the hosts that do a rescan will be affected.   This article provides information on: Determining whether this is the same problem Resolving the problem   Cause This issue occurs because the VMFS partition can be deleted by deleting the datastore from the vSphere Client. This is prevented by the software, if the datastore is in use. It can also happen if a physical server has access to the LUN on the SAN and does an install, for example.   Resolution To resolve this issue: Run the  partedUtil  command on the host with the issues and verify if your output

ما هى ال FSMO Roles

  بأختصار ال FSMO Roles هى اختصار ل Flexible Single Operation Master و هى عباره عن 5 Roles فى ال Active Directory و هما بينقسموا لقسمين A - Forest Roles 1- Schema Master Role و هى ال Role اللى بتتحكم فى ال schema و بيكون فى Schema Master Role واحد فى ال Forest بيكون موجود على Domain Controller و بيتم التحكم فيها من خلال ال Active Directory Schema Snap in in MMC بس بعد ما يتعمل Schema Register بواسطه الامر التالى من ال Cmd regsvr32 schmmgmt.dll 2-Domin Naming Master و هى ال Role المسئوله عن تسميه ال Domains و بتتأكد ان مفيش 2 Domain ليهم نفس الاسم فى ال Forest و بيتم التحكم فيها من خلال ال Active Directory Domains & Trusts B- Domain Roles 1-PDC Emulator و هى ال Role اللى بتتحكم فى ال Password change فى ال domain و بتتحكم فى ال time synchronization و هى تعتبر المكان الافتراضى لل GPO's و هى تعتبر Domain Role مش زى الاتنين الاولانيين و بيتم التحكم فيها من خلال ال Active directory Users & Computers عن طريق عمل كليك يمين على اسم الدومين و نختار operations master فى تاب ال PDC Emu

Unlock the VMware VM vmdk file

  Unlock the VMware VM vmdk file Kill -9 PID Sometimes a file or set of files in a VMFS become locked and any attempts to edit them or delete will give a device or resource busy error, even though the vm associated with the files is not running. If the vm is running then you would need to stop the vm to manipulate the files. If you know that the vm is stopped then you need to find the ESX server that has the files locked and then stop the process that is locking the file(s). 1. Logon to the ESX host where the VM was last known to be running. 2.  vmkfstools -D /vmfs/volumes/path/to/file  to dump information on the file into /var/log/vmkernel 3.  less /var/log/vmkernel  and scroll to the bottom, you will see output like below: a. Nov 29 15:49:17 vm22 vmkernel: 2:00:15:18.435 cpu6:1038)FS3: 130: <START vmware-16.log> b. Nov 29 15:49:17 vm22 vmkernel: 2:00:15:18.435 cpu6:1038)Lock [type 10c00001 offset 30439424 v 21, hb offset 4154368 c. Nov 29 15:49:17 vm22 vmkernel: gen 664