-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBulk Create Users.ps1
45 lines (39 loc) · 1.7 KB
/
Bulk Create Users.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<#
This script reads users account data from a .csv file and creates the users, and sets the password.
Chad Thomsen 9/27/2016
#>
#$Password = "XXXXXX"
$OU = "OU=Office,OU=External Users,DC=DOMAIN-NAME,DC=COM"
$datafile = "c:\admin\bulk-users.csv"
#Load Powershell system modules if not already loaded
$LoadedModules = Get-Module | Select-Object -ExpandProperty Name
ForEach ($LoadedModule in $LoadedModules){
If($LoadedModule -eq "ActiveDirectory") {$Loaded = $true}
}
If($Loaded -ne $true){Import-module ActiveDirectory}
#Read Data File in
$Users = Import-Csv -Delimiter "," -Path $datafile
foreach ($User in $Users)
{
$Displayname = $User.Firstname + " " + $User.Lastname
$UserFirstname = $User.Firstname
$UserLastname = $User.Lastname
$UserEmail = $User.Email
$UserPhone = $User.Phone
#$OU = "$User.OU"
$SAM = $UserFirstname + "." + $UserLastname
$UPN = $SAM + "@domain.com"
$Description = "External Production Account"
#$Password = $User.Password
Try{
New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN `
-GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword `
(ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" `
-ChangePasswordAtLogon $false -PasswordNeverExpires $true -EmailAddress "$UserEmail" `
-OfficePhone "$UserPhone"
Write-Host "$SAM account for $Displayname has been created."
}
Catch{
write-host "$Displayname Account already exists."
}
}