{"id":113,"date":"2014-07-09T15:17:11","date_gmt":"2014-07-09T19:17:11","guid":{"rendered":"http:\/\/blog.metcorp.org\/?p=113"},"modified":"2025-10-13T13:39:25","modified_gmt":"2025-10-13T17:39:25","slug":"powershell-using-active-directory-net-methods-in-powershell","status":"publish","type":"post","link":"https:\/\/adsecurity.org\/?p=113","title":{"rendered":"PowerShell: Using Active Directory .Net methods in PowerShell Part 1"},"content":{"rendered":"<p>There are times you don&#8217;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.<br \/>\n<strong>For more, check out <a title=\"PowerShell: Using Active Directory .Net methods in PowerShell\" href=\"https:\/\/adsecurity.org\/?p=192\">Part 2<\/a>.<\/strong><\/p>\n<p>Here are some alternatives to using Get-ADForest &amp; Get-Domain:<\/p>\n<div># Get Active Directory Forest Information<\/div>\n<div>$ADForestInfo = [System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest()<\/div>\n<div><\/div>\n<div>$ADForestInfo.Name<\/div>\n<div>$ADForestInfo.Sites<\/div>\n<div>$ADForestInfo.Domains<\/div>\n<div>$ADForestInfo.GlobalCatalogs<\/div>\n<div>$ADForestInfo.<wbr \/>ApplicationPartitions<\/div>\n<div>$ADForestInfo.ForestMode<\/div>\n<div>$ADForestInfo.RootDomain<\/div>\n<div>$ADForestInfo.Schema<\/div>\n<div>$ADForestInfo.SchemaRoleOwner<\/div>\n<div>$ADForestInfo.NamingRoleOwner<\/div>\n<div><\/div>\n<div># OR<\/div>\n<div><\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().Name<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().Sites<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().Domains<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().<wbr \/>GlobalCatalogs<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().<wbr \/>ApplicationPartitions<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().ForestMode<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().RootDomain<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().Schema<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().<wbr \/>SchemaRoleOwner<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Forest]::<wbr \/>GetCurrentForest().<wbr \/>NamingRoleOwner<\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div>###<\/div>\n<div><\/div>\n<div># Get Active Directory Domain Information<\/div>\n<div>\u00a0 # Target the current (local) computer&#8217;s domain:<\/div>\n<div>\u00a0 $ADDomainInfo = [System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetComputerDomain()<\/div>\n<div>\u00a0 # Target the current user&#8217;s domain:<\/div>\n<div>\u00a0 $ADDomainName = [System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCurrentDomain()<\/div>\n<div><\/div>\n<div><\/div>\n<div>$ADDomainInfo.Forest<\/div>\n<div>$ADDomainInfo.<wbr \/>DomainControllers<\/div>\n<div>$ADDomainInfo.Children<\/div>\n<div>$ADDomainInfo.DomainMode<\/div>\n<div>$ADDomainInfo.Parent<\/div>\n<div>$ADDomainInfo.PdcRoleOwner<\/div>\n<div>$ADDomainInfo.RidRoleOwner<\/div>\n<div>$ADDomainInfo.<wbr \/>DomainControllers<\/div>\n<div><\/div>\n<div># OR<\/div>\n<div><\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCurrentDomain().Forest<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCurrentDomain().<wbr \/>DomainControllers<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCurrentDomain().Children<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCurrentDomain().DomainMode<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCurrentDomain().Parent<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCurrentDomain().<wbr \/>PdcRoleOwner<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCurrentDomain().<wbr \/>RidRoleOwner<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCurrentDomain().<wbr \/>DomainControllers<\/div>\n<div><\/div>\n<div># Note: Use [System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCOMPUTERDomain().Attribute for the local computer&#8217;s domain info.<\/div>\n<div># Example: [System.DirectoryServices.<wbr \/>ActiveDirectory.Domain]::<wbr \/>GetCOMPUTERDomain().Forest<\/div>\n<div><\/div>\n<div>###<\/div>\n<div><\/div>\n<div># Get the local computer&#8217;s site information:<\/div>\n<div>$LocalSiteInfo = [System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite()<\/div>\n<div><\/div>\n<div>$LocalSiteInfo.Name<\/div>\n<div>$LocalSiteInfo.Domains<\/div>\n<div>$LocalSiteInfo.Subnets<\/div>\n<div>$LocalSiteInfo.Servers<\/div>\n<div>$LocalSiteInfo.AdjacentSites<\/div>\n<div>$LocalSiteInfo.SiteLinks<\/div>\n<div>$LocalSiteInfo.<wbr \/>InterSiteTopologyGenerator<\/div>\n<div>$LocalSiteInfo.Options<\/div>\n<div>$LocalSiteInfo.Location<\/div>\n<div>$LocalSiteInfo.<wbr \/>BridgeheadServers<\/div>\n<div>$LocalSiteInfo.<wbr \/>PreferredSmtpBridgeheadServers<\/div>\n<div>$LocalSiteInfo.<wbr \/>PreferredRpcBridgeheadServers<\/div>\n<div>$LocalSiteInfo.<wbr \/>IntraSiteReplicationSchedule<\/div>\n<div><\/div>\n<div># OR<\/div>\n<div><\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().Name<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().Domains<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().Subnets<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().Servers<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().<wbr \/>AdjacentSites<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().SiteLinks<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().<wbr \/>InterSiteTopologyGenerator<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().Options<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().Location<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().<wbr \/>BridgeheadServers<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().<wbr \/>PreferredSmtpBridgeheadServers<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().<wbr \/>PreferredRpcBridgeheadServers<\/div>\n<div>[System.DirectoryServices.<wbr \/>ActiveDirectory.<wbr \/>ActiveDirectorySite]::<wbr \/>GetComputerSite().<wbr \/>IntraSiteReplicationSchedule<\/div>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are times you don&#8217;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 &amp; Get-Domain: # Get Active Directory Forest Information $ADForestInfo = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() $ADForestInfo.Name $ADForestInfo.Sites &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/adsecurity.org\/?p=113\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[42,41,39,575],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-ad-net","tag-getaddomain","tag-getadforest","tag-powershell","item-wrap"],"_links":{"self":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=113"}],"version-history":[{"count":4,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":4820,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/113\/revisions\/4820"}],"wp:attachment":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}