locked
Change format of phone numbers in AD using powershell RRS feed

  • Question

  • I need to change all the phone numbers in AD from (xxx)-xxx-xxxx to xxx.xxx.xxxx Getting rid of dashes and adding periods

    I have tried Richard Meuller's script:

    # Retrieve all users with telephoneNumber in format "(xxx) xxx-xxxx" or "(xxx) xxx.xxxx".
    $Users = Get-ADUser -LDAPFilter "(telephoneNumber=(*)*)" -Properties distinguishedName, telephoneNumber
    ForEach ($User In $Users)
    {
        $DN = $User.distinguishedName
        # Replace ")" and "-" with ".".
        $Phone = $User.telephoneNumber -Replace "\)\-""."
        # Remove any spaces or "(".
        $Phone = $Phone -Replace "\s|\("""
        # Save corrected value for telephoneNumber.
        Set-ADUser -Identity $DN -OfficePhone $Phone
    }

    but keep getting error:

    Set-ADUser : cannot validate argument on parameter 'Identity".  The argument is null.

    Do I have to put in a searchbase scope?  Thanks!

    Tuesday, June 6, 2017 9:27 PM

Answers

  • I cannot explain your error message, but this version worked for me:

    # Retrieve all users with telephoneNumber in format "(xxx) xxx-xxxx" or "(xxx) xxx.xxxx".
    $Users = Get-ADUser -LDAPFilter "(telephoneNumber=(*)*)" -Properties distinguishedName, telephoneNumber
    ForEach ($User In $Users)
    {
        $DN = $User.distinguishedName
        # Replace ") " and ")" and "-" and spaces with ".".
        $Phone = $User.telephoneNumber -Replace "\)\s", "." -Replace "\)", "." -Replace "\-", "." -Replace "\s", "."
        # Remove any "(".
        $Phone = $Phone -Replace "\(", ""
        # Save corrected value for telephoneNumber.
        Set-ADUser -Identity $DN -OfficePhone $Phone
    }
    

    I did not want to modify phone numbers in my domain, but the values for the $Phone variable looked good, and I used $DN with Get-ADUser to retrieve information on each user, so $DN should work fine with Set-ADUser as well. $DN was never null.

    To troubleshoot, comment out the Set-ADUser statement and output the values of $DN and $Phone. For testing I replaced the Set-ADUser statement with:

    Get-ADUser -Identity $DN


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by Hello_2018 Wednesday, June 7, 2017 10:25 AM
    • Marked as answer by joe schmo Wednesday, June 7, 2017 4:57 PM
    Wednesday, June 7, 2017 12:47 AM

All replies

  • I cannot explain your error message, but this version worked for me:

    # Retrieve all users with telephoneNumber in format "(xxx) xxx-xxxx" or "(xxx) xxx.xxxx".
    $Users = Get-ADUser -LDAPFilter "(telephoneNumber=(*)*)" -Properties distinguishedName, telephoneNumber
    ForEach ($User In $Users)
    {
        $DN = $User.distinguishedName
        # Replace ") " and ")" and "-" and spaces with ".".
        $Phone = $User.telephoneNumber -Replace "\)\s", "." -Replace "\)", "." -Replace "\-", "." -Replace "\s", "."
        # Remove any "(".
        $Phone = $Phone -Replace "\(", ""
        # Save corrected value for telephoneNumber.
        Set-ADUser -Identity $DN -OfficePhone $Phone
    }
    

    I did not want to modify phone numbers in my domain, but the values for the $Phone variable looked good, and I used $DN with Get-ADUser to retrieve information on each user, so $DN should work fine with Set-ADUser as well. $DN was never null.

    To troubleshoot, comment out the Set-ADUser statement and output the values of $DN and $Phone. For testing I replaced the Set-ADUser statement with:

    Get-ADUser -Identity $DN


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by Hello_2018 Wednesday, June 7, 2017 10:25 AM
    • Marked as answer by joe schmo Wednesday, June 7, 2017 4:57 PM
    Wednesday, June 7, 2017 12:47 AM
  • Hi joe,

    If this not worked, here is a workaround:

    Export these phonenums via get-aduser cmdlet then export these result to csv, using microsoft excel to open it then replace what you want.

    using: 

    Import-Csv 'd:\postcode.csv' | % { Set-ADUser -Identity $_.name -OfficePhone $_.OfficePhone 

    to import it.

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, June 7, 2017 10:25 AM
  • Thanks Richard, your new one worked well for me!  Saved me time punching in numbers!
    Wednesday, June 7, 2017 4:58 PM