PoshTip #26 – Get a list of AD Organizational Units
First, you need to import the Active Directory module:
Import-Module ActiveDirectory
Now you can use the “Get-ADOrganizationalUnit” cmdlet with the “-filter” parameter:
Get-ADOrganizationalUnit -Filter 'Name -like "*"' | Format-Table Name, DistinguishedName
Note: The Format–Table cmdlet formats the output as a table with the “Name” and “DistinguishedName” properties.
Name DistinguishedName ---- ----------------- Domain Controllers OU=Domain Controllers,DC=Demo,DC=local PROD OU=PROD,DC=Demo,DC=local LAB OU=LAB,DC=Demo,DC=local DEV OU=DEV,DC=Demo,DC=local
You can get all the filters available with the “Get-Member” cmdlet:
Get-ADOrganizationalUnit -Filter *| Get-Member TypeName: Microsoft.ActiveDirectory.Management.ADOrganizationalUnit Name MemberType Definition ---- ---------- ---------- Contains Method bool Contains(string propertyName) Equals Method bool Equals(System.Object obj) GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator() GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() Item ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(s... City Property System.String City {get;set;} Country Property System.String Country {get;set;} DistinguishedName Property System.String DistinguishedName {get;set;} LinkedGroupPolicyObjects Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Linked... ManagedBy Property System.String ManagedBy {get;set;} Name Property System.String Name {get;} ObjectClass Property System.String ObjectClass {get;set;} ObjectGUID Property System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=ne... PostalCode Property System.String PostalCode {get;set;} State Property System.String State {get;set;} StreetAddress Property System.String StreetAddress {get;set;}
There are also many properties that can be used:
Get-ADOrganizationalUnit -Filter * -Properties *| Get-Member TypeName: Microsoft.ActiveDirectory.Management.ADOrganizationalUnit Name MemberType Definition ---- ---------- ---------- Contains Method bool Contains(string propertyName) Equals Method bool Equals(System.Object obj) GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator() GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() Item ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection... CanonicalName Property System.String CanonicalName {get;} City Property System.String City {get;set;} CN Property System.String CN {get;} Country Property System.String Country {get;set;} Created Property System.DateTime Created {get;} createTimeStamp Property System.DateTime createTimeStamp {get;} Deleted Property System.Boolean Deleted {get;} Description Property System.String Description {get;set;} DisplayName Property System.String DisplayName {get;set;} DistinguishedName Property System.String DistinguishedName {get;set;} dSCorePropagationData Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection... gPLink Property System.String gPLink {get;set;} instanceType Property System.Int32 instanceType {get;} isCriticalSystemObject Property System.Boolean isCriticalSystemObject {get;set;} isDeleted Property System.Boolean isDeleted {get;} LastKnownParent Property System.String LastKnownParent {get;} LinkedGroupPolicyObjects Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection... ManagedBy Property System.String ManagedBy {get;set;} Modified Property System.DateTime Modified {get;} modifyTimeStamp Property System.DateTime modifyTimeStamp {get;} Name Property System.String Name {get;} nTSecurityDescriptor Property System.DirectoryServices.ActiveDirectorySecurity nTSecurityDes... ObjectCategory Property System.String ObjectCategory {get;} ObjectClass Property System.String ObjectClass {get;set;} ObjectGUID Property System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Cul... ou Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection... PostalCode Property System.String PostalCode {get;set;} ProtectedFromAccidentalDeletion Property System.Boolean ProtectedFromAccidentalDeletion {get;set;} sDRightsEffective Property System.Int32 sDRightsEffective {get;} showInAdvancedViewOnly Property System.Boolean showInAdvancedViewOnly {get;set;} State Property System.String State {get;set;} StreetAddress Property System.String StreetAddress {get;set;} systemFlags Property System.Int32 systemFlags {get;} uSNChanged Property System.Int64 uSNChanged {get;} uSNCreated Property System.Int64 uSNCreated {get;} whenChanged Property System.DateTime whenChanged {get;} whenCreated Property System.DateTime whenCreated {get;}
Now you are able to retrieve all the Organizational Units with “WhenCreated” and “WhenChanged” properties:
Get-ADOrganizationalUnit -Filter * -Properties CanonicalName,WhenCreated,WhenChanged | Select-Object -Property CanonicalName,WhenCreated,WhenChanged CanonicalName WhenCreated WhenChanged ------------- ----------- ----------- Demo.local/Domain Controllers 29.08.2013 16:06:23 17.07.2015 16:16:51 Demo.local/PROD 07.11.2013 12:42:55 31.07.2015 15:53:09 Demo.local/LAB 07.11.2013 12:43:25 25.06.2014 11:52:49 Demo.local/DEV 07.11.2013 12:43:35 03.12.2013 11:55:39
Thanks for reading! You can follow me on Twitter @PrigentNico