PowerShell : How do I find all DCs in my forest ?

 

I find the –computerrole parameter of Get-QADComputer handy to find DCs and when usually I have to retrieve something from them with a WMI query, pipeline to (GWMI),  Here is a quick way to retrieve all DCs in a multi-domain forest model using the .NET namespace i.e [DirectoryServices.ActiveDirectory.Forest] method.

To find Global Catalogs only;

[DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().GlobalCatalogs | ft name,OSVersion,Domain

To find all domain controllers;

[DirectoryServices.Activedirectory.Forest]::GetCurrentForest().domains | %{$_.domaincontrollers} | ft name,OSVersion,Domain

 

To find out what other information you can retrieve from the DCs via this method simply pipe to the Get-Member Cmdlet;

[DirectoryServices.Activedirectory.Forest]::GetCurrentForest().domains | %{$_.domaincontrollers} | Get-Member

 

image