Set-ADUser cmdlet failing

I have some code and a csv file where I'm trying to bulk update users in AD.

$cred = Get-Credential
$csv = '.\UpdateUsers - Copy.csv'

$userlist = Import-Csv $csv
$userlist | foreach {
    Set-ADUser -Identity $_.SamAccountName -EA stop -City $_.City -Company $_.Company -Country $_.Country -Department $_.Department -Description $_.Description -DisplayName $_.DisplayName -EmailAddress $_.EmailAddress -EmployeeID $_.EmployeeID -EmployeeNumber $_.EmployeeNumber -Fax $_.Fax -GivenName $_.GivenName -MobilePhone $_.MobilePhone -Office $_.Office -OfficePhone $_.OfficePhone -POBox $_.POBox -PostalCode $_.PostalCode -SamAccountName $_.SamAccountName -State $_.State -StreetAddress $_.StreetAddress -Surname $_.Surname -Title $_.Title -UserPrincipalName $_.UserPrincipalName -Manager $_.manager -Credential $cred
}
The command succeeds on most of the users in the csv file.  It seems to fail, however, when it comes across an attribute that is null. For example, there are users in the csv file that have no value for the OfficePhone attribute.  How would I handle the script allowing null values for any of these properties?

September 14th, 2015 7:03pm

Yup. You have to filter out null attributes

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 7:17pm

Load like this and splat the result:

$p=@{
Identity=$_.SamAccountName
ErrorAction='Stop'

} if($_.City){$p.City=$_.City} if($_.Company){$p.Company=$_.Company} if($_.Country){$p.Country=$_.Country} if($_.Country){$p.Department=$_.Department} #... etc Set-AdUser @p

September 14th, 2015 7:23pm

That was the ticket. Thanks! Whatever spatting is, haha. Wish I could follow that logic.
Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 10:04pm