{"id":2068,"date":"2017-04-14T18:52:43","date_gmt":"2017-04-14T18:52:43","guid":{"rendered":"http:\/\/microsoftgeek.com\/?p=2068"},"modified":"2017-04-14T18:52:43","modified_gmt":"2017-04-14T18:52:43","slug":"powershell-get-a-list-of-my-domain-organizational-units","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=2068","title":{"rendered":"PowerShell &#8211; Get a list of my domain Organizational Units"},"content":{"rendered":"<h3 class=\"post-title entry-title\">PowerShell &#8211; Get a list of my domain Organizational Units<\/h3>\n<div class=\"post-header\"><\/div>\n<div id=\"post-body-2154364079146164595\" class=\"post-body entry-content\"><img decoding=\"async\" src=\"http:\/\/3.bp.blogspot.com\/-Em-Upc0k14Q\/Uz9dluV1yJI\/AAAAAAABj_E\/7EQ9SKkSLcM\/s1600\/1396679449_active_directory.png\" border=\"0\" \/><\/div>\n<div class=\"post-body entry-content\">Quick post, last week my coworker Andrey needed to list all the Organization Units in the domain by Canonical Name. I thought sharing the PowerShell One-Liner magic could save time to some people out there.<\/p>\n<p>In the following examples two methods to retrieve the information using<b>\u00a0Active Directory<\/b> and <b>ADSI\/NET<\/b>.<\/p>\n<h1>Active Directory Module<\/h1>\n<p>I found two ways to get this information using this module<\/p>\n<ul>\n<li>Get-ADOrganizationUnit<\/li>\n<li>Get-ADObject<\/li>\n<\/ul>\n<p>First we need to verify if the module is loaded and then search for Cmdlet that could meet our needs.<\/p>\n<pre># Check if the ActiveDirectory module is Loaded\r\nGet-Module -Name ActiveDirevtory\r\n\r\n# Check if the ActiveDirectory module is available\r\nGet-Module -Name ActiveDirectory -ListAvailable\r\n\r\n# Import the ActiveDirectory module\r\nImport-Module -Name ActiveDirectory\r\n\r\n# Find Cmdlets in the ActiveDirectory related to OrganizationalUnit\r\nGet-Command -Module ActiveDirectory -Name *OrganizationalUnit*<\/pre>\n<div class=\"separator\"><img decoding=\"async\" src=\"http:\/\/1.bp.blogspot.com\/-09sADPuRluY\/Uz9m3Odp-dI\/AAAAAAABj_g\/QetfmN9xTjo\/s1600\/2014-04-04_21-39-53.png\" border=\"0\" \/><\/div>\n<h1>Get-ADOrganizationalUnit<\/h1>\n<p>The Get-ADOrganizational unit cmdlet gets an organizational unit object or performs a search to retrieve multiple organizational units.<\/p>\n<p>Straight forward, we look for the properties available to us.<\/p>\n<pre># Get the properties available for each OrganizationalUnit object\r\nGet-ADOrganizationalUnit -Filter * -Properties *| Get-Member<\/pre>\n<p><b><u>Output:<\/u><\/b><\/p>\n<pre class=\"PoshConsoleSmall\">   TypeName: Microsoft.ActiveDirectory.Management.ADOrganizationalUnit\r\n\r\nName                            MemberType            Definition\r\n----                            ----------            ----------\r\nContains                        Method                bool Contains(string propertyName)\r\nEquals                          Method                bool Equals(System.Object obj)\r\nGetEnumerator                   Method                System.Collections.IDictionaryEnumerator G...\r\nGetHashCode                     Method                int GetHashCode()\r\nGetType                         Method                type GetType()\r\nToString                        Method                string ToString()\r\nItem                            ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPro...\r\nCanonicalName                   Property              System.String CanonicalName {get;}\r\nCity                            Property              System.String City {get;set;}\r\nCN                              Property              System.String CN {get;}\r\nCountry                         Property              System.String Country {get;set;}\r\nCreated                         Property              System.DateTime Created {get;}\r\ncreateTimeStamp                 Property              System.DateTime createTimeStamp {get;}\r\nDeleted                         Property              System.Boolean Deleted {get;}\r\nDescription                     Property              System.String Description {get;set;}\r\nDisplayName                     Property              System.String DisplayName {get;set;}\r\nDistinguishedName               Property              System.String DistinguishedName {get;set;}\r\ndSCorePropagationData           Property              Microsoft.ActiveDirectory.Management.ADPro...\r\ngPLink                          Property              System.String gPLink {get;set;}\r\ninstanceType                    Property              System.Int32 instanceType {get;}\r\nisCriticalSystemObject          Property              System.Boolean isCriticalSystemObject {get...\r\nisDeleted                       Property              System.Boolean isDeleted {get;}\r\nLastKnownParent                 Property              System.String LastKnownParent {get;}\r\nLinkedGroupPolicyObjects        Property              Microsoft.ActiveDirectory.Management.ADPro...\r\nManagedBy                       Property              System.String ManagedBy {get;set;}\r\nModified                        Property              System.DateTime Modified {get;}\r\nmodifyTimeStamp                 Property              System.DateTime modifyTimeStamp {get;}\r\nName                            Property              System.String Name {get;}\r\nnTSecurityDescriptor            Property              System.DirectoryServices.ActiveDirectorySe...\r\nObjectCategory                  Property              System.String ObjectCategory {get;}\r\nObjectClass                     Property              System.String ObjectClass {get;set;}\r\nObjectGUID                      Property              System.Nullable`1[[System.Guid, mscorlib, ...\r\nou                              Property              Microsoft.ActiveDirectory.Management.ADPro...\r\nPostalCode                      Property              System.String PostalCode {get;set;}\r\nProtectedFromAccidentalDeletion Property              System.Boolean ProtectedFromAccidentalDele...\r\nsDRightsEffective               Property              System.Int32 sDRightsEffective {get;}\r\nshowInAdvancedViewOnly          Property              System.Boolean showInAdvancedViewOnly {get...\r\nState                           Property              System.String State {get;set;}\r\nStreetAddress                   Property              System.String StreetAddress {get;set;}\r\nsystemFlags                     Property              System.Int32 systemFlags {get;}\r\nuSNChanged                      Property              System.Int64 uSNChanged {get;}\r\nuSNCreated                      Property              System.Int64 uSNCreated {get;}\r\nwhenChanged                     Property              System.DateTime whenChanged {get;}\r\nwhenCreated                     Property              System.DateTime whenCreated {get;}<\/pre>\n<div class=\"separator\"><\/div>\n<p>Now we just have to filter on the property CanonicalName.<\/p>\n<pre># Get the property CanonicalName for each Organizational Unit\r\nGet-ADOrganizationalUnit -Filter * -Properties CanonicalName | Select-Object -Property CanonicalName<\/pre>\n<div class=\"separator\"><\/div>\n<p><b><u>Output:<\/u><\/b><\/p>\n<pre class=\"PoshConsoleSmall\">CanonicalName\r\n-------------\r\nFX.LAB\/Domain Controllers\r\nFX.LAB\/TEST\r\nFX.LAB\/TEST\/Groups\r\nFX.LAB\/MTL\r\nFX.LAB\/Administration\r\nFX.LAB\/Administration\/Local\r\nFX.LAB\/Administration\/Global\r\nFX.LAB\/Administration\/Admin Users\r\nFX.LAB\/Administration\/ServiceAccounts\r\nFX.LAB\/Administration\/Test Jonathan OU\r\nFX.LAB\/TEST\/Users\r\nFX.LAB\/TEST\/Servers\r\nFX.LAB\/Winter Scripting Games 2014\r\nFX.LAB\/Winter Scripting Games 2014\/Event3\r\nFX.LAB\/Winter Scripting Games 2014\/Event3\/Finance Department\r\n<\/pre>\n<h1>Get-ADObject<\/h1>\n<div>The Get-ADObject cmdlet gets an Active Directory object or performs a search to retrieve multiple objects.<\/p>\n<pre># Get Organizational Unit objects\r\nGet-ADObject -Filter { ObjectClass -eq 'organizationalunit' }<\/pre>\n<\/div>\n<div class=\"separator\"><img decoding=\"async\" src=\"http:\/\/1.bp.blogspot.com\/-woYqM6FdiVA\/Uz9ul3R19uI\/AAAAAAABkAA\/k0vC09P0lS0\/s1600\/2014-04-04_22-45-52.png\" border=\"0\" \/><\/div>\n<pre># Get Organizational Unit objects\r\nGet-ADObject -Filter { ObjectClass -eq 'organizationalunit' } -Properties\u00a0CanonicalName | Select-Object -Property CanonicalName<\/pre>\n<div class=\"separator\"><\/div>\n<p><b><u>Output:<\/u><\/b><\/p>\n<pre class=\"PoshConsoleSmall\">CanonicalName\r\n-------------\r\nFX.LAB\/Domain Controllers\r\nFX.LAB\/TEST\r\nFX.LAB\/TEST\/Groups\r\nFX.LAB\/MTL\r\nFX.LAB\/Administration\r\nFX.LAB\/Administration\/Local\r\nFX.LAB\/Administration\/Global\r\nFX.LAB\/Administration\/Admin Users\r\nFX.LAB\/Administration\/ServiceAccounts\r\nFX.LAB\/Administration\/Test Jonathan OU\r\nFX.LAB\/TEST\/Users\r\nFX.LAB\/TEST\/Servers\r\nFX.LAB\/Winter Scripting Games 2014\r\nFX.LAB\/Winter Scripting Games 2014\/Event3\r\nFX.LAB\/Winter Scripting Games 2014\/Event3\/Finance Department\r\n<\/pre>\n<h1>ADSI\/NET<\/h1>\n<p>Finally, the ADSI method! This technique is a bit more complex, but this does not require any module\/snapin and can be run from PowerShell without any pre-requisites.<\/p>\n<pre>$info = ([adsisearcher]\"objectclass=organizationalunit\")\r\n$info.PropertiesToLoad.AddRange(\"CanonicalName\")\r\n$info.findall().properties.canonicalname<\/pre>\n<div class=\"separator\"><img decoding=\"async\" src=\"http:\/\/2.bp.blogspot.com\/-ILGQwSV-s1Q\/Uz-AcvXD5_I\/AAAAAAABkAc\/nXl7XlcGLdo\/s1600\/2014-04-05_0-01-25.png\" border=\"0\" \/><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell &#8211; Get a list of my domain Organizational Units Quick post, last week my coworker Andrey needed to list all the Organization Units in the domain by Canonical Name. I thought sharing the PowerShell One-Liner magic could save time to some people out there. In the following examples two methods to retrieve the information [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-2068","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2068"}],"version-history":[{"count":1,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2068\/revisions"}],"predecessor-version":[{"id":2069,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2068\/revisions\/2069"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}