Powershell to change AD user's Address

Janb14 21 Reputation points

Does anyone know how to create a for loop (or similar) that would change AD (on prem) user address? We've moved offices, and rather than change 400 user's AD profile individually, I thought to use a small script.
sorry I'm new to PowerShell

What I have so far:

//-- I wanted to test the output first - by displaying the users and street address. If successful > export to a .csv file > edit the streetAddress > import the .csv file. However, this outputs user names, but not streetAddress --//

Get-ADUser -Filter * -SearchBase "OU=Departments,OU=Users,OU=Sydney,OU=#company,DC=#company,DC=int" | select Name,streetAddress

//-- I was advised to try for loops, which is more efficient and doesn't require export and import of a .csv file. But this is incomplete. --//

ForEach Loop

foreach Get-ADUser
Get-ADuser -Filter "streetAddress -eq '123 Street City" | Set-ADUser -streetAddress '987 Road City'

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,996 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lein Baart 166 Reputation points

    First time posting, so please forgive any errors here, but thought I could help a bit.

    The answer proposed by RichMatheison-8856 is correct, but I just wanted to provide a little more clarity as to why you weren't seeing a street address in your output.

    Get-ADUser only returns a certain number of properties by default, to get additional properties you need to include the "-Properties" paramenter, along with the property name

    In your case this would be the below to find all users under a specific OU:

     Get-ADUser -Filter * -SearchBase "OU=Departments,OU=Users,OU=Sydney,OU=#company,DC=#company,DC=int" -Properties streetAddress | select Name, streetAddress  

    Or the below to return all users to with a street address equal to a specific string:

    Get-ADUser -Filter {streetAddress -eq "123 Street City"} -Properties streetAddress | select Name, streetAddress  

    To create a loop you need to save the result of the Get-ADUser command to a variable, then loop through each item:

    $allUsers = Get-ADUser -Filter {streetAddress -eq "123 Street City"}  
    foreach ($user in $allUsers)  
        Set-ADUser $user -streetAddress "123 New Address"  
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,111 Reputation points

    There should be no need for any loop. This should be all you need:

    Get-ADuser -Filter "streetAddress -eq '123 Street City'" | Set-ADUser -streetAddress '987 Road City'  

    FYI, your example is missing the closing "'" in the filter.

    3 people found this answer helpful.
    0 comments No comments

  2. Janb14 21 Reputation points

    Thank you, @Lein Baart and @Rich Matheisen
    You were both informative!

    After some more reading and practice - I found a similar answer that I used (and works). I added comments for those that need an explanation.

    //-Find any AD users with streetaddress 'TEST'
    $ADUser = Get-ADUser -Filter "StreetAddress -eq 'TEST'" -Properties StreetAddress

    //--$user is PS default value for user
    foreach($user in $ADUser)

    //--Changes address to any user in the container $ADUser, see line 1
    {Set-ADUser -Identity -StreetAddress "123 New Address"}

    //--Before running the script > I had to test this worked > changed my AD account address to "TEST" > ran the script > checked my AD account address and it changed to 123 New Address

    I changed the value of 'TEST' to the old address > ran the script > I searched AD using a filter of streetAddress "123 New Address" and found all the user's AD account had been updated successfully. --//