Learn about Windows PowerShell
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
Good blog, Ed. The AD module is very powerful, and I'm glad you are exploring it. There are many cmdlets, many of which are relatively unknown and unused. Get-ADRootDSE is a good example. Thanks.
I think you are still busy preparing sg2012 and don't have too much time left ...
Thank you for arranging this event for us!
And bigteddy is reight, of course!
The AD module is magnificant and has a lot of goods that may remain under the cover, if you wouldn't point them out! Great to stay tuned!
Can any one help me in writing a powershell scrip on:
Script that will read each users EmployeeType attribute and write it to ExtensionAttribute1.
This is good stuff, I have been wanting to start building scripts for the Sites and Services side of the house, just haven't got around to it. Thanks Ed.
My only recommendation is to install the RSAT tools locally on your machine and run the AD cmdlets from you workstation rather than using the remote PS Session (providing you are using Windows 7). If you are using Windows XP then you have to use the remote PS Session.
And how about using provider instead...? :)
ren cn=def* cn=CharlotteSite
I really like AD provider in MS AD module. TBH that's probably the only reason why I use it more and more every day. ;)
Get-ADReplicationSite "Default-First-Site-Name" | Rename-ADObject "CharlotteSite"
Sorry for the Typo.
Get-ADReplicationSite "Default-First-Site-Name" | Rename-ADObject -NewName "CharlotteSite"