There's no "telephoneNumber" key in the $props hash. Make sure you're running the correct code.
This will check for empty values. It will also distinguish between a missing user and multiple users from the Get-ADUser. In addition, it won't try to update a user if no properties are present in the CSV (other than the display name -- the script does NOT check to see if the displayname column in the CSV is empty!).
Import-Csv -Delimiter ";" -Path 'C:\ListeUsersGAL V2.csv' -Encoding Default |
ForEach-Object {
$u = Get-ADUser -SearchBase "OU=Users,OU=xxx,DC=xxx,DC=local" -Filter "displayname -eq '$($_.DisplayName)'"
if (-not $u){
# just some very basic exception handling -- you should probably do something more usefu
Write-Host "$($_.DisplayName) was not found"
}
elseif ($u -is [array]) {
# just some very basic exception handling -- you should probably do something more useful
Write-Host "There were $($u.count) users found to have the same displayname: '$($_.DisplayName)"
}
else {
$props = @{
Identity = $u.distinguishedName
}
$update = $false # don't try to update user if no updates are available
if ($_.Title.Trim().Length -gt 1){$props['title'] = $_.Title; $update = $true}
if ($_.telephoneNumber.Trim().length -gt 1){$props['officephone'] = $_.telephoneNumber; $update = $true}
if ($_.Department.Trim().Length -gt 1){$props['department'] = $_.Department; $update = $true}
if ($_.physicalDeliveryOfficeName.Trim().length -gt 1){$props['office'] = $_.physicalDeliveryOfficeName; $update = $true}
Try {
if ($update){
Set-ADUser @props -ErrorAction STOP
}
}
Catch {
# just some very basic exception handling -- you should probably do something more useful
Write-Host "Failed to update user with samAccountName '$($u.samaccountname)'"
Write-Host $_
}
}
}