-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAD - Import Users from CSV.ps1
45 lines (42 loc) · 1.56 KB
/
AD - Import Users from CSV.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
<#
.NOTES
===========================================================================
Created on: 2022-02-18
Created by: Brian Thorp
===========================================================================
.Description
Imports users from a CSV (AD - Export Users to CSV2.ps1)
#>
$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."Common Name"
$Enabled = $User.Enabled
# 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
{
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
# If user is created, show message.
Write-Host "The user account $username is created." -ForegroundColor Cyan
}
}