Can anyone please help me to get all the domains in Active Directory. I have tried many times, but all the programs are listing only the current working domain.
How can I do this?
To list all domain controllers the Get-ADDomainController PowerShell cmdlet is used. The Get-ADDomainController cmdlet can get all domain controllers or list specific ones with the various search parameters.
The traditional approach to finding and listing the Domain Controllers(DCs) in a forest is to use the Get-ADDomainController PowerShell command.
Right-click the root domain, and click Properties. Under the General tab, you will find the forest and domain functional levels currently configured on your Active Directory Domain Controller.
I had some issues getting LeeMobile's code to work in my case bacause it was trying to find my application's current domain context while running forest.Domains. I was able to get around it by doing something like this.
Forest forest = Forest.GetForest(new DirectoryContext(DirectoryContextType.Forest, "yourForestDomain", "username", "password"));
DomainCollection domains = forest.Domains;
Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "yourDomain", "username", "password"));
Forest forest = domain.Forest;
DomainCollection domains = forest.Domains;
The above uses the System.DirectoryServices.ActiveDirectory namespace. It'll give you a domain collection containing all the domains that are in the same forest as your given domain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With