Solved

Help formatting phone numbers using powershell

Posted on 2014-09-05
2
1,488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

724 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