The PowerShell script given below is an example of using PowerShell to update the mobile numbers of their organization's users in Active Directory (AD). ADSelfService Plus, an AD self-service password management and single sign-on solution, allows users to self-update their Active Directory profile information. Here is a comparison between updating users' AD information using PowerShell and ADSelfService Plus:
Run the following script to update the users' mobile numbers in AD using a CSV file. Create a CSV file with the users' usernames and mobile numbers and mention its file path in the script.
$UsersCSV = Import-CSV "<enter CSV file here"
$Results = @()
ForEach ($User in $UsersCSV) {
$Username = $User.username.trim()
$Number = $User.mobile.trim()
$UserDetails = $null
$UserCheck = $null
Try{
$UserDetails = Get-ADUser -Identity $Username -Properties MobilePhone
}
Catch{
$_.Exception.Message
Continue
}
If (!$UserDetails.'MobilePhone') {
Try{
Set-ADUser -Identity $username -replace @{ 'MobilePhone' = $($Number) }
$UserCheck = Get-ADUser -Identity $Username -Properties mobilephone -ErrorAction Stop
If ($UserCheck) {
$Object = New-Object PSObject -Property ([ordered]@{
"User name" = $Username
"Mobile" = $UserCheck.MobilePhone
})
$Results += $Object
}
}
Catch{
$_.Exception.Message
Continue
}
}
}
$Results | Format-Table
Prompt the user for their username. This ensures only authorized users update their own details.
$Username = Read-Host "Enter your username"Prompt users for new details. This script collects updated contact information.
$PhoneNumber = Read-Host "Enter your new phone number"
$OfficeLocation = Read-Host "Enter your new office location"Modify the AD user attributes. This updates the user's mobile number and office location in AD.
Set-ADUser -Identity $Username -MobilePhone $PhoneNumber -Office $OfficeLocation Write-Host "Your details have been updated."Run this script regularly using Task Scheduler. This ensures user details are updated on a scheduled basis.
$trigger = New-ScheduledTaskTrigger -Daily -At "08:00AM"
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Scripts\UpdateADDetails.ps1" Register-ScheduledTask -TaskName "UserSelfUpdate" -Trigger $trigger -Action $action -User "SYSTEM"Update user details in AD using the script below:
Set-ADUser -Identity username -Email "newemail@domain.com" -OfficePhone "1234567890"Yes, multiple attributes can be updated at once using the script below:
Set-ADUser -Identity username -Title "New Title" -Department "IT"Admin approval depends on permissions. Some attributes require admin rights.