none
Changing Multiple User Attributes in AD RRS feed

  • General discussion

  • This topic has been discussed in many other forums but I can't seem to make it work for my scenario. I have a CSV file with three categories. Name, OfficePhone, and MobilePhone. The csv file contains those users alongside the corresponding numbers. From here, I would like to be able to update all users on this list with their phone numbers. Here is an example of what it looks like (NOTE THAT SOME PEOPLE HAVE PHONE NUMBERS MISSING)

    I have tried writing different scripts and just can't seem to get any of them to work for various reasons. Also important, not all users are in the same OU.

    Here is one of the scripts I have created.

    Import-Csv C:\Users\admin\Desktop\PhoneNumber.csv | % {
    $Name = $_.Name
    $Users = Get-ADUser -Filter {name -eq $Name} -Properties OfficePhone, MobilePhone
    $Users = $Users.Name
    $Phone = $_.Telephone
    $Mobile = $_.Mobile
    
    foreach($user in $Users){
    Set-ADUser  $User -OfficePhone $Phone -MobilePhone $Mobile
    }
    }

    Can someone help?

    Thanks,

    Cullen

    • Changed type Bill_Stewart Wednesday, September 13, 2017 9:50 PM
    • Moved by Bill_Stewart Wednesday, September 13, 2017 9:50 PM User said they solved own problem (not sure how)
    Tuesday, August 1, 2017 5:48 PM

All replies

  • You don't need Get-AdUser.  Just loop the Csv with Set-AdUser.

    Using Name=name will not get you the correct users if there are two or more users with the same name.  Use SamAccountName or narrow the focus to on OU.

    Also, why would you set the user numbers to the settings they are already set to.

    Import-Csv C:\Users\admin\Desktop\PhoneNumber.csv | 
    	ForEach-Object{
    		Get-ADUser -Filter "Name -eq '$($_.Name)'" |
    			Set-ADUser -OfficePhone $_.TelePhone -MobilePhone $_.Mobile
    	}


    \_(ツ)_/

    Tuesday, August 1, 2017 6:08 PM
  • You don't need Get-AdUser.  Just loop the Csv with Set-AdUser.

    Using Name=name will not get you the correct users if there are two or more users with the same name.  Use SamAccountName or narrow the focus to on OU.

    Also, why would you set the user numbers to the settings they are already set to.

    Import-Csv C:\Users\admin\Desktop\PhoneNumber.csv | 
    	ForEach-Object{
    		Get-ADUser -Filter "Name -eq '$($_.Name)'" |
    			Set-ADUser -OfficePhone $_.TelePhone -MobilePhone $_.Mobile
    	}


    \_(ツ)_/

    So will this work without using Samaccountname as long as there isn't more than one user with the same name?

    Sorry I wasn't specific. I meant that some people don't have a phone number to add to Active Directory. Some people only have 1 number to add, and some have 0 to add.

    Tuesday, August 1, 2017 6:11 PM
  • Sorry I wasn't specific. I meant that some people don't have a phone number to add to Active Directory. Some people only have 1 number to add, and some have 0 to add.

    That is a completely different question. You will have to script for the exceptions. If a parameter is null you cannot set it. That is a bit more programming that you will need to do.


    \_(ツ)_/

    Tuesday, August 1, 2017 6:22 PM
  • Ok regardless, the script you provided doesn't work. Do I need samaccountname for certain?
    Tuesday, August 1, 2017 6:50 PM
  • What do you mean it mean "it doesn't work"? That tells us nothing.  What doesn't work.  What errors? Remember that we cannot see your screen.


    \_(ツ)_/

    Tuesday, August 1, 2017 7:08 PM
  • What do you mean it mean "it doesn't work"? That tells us nothing.  What doesn't work.  What errors? Remember that we cannot see your screen.


    \_(ツ)_/

    This error appears for each user. I blocked out potentially sensitive info
    Tuesday, August 1, 2017 7:12 PM
  • Please do not post screenshots.  They are not readable in most browsers.

    You cannot set a user property to a null value.  You must code around the empty fields.  Test each field in the CSV before applying it.  Only apply fields that have values.


    \_(ツ)_/

    Tuesday, August 1, 2017 7:15 PM
  • Ok understandable. Also I'm getting an error because these users are not in the root directory. The script isn't searching other OUs which is where some of the users are located. How can I get around that?
    Tuesday, August 1, 2017 7:34 PM
  • get-AdUser searches ALL of AD.

    You cannot use "Name" with a display name. It must be the CN attribute of the object.


    \_(ツ)_/

    Tuesday, August 1, 2017 7:40 PM
  • Personally, I would not even consider updating AD objects en masse without a unique attribute (e.g., sAMAccountName).

    -- Bill Stewart [Bill_Stewart]

    Tuesday, August 1, 2017 8:00 PM
  • I absolutely agree with Bill.

    To test your Csv file just use it to return the objects.  It is likely that the name in the Csv is the display name in which case you are likely to get no output or limited output.

    Import-Csv C:\Users\admin\Desktop\PhoneNumber.csv |
    	ForEach-Object{Get-ADUser -Filter "Name -eq '$($_.Name)'"}
    


    \_(ツ)_/

    Tuesday, August 1, 2017 8:05 PM
  • Ok I changed my script around and managed to get it to work. Thank you for all the help!

    Thanks,

    Cullen

    Friday, August 4, 2017 5:13 PM