I am trying to write a powershell script that will grab the users telephone number from active directory, select a portion of it (last 4 numbers), concatenate some numbers onto that, and then add that back into Active directory as the IP phone attribute.
I was able to do this successfully by exporting data to a csv modifying and then re-importing but I know there is a way of doing this all from the command line.
The command I used to dump the telephone number was
get-qaduser -phonenumber xxx-xxx-xxxx | select-object phonenumber
I am not sure how to actually get and manipulate the value that is returned from that command. I tried
$phone = get-qaduser -phonenumber xxx-xxx-xxxx | select-object phonenumber
Which seems to work but it stores the whole output not just the telelphone number.
So I guess my first two questions are
1. How can i capture only the 10 digit telephone number
2. How can I manipulate that in powershell
for example
(123)-456-7890
if so you can try this
$phone = (get-qaduser USERNAME).phonenumber
$phone.substring(10,4)
Just change the first number to fit your needs.
so if you numbers are
(123)456-7890
it would be
$phone = (get-qaduser USERNAME).phonenumber
$phone.substring(9,4)
then to change
note:this has not been tested so test in dev first.
$newphone = $phone - replace "$($phone.substring(9,4))"
set-qaduser USERNAME -telephonenumber $newphone