?
Solved

Help formatting phone numbers using powershell

Posted on 2014-09-05
2
Medium Priority
?
1,591 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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Suggested Courses

770 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