none
Convert phone number field to a default format in AD via Powershell

    Question

  • We have populate or number in a random way

    Exempel:

    SamAccountName mobile
    bl 4520120958
    pra +45 2010 8442
    pka +45 2010 8441
    ean 4520108447
    up +45 2010 8434
    tiln 23739261
    tosi 4520490130
    lola 37140333
    sibr +45 23739075
    htl +45 23739035
    shc +45 23739097

    I will like to have a default that is +45 XXXX XXXX for all user

    Is there an smart way to do it via powershell ?

    • Moved by Richard MuellerMVP Thursday, July 12, 2018 12:27 PM Not a TechNet Wiki question
    Thursday, July 12, 2018 12:20 PM

All replies

  • I moved your question to a more appropriate forum.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, July 12, 2018 12:43 PM
  • You will have to parse the string and change it to what you need.

    You can start with removing the opening

    ($number -replace '^+45|45') -replace ' '

    Now you are left with the remaing 8 digits so split them and add it all back together:

    '+45 {0} {1}' -f $first4,$last4

     


    \_(ツ)_/


    Thursday, July 12, 2018 12:49 PM
    Moderator
  • You will have to parse the string and change it to what you need.

    You can start with removing the opening

    ($number -replace '^+45|45') -replace ' '

    Now you are left with the remaing 8 digits so split them and add it all back together:

    '+45 {0} {1}' -f $first4,$last4

     


    \_(ツ)_/


    Some of those numbers have space(s) separating the two four-digit groups. Here's another way to accomplish the reformatting:

    $x = $number -replace '(?:\+?45)?\s?(\d{4})\s?(\d{4})', '+45 $1 $2'
    The first group "(?:\+45|45)?" is non-capturing, and optional. All the spaces (\s?) are also optional.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Thursday, July 12, 2018 6:37 PM