-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate-ADUserInformation.ps1
58 lines (35 loc) · 2.63 KB
/
Update-ADUserInformation.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
<#
.DESCRIPTION
Import user details from csv and import the values into Active Directory
.EXAMPLE
.\Update-UserInformation $PATHTOCSV
.NOTES
Author: Nathan Stewart
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,HelpMessage='Filepath of CSV')]
[string]$CSV
)
$userinformation = Import-Csv $CSV
foreach ($user in $userinformation) {
#must use LDAP names of each field not the powershell names when using -replace in set-aduser
#if entry in spreadsheet is empty, ignore it
$username = $user.Name
$usersam = Get-ADUser -Filter {name -eq $username} | select -ExpandProperty samAccountName
$hash = @{}
if(!($user.department -eq "")){$hash.Department = $user.department;}
if(!($user.title -eq "")){$hash.Title = $user.title;}
if(!($user.telephoneNumber -eq "")){$hash.telephoneNumber = $user.telephoneNumber;}
#Check if hash is empty
if($hash.Count -gt 0) {
Write-Host "Updating user: $username"
Set-ADUser $usersam -Replace $hash
}
#The manager property requires more logic to update
if(!($user.manager -eq "")){
$managername = $user.Manager
$manager = Get-ADUser -Filter {name -eq $managername} | select -ExpandProperty SamAccountName
Set-ADUser $usersam -Manager $manager
}
}