My blogspot: /CQman/
How to batch change office phone attribute values for O365 users via PowerShell?
Demand information:
O365 users in the organization have manually entered their office phone when they were created, and now they need to add a uniform number in front of their office phone, such as "0571-0985", and modify it in a uniform way in a batch.
Note: The Office phone of O365 users corresponds to the Work phone of Exchange mailbox users.
Mobile phone for O365 users corresponds to Mobile phone for Exchange mailbox users.
The attributes of the user's backend are: Office phone and Work phone correspond to PhoneNumber; Mobile phone corresponds to Mobilephone.
O365 Management Console:
Exchange admin center:
Solution:
Get mailbox account through get-mailbox command, then use filter command to get only mailbox users (exclude "conference room account"), then pass the data to get-msoluser to get the value of user's phone attribute and put the value of the existing phone attribute.
Assign it to a variable for saving; then combine specific phone number prefixes and assign the value to a new variable, finally use Set-MsolUser to change the settings, and finally display the DisplayName, UserPrincipalName, PhoneNumber outputs of all the changed users.
Steps:
- Open powershell ISE copy the following command into powershell ISE or save it as a .ps1 script file.
#Change Powershell execution policy
Set-Executionpolicy -scope Process -executionPolicy Unrestricted -force
#connect to Exchange online
$UserCredential = Get-Credential
$exchangeSession = New-PSSession -ConfigurationName -ConnectionUri /PowerShell -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $exchangeSession -DisableNameChecking -AllowClobber
#connect to Office 365
Import-Module MSOnline
Connect-MsolService -AzureEnvironment AzureChinaCloud -Credential $UserCredential
#Get all mailbox users
$users = Get-Mailbox -ResultSize unlimited -Filter {(RecipientTypeDetails -eq 'UserMailbox')}|% {Get-MsolUser -UserPrincipalName $_.UserPrincipalName }
$Output = @()
#Traversing every user and make changes
foreach($user in $users){
if($ -ne $null )
{
$phone = $
$Prefix = "+86" # Here's +86 as an example
$PN = $Prefix + $phone
Set-MsolUser -UserPrincipalName $ -PhoneNumber $PN
$output += Get-MsolUser -UserPrincipalName $ |Select-Object DisplayName,UserPrincipalName,PhoneNumber
}
else {continue}
}
$output |Out-GridView
Note: The above commands are only for mailbox users and the original Office phone (or Work phone) attribute is not empty.
before modification
Get-MsolUser |ft DisplayName ,UserPrincipalName,PhoneNumber
After running the command:
Note: The above command does not change the value of the phone attribute for the conference room mailbox.
Output content