-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAD - Create Users from CSV Mk2.ps1
75 lines (64 loc) · 2.14 KB
/
AD - Create Users from CSV Mk2.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
63
64
65
66
67
68
69
70
71
72
73
74
75
<#
.NOTES
===========================================================================
Created on: 2022-02-18
Created by: Brian Thorp
===========================================================================
CSV needs the following headers:
GivenName
Surname
SamAccountName
DisplayName
Password
UserPrincipalName
OU
Enabled
.Description
Imports users from a CSV file and creates them in AD
#>
$ImportedUsers = Import-CSV C:\ContosoTemp\ADUsers.csv
foreach ($User in $ImportedUsers)
{
# Get usable variables for each csv row
$FirstName = $User.GivenName
$LastName = $User.Surname
$UserName = $User.SamAccountName
$DisplayName = $User.DisplayName
$Password = $User.Password
$UPN = $User.UserPrincipalName
$OU = $User.OU
$Enabled = $User.Enabled
$OUExists = [adsi]::Exists("LDAP://$OU")
# Write-Host "$OU"
# Write-Host "OU Exists: $OUExists"
# Check to see if user exists already
if (Get-ADUser -Filter { SamAccountName -eq $UserName } )
{
Write-Warning "A user account with username $username already exists in Active Directory."
}
else
{
if ($OUExists)
{
Write-Host -ForegroundColor Green " Information: OU Exists"
New-ADUser `
-SamAccountName $username `
-UserPrincipalName "$username@$UPN" `
-Name "$firstname $lastname" `
-GivenName $firstname `
-Surname $lastname `
-Initials $initials `
-Enabled $True `
-DisplayName $DisplayName `
-AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True `
-Path $OU #`
#-Whatif
# If user is created, show message.
Write-Host "The user account $username is created." -ForegroundColor Cyan
}
else
{
Write-Warning "OU Doesnt exist ($OU) so we're skipping $username -- check your CSV!!"
}
}
}