-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd-ADUsers.ps1
62 lines (55 loc) · 2.29 KB
/
Add-ADUsers.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Import active directory module for running AD cmdlets
Import-Module ActiveDirectory
# Store the data from NewUsersFinal.csv in the $ADUsers variable
$ADUsers = Import-Csv C:\temp\NewUsersReceived.csv -Delimiter ";"
# Define UPN
$UPN = "pwt.local"
# Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {
#Read user data from each field in each row and assign the data to a variable as below
$username = $User.username
$password = $User.password
$firstname = $User.firstname
$lastname = $User.lastname
$initials = $User.initials
$OU = $User.ou #This field refers to the OU the user account is to be created in
$email = $User.email
$jobtitle = $User.jobtitle
$company = $User.company
$department = $User.department
$profilepath = "\\AD01.pwt.local\profile$\%username%"
$homepath = "\\AD01.pwt.local\home$\%username%"
$homedrive = "P:"
# Check to see if the user already exists in AD
if (Get-ADUser -F { SamAccountName -eq $username }) {
# If user does exist, give a warning
Write-Warning "A user account with username $username already exists in Active Directory."
}
else {
# User does not exist then proceed to create the new user account
# Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $username `
-UserPrincipalName "$username@$UPN" `
-Name "$firstname $lastname" `
-GivenName $firstname `
-Surname $lastname `
-Initials $initials `
-Enabled $True `
-DisplayName "$lastname, $firstname" `
-Path $OU `
-Company $company `
-EmailAddress $email `
-ProfilePath $profilepath `
-HomeDirectory $homepath `
-HomeDrive $homedrive `
-Title $jobtitle `
-Department $department `
-ChangePasswordAtLogon $False `
-PasswordNeverExpires $True `
-AccountPassword (ConvertTo-secureString $password -AsPlainText -Force)
# If user is created, show message.
Write-Host "The user account $username is created." -ForegroundColor Cyan
}
}
Read-Host -Prompt "Press Enter to exit"