Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, at the Charlotte Windows PowerShell user group meeting, one of the members was talking about adding mailing information to all of the users in a particular organizational unit. There are many reasons that this information may change: Street names are renamed all the time in the United States; cities incorporate other cities; postal codes change due to shifting populations. These are all reasons outside of one’s control. And there are other reasons as well, for example, companies may consolidate offices, close offices, or move offices. Everyone who used to work in the Charlotte, North Carolina, office may suddenly find themselves commuting to Jacksonville, Florida.

Whatever the reason is for updating address information, Windows PowerShell and the Active Directory module make this task fairly simple. It involves a number of attributes, however, and because all of the values for the attributes are static, this is a great time to use splatting.

Note I have several really good articles that talk about splatting. I have written some of them, and others have been written by various guests. Splatting is a technique introduced with Windows PowerShell 2.0, and, if you have not been using it, today is a great time to learn.

Populating address information in Active Directory

There are a couple of approaches to populating address information in Active Directory Domain Services (AD DS). The first approach is to search for particular values and modify the offending values. A second approach is to search for empty or null values for specific attributes, and then populate only the missing data. A third approach is to simply blast new values for a select group of users that meet a specific search criteria. This is the approach I take today.

Create three hash tables for splatting

I want to populate the city, state, streetaddress, postalcode, and the country of each user in the TestOU. Because I know that the city properties are already populated, I know that I will be searching for the City property. However, it may be that the city will need to change once the new properties apply to the user (depends on the exact scenario). Therefore, I decide to create three hash tables. Each hash table resides in a variable with the name of the city. Each hash table is used to apply the appropriate attribute values when calling the Set-ADUser cmdlet from the Active Directory module.

Note Keep in mind, the key properties of the hash table (city, state, streetaddress, postalcode) must match exactly with the Set-ADUser cmdlet parameter names. This is important because ADSI stores the city information in an attribute named l (a lowercase letter L), but the Set-ADUser cmdlet uses the parameter name of Cityto provide the same information. There are similar attribute name changes to be aware of when migrating old ADSI scripts to use the Active Directory cmdlets.

The three hash tables are shown here.

$charlotte = @{

“City” = “Charlotte”

“State” = “NC”

“StreetAddress” = “One Microsoft Way”

“PostalCode” = “12345” }

$atlanta = @{

“City” = “Atlanta”

“State” = “GA”

“StreetAddress” = “Two Microsoft Way”

“PostalCode” = “23456” }

$jacksonville = @{

“City” = “Jacksonville”

“State” = “FL”

“StreetAddress” = “Three Microsoft Way”

“PostalCode” = “34567” }

The code to make the change is simple. I use the Get-ADUser cmdlet to search the organizational unit, and I add a filter to look for the city equal to Charlotte. I then pipe the results to the Set-ADUser cmdlet and splat the Charlotte values. Because all three cities reside in the United States, I use a static Country value of US. Here is the code to assign the Charlotte values to users with a city value of Charlotte.

Get-ADUser -SearchBase ‘ou=testou,dc=iammred,dc=net’ -Filter `

{city -eq ‘charlotte’} |

Set-ADUser @Charlotte -Country US

Now, I do a cut-and-paste job to create two more searches to find Atlanta and to find Jacksonville. I also splat their values as well. Only the search filter and the splatted values change.

Get-ADUser -SearchBase ‘ou=testou,dc=iammred,dc=net’ -Filter `

{city -eq ‘atlanta’} |

Set-ADUser @atlanta -Country US

Get-ADUser -SearchBase ‘ou=testou,dc=iammred,dc=net’ -Filter `

{city -eq ‘jacksonville’} |

Set-ADUser @jacksonville -Country US

Join me tomorrow for a guest blog post by the Scripting Wife as she talks about our upcoming European Scripting Tour.