update telephone field in ADUC from ipphone field

I need to update the telephone number field in the user ADUC from the IPphone field with powershell . The ipphone  has this format 000-0000 the telephone field has this format 000-000-0000 I need to include the area code .

The ipphone is already populated so it needs to get that number and place it in the telephone field but with an area code in front of it so that all 10 digits are populated
Thank you
gd6627Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Well, where's the area code supposed to come from?
And which users should be processed? Do you have a list, or just each and every user with the ipPhone property? Or just from one OU?
Should the ipPhone property by cleared or kept as it is?
The command itself is rather easy:
Get-ADUser TestUser -Properties ipPhone | ForEach-Object {Set-ADUser -Identity $_ -OfficePhone "123-$($_.ipPhone)"}

Open in new window

0
Shaun VermaakTechnical Specialist/DeveloperCommented:
Do you have the region for users? It can be users as a lookup list to get to the area code
0
gd6627Author Commented:
I can either input the area code so for example if my csv file has a list of users and I have the 7 digit numbers I would like to say heres the are code 555 now append the 7 digit number with this area code and update the telephone field in AD like so ex 555-123-4567

Or I can have the are code as part of the csv input so that I can have different are codes
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

gd6627Author Commented:
Hi Obda, the iphone should stay as is , I have a list of users firstnames and last names or I can use samaccounts . I just need to add the ipphone as part of the telephone field with all 10 digits 000-000-0000
0
oBdACommented:
This expects a csv with a column "SamAccountName" and a column "AreaCode" (additional columns will just be ignored):
Import-Csv -Path C:\Temp\users.csv | ForEach-Object {
	"Processing $($_.SamAccountName) ..." | Write-Host
	If ($ADUser = Get-ADUser -Identity $_.SamAccountName -Properties ipPhone) {
		Set-ADUser -Identity $ADUser.DistinguishedName -OfficePhone "$($_.AreaCode)-$($ADUser.ipPhone)"
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gd6627Author Commented:
so the csv will have a columm for area code a columm for the 7 digit number and a columm for the samaccount correct. ok no I take that back your using the iphone field to append the users telephone field
0
oBdACommented:
Just a column for the three-digit area code. the 7-digit number will be taken from the ipPhone attribute as per your description.
0
gd6627Author Commented:
Hey Obda, Am getting the following error I have attached the data file


Processing  ...
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At E:\PSSCRIPTS\ADTemplates\Phone\NEw PHONE SCRIPT\UpdateTelPhoneFieldfromIPPhone.ps1:8 char:37
+     If ($ADUser = Get-ADUser -Identity $_.SamAccountName -Properties  ...
+                                        ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Samacc-AreCodes312Fulton.csv
0
oBdACommented:
Your first column is called "samaccount" instead of "SamAccountName".
0
gd6627Author Commented:
yes I caught that I changed it but its still the same
0
gd6627Author Commented:
just a thought shouldn't the distinguished name be samaccountname
0
oBdACommented:
And you need to remove the space before the comma. That would only work if the fields were enclosed in double quotes,
0
gd6627Author Commented:
what comma?
0
oBdACommented:
samaccount ,Areacode
----------^ That space

Open in new window

0
gd6627Author Commented:
in the csv file?
0
oBdACommented:
Yes.
0
gd6627Author Commented:
I have to columms in the csv theres no commas
0
oBdACommented:
Here are the first few lines of the csv you posted. There is clearly a space after "samaccount", right before the comma/delimiter. This makes the name of the first column "samaccount " (including the trailing space!).
samaccount ,Areacode
dicarpen,312
shajones,312
stmiller,312

Open in new window

The first line must be
SamAccountName,Areacode
-------------^^^ No space!

Open in new window

0
gd6627Author Commented:
Hmmm Whats the best way to clean that up
0
oBdACommented:
You replace "samaccount " with "SamAccountName" and save the file, what's the big deal?
0
gd6627Author Commented:
so now am getting the telephone field updated like this -000-0000 no area code ?
0
oBdACommented:
Make sure there is no leading/trailing space around "AreaCode" in the header line. either.
0
gd6627Author Commented:
That did it my friend ! weird how those spaces got in there . Thanks so much for your help. Your very good at this
0
gd6627Author Commented:
oBda had Outstanding guidance and support and quick to respond!


Shaun Thank you for your assistance as well but it didn't quite address my issue
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.