none
Unable to import empty field from csv using Active directory powershell RRS feed

  • Question

  • We are using below AD PowerShell script to update/import user attributes using csv file. But when there is empty value for specific attribute for user in csv file it will failed with below error.

    We are running Windows 2003 DC, but running the script on windows 7 RSAT tool installed

    Error:

    Set-ADUser : replace
    At line:1 char:30
    + $USERS | Foreach { Set-ADUser <<<<  -Identity $_.samaccountname -Replace @{Department = "$($_.Department)"}}
        + CategoryInfo          : InvalidOperation: (testuseropeneir:ADUser) [Set-ADUser], ADInvalidOperationException
        + FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser

    Script:

    Import-Module ActiveDirectory

    $USERS = Import-CSV D:\temp\users.csv

    $USERS | Foreach {Set-ADUSer -Identity $_.samaccountname -l $_.l -title $_.title -postalCode $_.postalCode -department $_.department -streetAddress $_.streetAddress -mobile $_.mobile -manager $_.manager}

    Sample CSV file:

    # Mobile filed is empty

    SamAccountName,l,title,postalCode,department,streetAddress,mobile,manager
    testuser1,net,Engineer,T12345,Sales,"North Quarter, John's Road",,testuser2

    Tuesday, May 1, 2018 12:30 PM

Answers

  • Using PowerShell to update users from a CSV can be relatively easy, but it is tricky to handle exceptions, like missing fields. Code is needed to check every field in the CSV. If it helps, I coded a PowerShell script to update users from a CSV some time ago here:

    https://gallery.technet.microsoft.com/Update-Active-Directory-cd5c5513#content

    I attempt to handle most things that can go wrong, especially blank or missing values. The script also captures and logs all errors. The description lists the steps to use the script, and lists the limitations (it cannot rename users, and it can only replace completely or clear multi-valued attributes).


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by jrv Tuesday, May 1, 2018 5:01 PM
    • Marked as answer by MAK_Mahesh Friday, May 4, 2018 1:20 PM
    Tuesday, May 1, 2018 3:01 PM
    Moderator

All replies

  • You must test for missing values in the CSV, and either skip them or use the -Clear parameter of Get-ADUser to clear the attribute. AD does not allow you to assign empty strings or missing values to attributes.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by jrv Tuesday, May 1, 2018 5:01 PM
    Tuesday, May 1, 2018 12:39 PM
    Moderator
  • Using PowerShell to update users from a CSV can be relatively easy, but it is tricky to handle exceptions, like missing fields. Code is needed to check every field in the CSV. If it helps, I coded a PowerShell script to update users from a CSV some time ago here:

    https://gallery.technet.microsoft.com/Update-Active-Directory-cd5c5513#content

    I attempt to handle most things that can go wrong, especially blank or missing values. The script also captures and logs all errors. The description lists the steps to use the script, and lists the limitations (it cannot rename users, and it can only replace completely or clear multi-valued attributes).


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by jrv Tuesday, May 1, 2018 5:01 PM
    • Marked as answer by MAK_Mahesh Friday, May 4, 2018 1:20 PM
    Tuesday, May 1, 2018 3:01 PM
    Moderator
  • Thanks Richard.. I will try this script..
    Friday, May 4, 2018 1:20 PM