![sample powershell scripts to list directory contents sample powershell scripts to list directory contents](https://cdn.ttgtmedia.com/rms/onlineimages/howell_move_item_demofolder_mobile.jpg)
This will only check and show a count for users. Note: it will not show if there is a computer object in the OU. If the UserCount value is showing 0, it means that there are no users in the OU. It will show an output with a column UserCount. UserCount = -Filter * -SearchBase $_.DistinguishedName -SearchScope OneLevel).Count Get-ADOrganizationalUnit -Properties CanonicalName -Filter * | Sort-Object CanonicalName |įorEach-Object = Split-Path $_.CanonicalName -Leaf This will show us if there are users present in the OU. We like to get a list of the OUs, including user count with PowerShell. But how do we know if there are users present in the OU? Get a list of all Organizational Units including UserCount with PowerShell
![sample powershell scripts to list directory contents sample powershell scripts to list directory contents](https://cdn-blog.netwrix.com/wp-content/uploads/2018/04/Manage_CL3.png)
The output with all the OUs in AD is a good list. PS C:\> Get-ADOrganizationalUnit -Properties CanonicalName -Filter * | Sort-Object CanonicalName | Format-Table CanonicalName, DistinguishedNameĪlitajran.local/AT Company OU=AT Company,DC=alitajran,DC=localĪlitajran.local/AT Company/Groups OU=Groups,OU=AT Company,DC=alitajran,DC=localĪlitajran.local/AT Company/Servers OU=Servers,OU=AT Company,DC=alitajran,DC=localĪlitajran.local/AT Company/Servers/Exchange OU=Exchange,OU=Servers,OU=AT Company,DC=alitajran,DC=localĪlitajran.local/AT Company/Users OU=Users,OU=AT Company,DC=alitajran,DC=localĪlitajran.local/AT Company/Users/HR OU=HR,OU=Users,OU=AT Company,DC=alitajran,DC=localĪlitajran.local/AT Company/Users/IT OU=IT,OU=Users,OU=AT Company,DC=alitajran,DC=localĪlitajran.local/Domain Controllers OU=Domain Controllers,DC=alitajran,DC=localĪlitajran.local/Microsoft Exchange Security Groups OU=Microsoft Exchange Security Groups,DC=alitajran,DC=local This will show us an OU breakdown structure and is easier to read. We will make use of the Get-ADOrganizationalUnit cmdlet. Get a list of all the OUs in Active Directory. Get a list of all Organizational Units with PowerShell
![sample powershell scripts to list directory contents sample powershell scripts to list directory contents](https://i.stack.imgur.com/5UlVX.png)