PowerShell: Using Active Directory .Net methods in PowerShell Part 1
There are times you don’t have access to the Active Directory PowerShell cmdlets. One of the great things about PowerShell is the ability to use .Net in PowerShell scripts. For more, check out Part 2.
Here are some alternatives to using Get-ADForest & Get-Domain:
Get Active Directory Forest Information
$ADForestInfo = System.DirectoryServices.ActiveDirectory.Forest::GetCurrentForest()
$ADForestInfo.Name
$ADForestInfo.Sites
$ADForestInfo.Domains
$ADForestInfo.GlobalCatalogs
$ADForestInfo.ApplicationPartitions
$ADForestInfo.ForestMode
$ADForestInfo.RootDomain
$ADForestInfo.Schema
$ADForestInfo.SchemaRoleOwner
$ADForestInfo.NamingRoleOwner
OR
Get Active Directory Domain Information
Target the current (local) computer’s domain:
$ADDomainInfo = System.DirectoryServices.ActiveDirectory.Domain::GetComputerDomain()
Target the current user’s domain:
$ADDomainName = System.DirectoryServices.ActiveDirectory.Domain::GetCurrentDomain()
$ADDomainInfo.Forest
$ADDomainInfo.DomainControllers
$ADDomainInfo.Children
$ADDomainInfo.DomainMode
$ADDomainInfo.Parent
$ADDomainInfo.PdcRoleOwner
$ADDomainInfo.RidRoleOwner
$ADDomainInfo.DomainControllers
OR
Note: Use System.DirectoryServices.ActiveDirectory.Domain::GetCOMPUTERDomain().Attribute for the local computer’s domain info.
Example: System.DirectoryServices.ActiveDirectory.Domain::GetCOMPUTERDomain().Forest
Get the local computer’s site information:
$LocalSiteInfo = System.DirectoryServices.ActiveDirectory.ActiveDirectorySite::GetComputerSite()
$LocalSiteInfo.Name
$LocalSiteInfo.Domains
$LocalSiteInfo.Subnets
$LocalSiteInfo.Servers
$LocalSiteInfo.AdjacentSites
$LocalSiteInfo.SiteLinks
$LocalSiteInfo.InterSiteTopologyGenerator
$LocalSiteInfo.Options
$LocalSiteInfo.Location
$LocalSiteInfo.BridgeheadServers
$LocalSiteInfo.PreferredSmtpBridgeheadServers
$LocalSiteInfo.PreferredRpcBridgeheadServers
$LocalSiteInfo.IntraSiteReplicationSchedule