Solved

Help formatting phone numbers using powershell

Posted on 2014-09-05
2
1,253 Views
Last Modified: 2014-09-05
Hello,  I am trying to create a powershell to take all my users in active directory and format all of their phone numbers the same  (XXX)XXX-XXXX

I have the script getting the users and stripping all no numeric characters and writing the stripped number to the office phone field in AD.  however the formatting is not applying.  It does not error.  It just ignores it and writes the 10 digit number.

Here is the Powershell I am running. (this one has been modified to a single user for testing)

# Retrieve Single User
$Users =  Get-ADUser -LDAPFilter "(sAMAccountName=testuser)" -Properties sAMAccountName, telephoneNumber
ForEach ($User In $Users)
 {
     $Name  = $User.sAMAccountName
     # Replace non-digits with blank.
     $Phone = $User.telephoneNumber -Replace "[^0-9]", ""
     # Check length.
     If ($Phone.Length -eq 10)
     {
         # Format number as (xxx)xxx-xxxx
         $Phone= "{0:(###)###-####}" -f $Phone
         # Save corrected value for telephoneNumber.
         Set-ADUser -Identity $Name -OfficePhone $Phone
     }
     Else {"$Name invalid value: $Phone"}
 }
0
Comment
Question by:chris_miller
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40306462
Try
$Phone= "{0:(###)###-####}" -f ([int64]$Phone)

Open in new window

0
 
LVL 1

Author Comment

by:chris_miller
ID: 40306508
yes that worked :)  thanks so much.  

I was trying -as [long] but I had it on the wrong line.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now