######################################################### # # Name: BulkCreate200UsersFromCSV.ps1 # Author: Tony Murray # Version: 1.0 # Date: 29/12/2010 # Comment: PowerShell 2.0 script to # bulk create users from csv file # ######################################################### # Function to test existence of AD object function Test-XADObject() { [CmdletBinding(ConfirmImpact="Low")] Param ( [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, HelpMessage="Identity of the AD object to verify if exists or not." )] [Object] $Identity ) trap [Exception] { return $false } $auxObject = Get-ADObject -Identity $Identity return $true } # Import the Active Directory Powershell Module Import-Module ActiveDirectory -ErrorAction SilentlyContinue # Specify the target OU for new users $targetOU = "OU=Standard Users,OU=_North,DC=North,DC=com" # Find the current domain info $domdns = (Get-ADDomain).dnsroot # for UPN generation # Specify the folder and CSV file to use $impfile = "C:\util\CSV\200Users.csv" # Check if the target OU is valid $validOU = Test-XADObject $targetOU If (!$validOU) { write-host "Error: Specified OU for new users does not exist - exiting...." exit } # Set the password for all new users $password = read-host "Enter password" -assecurestring # Parse the import file and action each line $users = Import-CSV $impFile foreach ($user in $users) { $samname = $user.samaccountname $dplname = $user.displayname $givname = $user.givenname $surname = $user.sn $upname = "$samname" + "@" +"$domdns" New-ADUser –Name $dplname –SamAccountName $samname –DisplayName $dplname ` -givenname $givname -surname $surname -userprincipalname $upname ` -Path $targetou –Enabled $true –ChangePasswordAtLogon $true ` -AccountPassword $password } #All done