Wednesday, March 26, 2014

Everyday Powershell - Part 18 - Set Address Details in Active Directory

Here's a request from one of our readers. He's tidying up a negelected AD he inherited and needed a way to apply consistent addresses to his AD User Objects.

Powershell is great at this stuff! We've decided to use group membership as a way to decide who should get what data, but this could be easily applied via OU.
<# For this to work setup a CSV in C:\Scripts call it Groups.csv
And fill it with data like this; 
SecurityGroup,Address,City,Zip,State,Country,Telephone
Group1,1 Some St,SomeTown,1122,SOMESTATE,AU,615551234
Group2,2 Some Other St,SomeOtherTown,2211,SOMESTATE,AU,615554321 #>

$csvdata =import-csv C:\Scripts\Groups.csv
foreach($row in $csvdata)
{
    $users= Get-ADGroupMember $row.securitygroup
    foreach($user in $users)
    {
        Set-ADUser $user -StreetAddress $row.Address -city $row.city -State $row.State -Postalcode $row.Zip -Country $row.Country -OfficePhone $row.Telephone
    }
}
Just a simple script with two loops, first one iterates through the rows in the CSV the second group steps through each user in the security group identified in the initial loop.

Nothing fancy but it fixed our readers problem. Such is the power of the shell!

No comments:

Post a Comment