-
Notifications
You must be signed in to change notification settings - Fork 0
/
PS_AD_User_Importer.ps1
37 lines (32 loc) · 1.26 KB
/
PS_AD_User_Importer.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
# Path to CSV File
$ADUsers = Import-csv C:\Temp\NewUsers.csv
foreach ($user in $ADUsers) {
$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$Department = $User.department
$OU = $User.ou
# Check to see if user account exists in Active Directory
if (Get-ADUser -Filter {SamAccountName -eq $Username}) {
# If user does exist then output a warning message
Write-Warning "A user account named $Username already exists within Active Directory"
}
else {
# If the user does not exist then create a new user account
# Account will be created in the OU listed in the $OU variable in the CSV file.
# Ensure you change the domain name in the "-UserPrincipalName" variable
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$username@SecureCo.com" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $true `
-ChangePasswordAtLogon $true `
-DisplayName "$Lastname, $Firstname" `
-Department $Department `
-Path $OU `
-AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force)
}
}