none
Remove Non Numeric Characters from Script RRS feed

  • Question

  • Hi All,

    Hope you can help out on this one. We are deploying Skype and while our AD is fully populated with E.164 number in the 'telephonenumber' field I need to copy the last 5 digits into the 'ipphone' field for all users in an OU. 

    I have got the copy to work (need to do 6 as the space) but how can I run it so the space or any non-numeric characters are removed after the import?

    Thanks

    Jon

    Get-ADUser -identity 'jon.garrett' -Properties OfficePhone,ipPhone | ForEach
    
    {
    
        $phoneNumber = $_.OfficePhone.Substring($_.OfficePhone.Length - 6, 6)
    
        Set-ADUser -Identity $_.SamAccountName -Replace @{ipPhone=$phoneNumber}
        Set-ADUser -Identity $_.SamAccountName -Replace @{ipPhone=" ", ""}
      
      }

    Wednesday, June 27, 2018 9:49 AM

Answers

  • My example:

    'Telephone Number: +44 123 456 7890 (has the space)' -replace '\D' -match '(\d{5})$' | Out-Null ;$Matches[1]

    So the completed code according to your example from above would be this:

    Get-ADUser -identity 'jon.garrett' -Properties OfficePhone,ipPhone | 
        ForEach-Object{
            $phoneNumber = $($_.OfficePhone -replace '\D' -match '(\d{5})$' | Out-Null ;$Matches[1])
            Set-ADUser -Identity $_.SamAccountName -Replace @{ipPhone=$phoneNumber}
        }
    


    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Wednesday, June 27, 2018 11:25 AM
    • Marked as answer by Jon Garrett Wednesday, June 27, 2018 11:30 AM
    Wednesday, June 27, 2018 11:20 AM
  • Replace

    $phoneNumber = $_.OfficePhone.Substring($_.OfficePhone.Length - 6, 6)

    with:

    $phoneNumber = ($_.OfficePhone.Substring($_.OfficePhone.Length - 6, 6)) -replace '\D',''

    • Marked as answer by Jon Garrett Wednesday, June 27, 2018 11:30 AM
    Wednesday, June 27, 2018 11:13 AM

All replies

  • You might provide some examples how the numbers look like in your environment. Please have in mind that we're not all from the same country and have different systems and different telephone number lengths.  ;-)

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, June 27, 2018 10:18 AM
  • Yeah good point. I work in a multi national firm so thats why I'm just taking the last 6 digits. I just want to strip the rubbish out really so its only numeric.

    An example is:

    Telephone Number: +44 123 456 7890 (has the space)

    IPPhone: 67890

    Wednesday, June 27, 2018 10:54 AM
  • You could use replace with some regex to get rid of everything but the numbers:

    $input = "abc1 2 3 4 567"
    $onlyNumbers = $input -replace '\D',''
    $onlyNumbers # Result: 1234567

    • Proposed as answer by jrv Wednesday, June 27, 2018 10:59 AM
    Wednesday, June 27, 2018 10:55 AM
  • Sorry jrv really rubbish at Scripts! how does that go in the script exactly....
    Wednesday, June 27, 2018 11:12 AM
  • Replace

    $phoneNumber = $_.OfficePhone.Substring($_.OfficePhone.Length - 6, 6)

    with:

    $phoneNumber = ($_.OfficePhone.Substring($_.OfficePhone.Length - 6, 6)) -replace '\D',''

    • Marked as answer by Jon Garrett Wednesday, June 27, 2018 11:30 AM
    Wednesday, June 27, 2018 11:13 AM
  • My example:

    'Telephone Number: +44 123 456 7890 (has the space)' -replace '\D' -match '(\d{5})$' | Out-Null ;$Matches[1]

    So the completed code according to your example from above would be this:

    Get-ADUser -identity 'jon.garrett' -Properties OfficePhone,ipPhone | 
        ForEach-Object{
            $phoneNumber = $($_.OfficePhone -replace '\D' -match '(\d{5})$' | Out-Null ;$Matches[1])
            Set-ADUser -Identity $_.SamAccountName -Replace @{ipPhone=$phoneNumber}
        }
    


    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Wednesday, June 27, 2018 11:25 AM
    • Marked as answer by Jon Garrett Wednesday, June 27, 2018 11:30 AM
    Wednesday, June 27, 2018 11:20 AM
  • Great thanks. I will test in our Dev now and will mark as answered if all ok. 

    Thanks all. 

    Wednesday, June 27, 2018 11:28 AM