Solved

Help formatting phone numbers using powershell

Posted on 2014-09-05
2
1,149 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

759 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

26 Experts available now in Live!

Get 1:1 Help Now