-
Notifications
You must be signed in to change notification settings - Fork 4
/
createusers.ps1
61 lines (54 loc) · 2.71 KB
/
createusers.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
# Script to create Active Directory accounts
# v2.1 2/25/2019
# Todd Klindt
# http://www.toddklindt.com
# Blog post, Yay!!!
# Add the Active Directory bits and not complain if they're already there
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
# set default password
# change pass@word1 to whatever you want the account passwords to be
$defpassword = (ConvertTo-SecureString "pass@word1" -AsPlainText -force)
# Get domain DNS suffix
$dnsroot = '@' + (Get-ADDomain).dnsroot
# Import the file with the users. You can change the filename to reflect your file
$users = Import-Csv .\users.csv
foreach ($user in $users) {
if ($user.manager -eq "") # In case it's a service account or a boss
{
try {
New-ADUser -SamAccountName $user.SamAccountName -Name ($user.FirstName + " " + $user.LastName) `
-DisplayName ($user.FirstName + " " + $user.LastName) -GivenName $user.FirstName -Surname $user.LastName `
-EmailAddress ($user.SamAccountName + $dnsroot) -UserPrincipalName ($user.SamAccountName + $dnsroot) `
-Title $user.title -Enabled $true -ChangePasswordAtLogon $false -PasswordNeverExpires $true `
-AccountPassword $defpassword -PassThru `
}
catch [System.Object]
{
Write-Output "Could not create user $($user.SamAccountName), $_"
}
}
else
{
try {
New-ADUser -SamAccountName $user.SamAccountName -Name ($user.FirstName + " " + $user.LastName) `
-DisplayName ($user.FirstName + " " + $user.LastName) -GivenName $user.FirstName -Surname $user.LastName `
-EmailAddress ($user.SamAccountName + $dnsroot) -UserPrincipalName ($user.SamAccountName + $dnsroot) `
-Title $user.title -manager $user.manager `
-Enabled $true -ChangePasswordAtLogon $false -PasswordNeverExpires $true `
-AccountPassword $defpassword -PassThru `
}
catch [System.Object]
{
Write-Output "Could not create user $($user.SamAccountName), $_"
}
}
# Put picture part here.
$filename = "$($user.SamAccountName).jpg"
Write-Output $filename
if (test-path -path $filename)
{
Write-Output "Found picture for $($user.SamAccountName)"
$photo = [byte[]](Get-Content $filename -Encoding byte)
Set-ADUser $($user.SamAccountName) -Replace @{thumbnailPhoto=$photo}
}
}