I was tasked with updating a number of mobile numbers in Active Directory and the easiest way that this was accomplished was by using a CSV file and PowerShell.

The CSV consisted of two columns. One for the UPN and another for the mobile number.

With any script, I normally use the -WhatIf statement. On line 9, between the two curly brackets ( } -WhatIf } ) you can put the statement in and this will give you an output of what is being changed.

In the figure below, this is the code used and I have provided it at the end of the article in text format for you to use. Just copy it to notepad and save it as a .PS1 file.

Exchange 2019 / active directory :- update mobile numbers using powershell
$Users = Import-Csv "C:\Temp\Numbers.csv"

foreach ($u in $Users) {

$Staff = Get-aduser -Server mydc.domain.com -filter "UserprincipalName -eq '$($u.upn)'" -Properties samaccountname,UserprincipalName | select samaccountname,UserprincipalName

foreach ($s in $Staff){ 

Set-ADUser -Identity $s.samaccountname -Replace @{mobile = $($u.mobile)} }

}

Let’s go over what the script does.

$Users = this is a variable to import the CSV file. The CSV file has a column called UPN and mobile and this is mapped in the lines with $u.
$Staff = this is another variable that queries Active Directory (note this was run from a DC, so no modules were imported)
The set command uses a replace option (-Replace) to either update an existing number and if nothing exists then the number is simply added.

Hope it helps.

Discover more from Everything-PowerShell

Subscribe now to keep reading and get access to the full archive.

Continue reading