More on Updating AD user attributes

Welcome Forums General PowerShell Q&A More on Updating AD user attributes

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #6320

    Points: 0
    Rank: Member

    by royse at 2013-04-10 18:03:07

    I'm reading a .csv file that uses the employeeID as a key to find the user.
    The basic code for that is below, although I'm updating 15 attributes and this only show 2 of them.
    This all works well until I hit an empty cell in the .csv.
    What I need it to do is ignore empty cells.
    For example, if the "Work Phone" cell is empty, don't set the officePhone attribute
    All I have come up with so far is to check each cell for the existance of a value and then try to concatenate a command line.
    i.e. if ($str) CommandLine = CommandLine + next attribute
    There has got to be an easier, more efficient way.
    Any suggestions greatly appreciated!

    Import-module ActiveDirectory

    $Users = Get-ADUser -Filter * -Properties EmployeeID

    $UserHash = @{}
    foreach ($User in $Users)
    $UserHash.Add($User.EmployeeID, $User.SamAccountName)

    Clear-Variable Users
    $CSV = Import-Csv -Path '.\Users.csv'
    foreach ($Record in $CSV)
    Set-ADUser -Identity ($UserHash.Item($Record.EmpID)) -OfficePhone $Record."Work Phone" -Add @{otherTelephone = $Record."Extension"

    by ps_gregg at 2013-04-10 20:53:26

    Hi Royse,

    I run a similar process each night with a database that updates AD. Sometimes the database fields are blank so here is an example of the concept I use:

    foreach ($Record in $CSV)
    # Create Hash Table to hold any changes that need to be applied
    $objectattributes = @{}

    #If statement written as multi-line
    if($record.deptNum -ne $null){
    $objectattributes += @{departmentNumber=$($record.deptNum)}

    #If statement written as one-line
    if($record.LOCATION -ne $null){ $objectattributes += @{physicalDeliveryOfficeName=$($record.LOCATION)} }

    # If the $objectattributes contains any data, write it to Active Directory
    if($objectattributes.count -ne 0) {
    Set-ADUser -Identity $user -Replace $objectattributes

    In my script I also check the database value against the attribute value in the AD record before deciding to update. There is no sense in updating if the values are the same.

    Note: pardon me if there are minor errors in what I wrote above, I am working from memory since I don't have my script in front of me — but hopefully this gives you an idea on the concept that I chose to use. If there are better ways to do this I'd like to know too.


    by Slashj at 2013-04-24 05:11:25

    Hi there,

    As far as i know, you dont have to check propertyvalues with "-ne $null" and such.

    If you just want to check if an Cell is empty or not, then i suggest using this one here:

    if($record."Work Phone"){
    Set-ADUser -Identity ($UserHash.Item($Record.EmpID)) -OfficePhone $Record."Work Phone" -Add @{otherTelephone = $Record."Extension"
    "Work Phone is emtpy"

    I also tested it quick on my local machine, this should work for you.


The topic ‘More on Updating AD user attributes’ is closed to new replies.