{"id":192,"date":"2014-09-02T15:17:05","date_gmt":"2014-09-02T19:17:05","guid":{"rendered":"http:\/\/blog.metcorp.org\/?p=192"},"modified":"2025-10-13T13:39:49","modified_gmt":"2025-10-13T17:39:49","slug":"powershell-using-active-directory-net-methods-in-powershell-2","status":"publish","type":"post","link":"https:\/\/adsecurity.org\/?p=192","title":{"rendered":"PowerShell: Using Active Directory .Net methods in PowerShell Part 2"},"content":{"rendered":"<p>Powershell has the incredible ability to run some .Net methods natively. \u00a0Some of this data can also be gathered using AD commandlets.<br \/>\n<strong>Read <a title=\"PowerShell: Using Active Directory .Net methods in PowerShell Part 1\" href=\"https:\/\/adsecurity.org\/?p=113\">Part 1<\/a> for others.<\/strong><\/p>\n<p>Here are a few of my favorites.<\/p>\n<p><strong>Get a Computer\u2019s Site:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite()<\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><strong>Get a User\u2019s Domain:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name<br \/>\n<\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><strong>Get a Computer\u2019s Domain:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">\u00a0[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain().Name<\/span><\/p><\/blockquote>\n<p><strong>List Active Directory FSMOs:<\/strong><\/p>\n<blockquote>\n<blockquote><p><span style=\"color: #0000ff;\">([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).SchemaRoleOwner<\/span><br \/>\n<span style=\"color: #0000ff;\">([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).NamingRoleOwner<\/span><\/p><\/blockquote>\n<blockquote><p><span style=\"color: #0000ff;\">([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).InfrastructureRoleOwner<\/span><br \/>\n<span style=\"color: #0000ff;\">([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).PdcRoleOwner<\/span><br \/>\n<span style=\"color: #0000ff;\">([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).RidRoleOwner<\/span><\/p><\/blockquote>\n<\/blockquote>\n<p><strong>List All Domain Controllers in a Domain:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers\u00a0<\/span><\/p><\/blockquote>\n<p><strong>\u00a0Get Active Directory Domain Mode:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">\u00a0[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainMode \u00a0\u00a0<\/span><\/p><\/blockquote>\n<p><strong>\u00a0Get Trusts for current Active Directory Domain:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).GetAllTrustRelationships()<\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><strong>Get Active Directory Forest Name:<\/strong><\/p>\n<blockquote><p><span class=\"Apple-style-span\" style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre; color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Name<\/span><\/p><\/blockquote>\n<p><strong>Get a List of Sites in the Active Directory Forest:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[array] $ADSites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites<\/span><\/p><\/blockquote>\n<p><strong>Get Active Directory Forest Domains:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Domains<\/span><\/p><\/blockquote>\n<p><strong>Get Active Directory Forest Global Catalogs:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().GlobalCatalogs<\/span><\/p><\/blockquote>\n<p><strong>Get Active Directory Forest Application Partitions:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().ApplicationPartitions<\/span><\/p><\/blockquote>\n<p><strong>Get Active Directory Forest Mode:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().ForestMode\u00a0<\/span><\/p><\/blockquote>\n<p><strong>Get Active Directory Forest Root Domain:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().RootDomain\u00a0<\/span><\/p><\/blockquote>\n<p><strong>Get Active Directory Forest Schema DN:<\/strong><\/p>\n<blockquote><p><span style=\"color: #0000ff;\">[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Schema <\/span><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Powershell has the incredible ability to run some .Net methods natively. \u00a0Some of this data can also be gathered using AD commandlets. Read Part 1 for others. Here are a few of my favorites. Get a Computer\u2019s Site: [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite() &nbsp; Get a User\u2019s Domain: [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name &nbsp; Get a Computer\u2019s Domain: \u00a0[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain().Name List Active Directory FSMOs: &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/adsecurity.org\/?p=192\">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":[65,20,66,67,575],"class_list":["post-192","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-netmethods","tag-activedirectory","tag-addomain","tag-adforest","tag-powershell","item-wrap"],"_links":{"self":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/192","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=192"}],"version-history":[{"count":9,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":4821,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions\/4821"}],"wp:attachment":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}