Update AD users based on csv file

Hi There, 

I am trying to bulk update few users based on CSV file and got the following working script:

$users = Import-Csv "UserDetails.csv"
foreach ($user in $users) {
     $userEmail = $user."Email"
     $UPN = get-qaduser $userEmail | select UserPrincipalName
     set-qaduser -Identity $UPN.UserPrincipalName -Title $user."Title" -Department $user."Department" -StreetAddress $user."Street Address" -City $user."City" -PostalCode $user."Postal Code" 
}
Write-Host "There were " $usersInCSV "accounts in the CSV and the script updated " $count "accounts"

The above script is working fine, however in case any if any attribute is empty in the CSV, it clears out the existing value too. E.g. if I don't set CITY in CSV, it clears the existing value too. I know in Powershell, there is a command update-aduser which can help with this issue. Is there any command something similar in OneIdentity too?

Thanks in Advance

  • I got the working workaround. 

    Added if (!($user."Department" )) statement before the set-qaduser. 

    Thanks everyone 

  • I put together the script below to give you a different way of setting these properties.  Instead of the switches you are using above, the script creates a collection of <property>=<value> pairs and passes them to the -ObjectAttributes switch.  It checks to see if the property has a value to be passed or not.  If it doesn't, then it excludes the property from the information being passed to -ObjectAttributes  It may not be the best implementation but it works.

    It assumes that the input file has a username field in the header but you can change the name of the field in the file that identifies the user by changing the variable '$UserIdentityProperty'

    $Users = import-csv .\Sample_Input.txt

    # The next line tells the code that the user is identified by the 'Username' field in each record
    # The assumption is that this will never be blank

    $UserIdentityProperty = "Username"

    foreach ($User in $Users)
    {

    # Initialize a table to hold the <property> <value> pairs

    $PropertyTable = @{}

    # Get a list of the properties in the current user record
    # This allows the code to deal with any input file
    # A static list of properties could also be defined
    # to replace '$User | Get-Member -MemberType NoteProperty | select -ExpandProperty Name'

    $User | Get-Member -MemberType NoteProperty | select -ExpandProperty Name | foreach {

    $CurrentPropertyName = $_

    # Get the contents of the property from the current User record from the file

    $CurrentPropertyContents = $($User.$CurrentPropertyName)

    # Check if the current property is not blank and that it is not the user identifier
    # Note that we don't want the user identifier among the properties to be set

    If (($CurrentPropertyContents) -and ($CurrentPropertyName -ne $UserIdentityProperty))
    {

    # This adds <property> = <value> to the $PropertyTable
    # This list of property and value pairs will then be sent to the Set- command

    $PropertyTable.Add($CurrentPropertyName,$CurrentPropertyContents)

    }

    $CurrentPropertyContents = $null

    }

    # Apply the non-blank <property> = <value> pairs to the user object

    #Set-QADUser -Identity $($User.$UserIdentityProperty) -ObjectAttributes $PropertyTable

    $PropertyTable | fl


    # Clear the property table to get ready for the next user record

    $PropertyTable = $null

    }