Powershell – Active Directory PowerShell Script select field and add to another field

active-directorypowershell

I'm trying to come up with a script to take a field from a user (OfficeNumber) in AD and move it to another user field (HomeNumber) proceeding by an 800 number. So essentially what we are doing is setting up one touch dialing in AD.

Example user John Doe – under the general tab next to Telephone Number which is the OfficeNumber field, he has an extension 1234. In the Telephones tab we have the Home field, which is the HomeNumber field in PowerShell and its blank under this tab for John Doe.

What I want to do is populate the HomeNUmber field with the Office number but proceeded by an 800 number. So it will look like this: '8001239876,,,1,1234'

I know how to update these fields manually with the set-aduser command but i want to do a bulk script for all users in an OU. Thanks!

Edit: here is the code I came up with:

Import-Module ActiveDirectory
Clear-Host
# Getting telephone number value from all users into specific location to $users variable
$users = Get-ADUser -Filter * -SearchBase "dc=domain,dc=com" -Properties telephoneNumber,homephone
# Defining phone number
$phoneNumber = "8008251234,,,1,"
# Creating cycle and digging into users one by one
foreach ($user in $users)
{
  # Creating variable which will contain predefined phone number and adds value of Telephone Number field 
  $homePhone = $phoneNumber + $user.telephoneNumber
  # Setting Home Number field with new variable in cycle (for each user)
  Set-ADUser -Identity $user.SamAccountName -HomePhone $homePhone
}

Best Answer

Here's the general idea:

  1. Run get-aduser to enumerate the accounts that you want. Maybe use a filter to get only accounts with an OfficeNumber. Save this result to a variable. $users for example.

  2. Use a foreach($user in $users) block to loop through the list of users.

  3. Inside of that foreach block, grab the Officenumber, prepend your 1-800 number, and save it to a new variable. Then write that variable to the HomeNumber attribute.

Related Topic