Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to set the users’ display names in Active Directory Domain Services.
Hey, Scripting Guy! I have a number of user names that were hastily created. I thought at first they were going to be temporary workers, but it seems that temporary is hanging around a lot longer than previously anticipated. This is actually causing issues with some of our Active Directory queries, because empty strings return when we were expecting data. I am wondering if there is an easy way to populate the display name— it would be fine to make it the same as the user name. I just need to get this done, and I do not want to spend a lot of time with it. Can you help me?
—HH
Hello HH,
Microsoft Scripting Guy, Ed Wilson, is here. This afternoon, it has not rained. In fact, it has not rained for two days. It must be some sort of record. Anyway, I am sipping a cup of Gun Powder Green Tea with a spoonful of lemon grass, a crushed cinnamon stick, a spoonful of rose hips, and a spoonful of Jasmine flowers. I let it steep for four minutes, and it is a lovely, light, and refreshing flavor. The tea is the perfect companion for doing a little bit of Active Directory Domain Services (AD DS) work.
Query AD DS
The first thing I always do is query AD DS to see what sort of data I have. This is easy to do when using Windows PowerShell and the Active Directory module. Here is the command I use to query for UserName and DisplayName:
Get-ADUser -SearchBase 'ou=testou,dc=iammred,dc=net' -Filter * -Properties displayname | select name, displayname
The output is shown here:
PS C:\> Get-ADUser -SearchBase 'ou=testou,dc=iammred,dc=net' -Filter * -Properties di
splayname | select name, displayname
name displayname
---- -----------
newtestuser70
newtestuser71
newtestuser72
newtestuser73
<truncated>
Set DisplayName
To set the DisplayName attribute to be the same as the user name, I can pipe the results from the Get-ADUser cmdlet to the Set-ADUser cmdlet. The thing that is great about this is that the Set-ADUser cmdlet has a parameter for DisplayName, and this it makes it really easy. Here is the command I tried first:
Get-ADUser -SearchBase 'ou=testou,dc=iammred,dc=net' -Filter * -Properties displayname | Set-ADUser -DisplayName $_.name
But this command does not work, because the value of $_ is null here. Therefore, when I use the Up arrow to check my results, it comes up empty as shown here:
PS C:\> Get-ADUser -SearchBase 'ou=testou,dc=iammred,dc=net' -Filter * -Properties di
splayname | select name, displayname
name displayname
---- -----------
newtestuser70
newtestuser71
newtestuser72
<truncated>
Well, I decide to try it without the $_ to see if the cmdlet is smart enough to pick up the Name property directly without the assistance of $_. So here is the command I use:
Get-ADUser -SearchBase 'ou=testou,dc=iammred,dc=net' -Filter * -Properties displayname | Set-ADUser -DisplayName name
Now I check the results, and they are exactly what I was afraid of the first time—I have a bunch of names. See for yourself:
PS C:\> Get-ADUser -SearchBase 'ou=testou,dc=iammred,dc=net' -Filter * -Properties di
splayname | select name, displayname
name displayname
---- -----------
newtestuser70 name
newtestuser71 name
newtestuser72 name
newtestuser73 name
<truncated>
So I try one more time. I assume that this command will work, but I was hoping that I could directly pipe to the Set-ADUser cmdlet without slowing things down with a Foreach-Object cmdlet (% is the alias). Here is the command:
Get-ADUser -SearchBase 'ou=testou,dc=iammred,dc=net' -Filter * -Properties displayname | % {Set-ADUser -Identity $_ -DisplayName $_.name}
As I expected, the command worked properly. Here are the results:
PS C:\> Get-ADUser -SearchBase 'ou=testou,dc=iammred,dc=net' -Filter * -Properties di
splayname | select name, displayname
name displayname
---- -----------
newtestuser70 newtestuser70
newtestuser71 newtestuser71
newtestuser72 newtestuser72
newtestuser73 newtestuser73
<truncated>
So all-in-all…not bad. It took me like less than three minutes to perform this task. It would have taken even less time if I went with what I suspected would have been the command in the first place. But hey, that is how one learns (or at least the way I learn—I just try it and see what happens).
HH, that is all there is to using Windows PowerShell to set the display names of all your users in a particular organizational unit to be the same as the user names. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy