-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_user_cn.ps1
37 lines (33 loc) · 1.13 KB
/
change_user_cn.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
#change_user_displayname.ps1
#
# Synopsis
#
# Get all Users from an active directory organisation unit and
# change display name, distinguished name and companyname
#
#
################################################################
#Variables - edit as required
################################################################
$container = "OU=ouname,DC=domain,DC=local"
$company = "Your Comany Name here"
$emaildomain = "yourcompany.org"
################################################################
#Read users into variable.
$users = get-aduser -filter * -SearchBase $container
#Process users.
Foreach ($user in $users) {
$newDN = $user.surname + ", " + $user.GivenName
$email=$user.GivenName+"."+$user.surname+"@"+$emaildomain
$email=$email.ToLower()
#Change display name, and company name
set-aduser $user -Displayname $newDN -company $company -ErrorAction SilentlyContinue
Set-ADUser -Identity $user -EmailAddress $email
#Change distinguished name
Try {
Rename-ADObject -identity $user -Newname $newDN
}
Catch {
Write-Host "$user may already exist."
}
}