Use PowerShell to Rename Active Directory Sites
Summary: Microsoft Scripting Guy Ed Wilson shows how to use the Active Directory PowerShell cmdlets to query and to rename a site.
Hey, Scripting Guy! I am getting excited about using Windows PowerShell to manage Active Directory. But there are some things that are really easy to do in the GUI that I cannot seem to be able to do by using the cmdlets. Is there some reason for this?
Microsoft Scripting Guy, Ed Wilson, is here. Well, things are certainly looking up around Charlotte, North Carolina in the United States. In just a few days, the Scripting Wife and I will head north to Columbus, Ohio to participate in the first ever PowerShell Saturday. This event is already a tremendous success—it sold out in less than three weeks, and the Central Ohio PowerShell User Group has added ten new users—with the event yet to happen. In fact, there have been several requests to host such an event in various cities around the world—and like I said, the day has not yet arrived. Success? I most certainly think so. Much of the credit goes to Wes Stahler, the president of the Central Ohio PowerShell User Group, in addition to Ashley McGlone, Brian Jackett, and the Scripting Wife.
On Monday, following PowerShell Saturday, I begin a series of five Live Meetings called, PowerShell for the Busy Admin. This is a special TechNet webcast that is part of the Road to TechEd. It is also my intention to help you hone your Windows PowerShell skills prior to the 2012 Scripting Games.
Along the way, there will also be a new Windows PowerShell quiz. If you have not taken the 2011 Scripting Games quiz, you should. It is a great learning tool, and has been taken by thousands of your peers.
RC, you are right. In the Active Directory Sites and Services MMC, it is easy to rename a site. All you need to do is to right-click the site and select Rename from the action menu. By default, the first site is called Default-First-Site-Name, which is not too illuminating. The GUI way to rename a site is shown in the image that follows.
RC, to work with Active Directory Sites and Services, it is necessary to understand that they are a bit strange. First of all, they reside in the configuration naming context. Connecting to this context by using the Active Directory module is rather simple. All I need to do is use the Get-ADRootDSE cmdlet, and then select the ConfigurationNamingContext property. First I make a connection to my domain controller and import the Active Directory module. This is shown here.
Enter-PSSession -ComputerName dc3 -Credential iammred\administrator
Here is the code that will retrieve all of the sites. It uses the Get-ADObject cmdlet to search the configuration naming context for objects that are the class of Site.
Get-ADObject -SearchBase (Get-ADRootDSE).ConfigurationNamingContext -filter “objectclass -eq ‘site'”
After I have the site I want to work with, I change the DisplayName attribute. To do this, I pipe the site object to the Set-ADOObject cmdlet. The Set-ADOObject cmdlet allows me to set a variety of attributes on an object. This command is shown here. (This is a single command that is broken into two pieces at the pipe character.)
Get-ADObject -SearchBase (Get-ADRootDSE).ConfigurationNamingContext -filter “objectclass -eq ‘site'” | Set-ADObject -DisplayName CharlotteSite
After I have set the DisplayName attribute, I decide to rename the object. To do this, I use another cmdlet called Rename-ADObject. Once again, to simplify things I pipe the Site object to the cmdlet, and I assign a new name for the site. This command is shown here. (This is also a one line command broken at the pipe character.)
Get-ADObject -SearchBase (Get-ADRootDSE).ConfigurationNamingContext -filter “objectclass -eq ‘site'” | Rename-ADObject -NewName CharlotteSite
The commands I used, as well as any associated output appear in the figure that follows.
I decide to go back to the MMC to verify that the name change took place. Normally, I would just rerun the site query, but because I already had the MMC open, it was simple enough to do (besides it makes a colorful picture).
RC, that is all there is to using Windows PowerShell to rename a site. Tomorrow 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 email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy