Link to home
Start Free TrialLog in
Avatar of Ackles
AcklesFlag for Switzerland

asked on

edit mobile phone number in office 365

Dear Experts,
I have a weird scenario, we imported lot of users in Office 365 (no dir sync) & after the import realised that many of the mobile phones are having a space in the number like: + 1 43 343 5433
The problem is with the space between + & 1st number only, rest of the space should be as it is, so the correct format would be: +1 43 343 5433

I would like to someone to help me with Powershell to find all users who have this space after + & then trim it.

I know I can export & then reimport with csv, but would prefer something with powershell so I can do it without csv.

Thanks in advance for your help!
Regards,
A
Avatar of Albert Widjaja
Albert Widjaja
Flag of Australia image

Have you changed the number from the Active Directory Users & Computers console instead ?
Avatar of Ackles

ASKER

No, they were imported via excel
Avatar of Ackles

ASKER

I've found the regular expression as -Replace "(^\+(\s)1)", "+1"

This works to trim the space, but I have to make sure that I can get it to change the user mobile phone attribute.
If you have a list of users to update, you can use following code..
GC C:\User.txt | Get-User | %{Set-User $_.SamAccountName -MobilePhone ($_.MobilePhone -Replace "^\+\s+1","+1")}

Open in new window

User.txt format..
UserA
UserB
UserC

Open in new window

Avatar of Ackles

ASKER

Thanks,
I don't want to use the list...
Can it be directory done in powershell?
I can filter the users who's numbers are wrong

Get-MsolUser | where {$_.MobilePhone -Like "+ 1*"}  | ft DisplayName, MobilePhone -AutoSize
This shows all users who's numbers are not correct.

Question is how do I put this into variable to apply regex?
Try..
Get-MsolUser | where {$_.MobilePhone -Like "+ 1*"}  | %{Set-MsolUser $_.SamAccountName -MobilePhone ($_.MobilePhone -Replace "^\+\s+1","+1")}

Open in new window

Avatar of Ackles

ASKER

DAMN.... it delete the mobile number completely
If should not delete if the Get-MsolUser | where {$_.MobilePhone -Like "+ 1*"}  | ft DisplayName, MobilePhone command lists the mobile number..
Avatar of Ackles

ASKER

well, i ran it for one user only because i didn't want to take risk, this is what I ran:

get-msoluser -UserPrincipalName abc@xyz.com | set-msoluser  -MobilePhone ($_.MobilePhone -Replace "^\+\s+1","+1")

After this command when i tried to see the value for user, it was empty for mobile phone
:-) That will not work, because you are not using foreach command, so value of $_.MobilePhone would be null..

For single user try..
Get-msoluser -UserPrincipalName abc@xyz.com | %{Set-msoluser $_.UserPrincipalName -MobilePhone ($_.MobilePhone -Replace "^\+\s+1","+1")}

Open in new window

Avatar of Ackles

ASKER

it gives error, A positional parameter cannot be found that accepts argument "abc@xyz.com"
Get-msoluser -UserPrincipalName abc@xyz.com | %{Set-msoluser -UserPrincipalName $_.UserPrincipalName -MobilePhone ($_.MobilePhone -Replace "^\+\s+1","+1")}

Open in new window

Avatar of Ackles

ASKER

Now it works!!!
Should I run the previous command you gave for all users or do you want to check?
Because it will run for all the users???
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ackles

ASKER

Awesome Stuff!!!