Skip to main content

PowerShell Get-ADOrganizationalUnit: Query Active Directory OUs

• 3 min read
powershell active-directory get-adorganizationalunit ou-management tutorial

PowerShell Get-ADOrganizationalUnit: Complete Guide to Querying OUs

Overview

The Get-ADOrganizationalUnit cmdlet retrieves organizational unit objects from Active Directory. Used for querying, filtering, and analyzing OU structure and properties.

Common Tasks:

  • List all OUs in domain
  • Find specific OUs by name or path
  • Query OU properties and structure
  • Check OU descriptions
  • Verify OU paths for scripting

Prerequisites:

  • PowerShell 5.1 or later
  • Active Directory PowerShell module
  • Domain user or administrator permissions
  • Read access to Active Directory

Syntax

Get-ADOrganizationalUnit [-Identity] <ADOrganizationalUnit> [-Properties <string[]>]
Get-ADOrganizationalUnit -Filter <string> [-SearchBase <string>] [-SearchScope <ADSearchScope>]
```powershell

### Key Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `-Identity` | ADOrganizationalUnit | OU to retrieve (DN, GUID, or name) |
| `-Filter` | String | LDAP-style filter for searching |
| `-SearchBase` | String | OU distinguished name to search from |
| `-SearchScope` | ADSearchScope | Base, OneLevel, or Subtree |
| `-Properties` | String[] | Attributes to retrieve |

### SearchScope Options

- **Base** - Only the specified OU
- **OneLevel** - Direct children only (not nested)
- **Subtree** - All nested levels (default)

---

## Examples

### Example 1: Get All OUs

```powershell
Get-ADOrganizationalUnit -Filter * | Select-Object Name, DistinguishedName
```powershell

**Output:**
```powershell
Name         DistinguishedName
----         -----------------
Users        OU=Users,DC=contoso,DC=com
Computers    OU=Computers,DC=contoso,DC=com
Finance      OU=Finance,OU=Users,DC=contoso,DC=com
IT           OU=IT,OU=Users,DC=contoso,DC=com
```powershell

### Example 2: Get Specific OU by Name

```powershell
$ou = Get-ADOrganizationalUnit -Filter "Name -eq 'Finance'"
Write-Host "OU: $($ou.Name)"
Write-Host "Path: $($ou.DistinguishedName)"
```powershell

### Example 3: Get OU by Distinguished Name

```powershell
Get-ADOrganizationalUnit -Identity "OU=Finance,OU=Users,DC=contoso,DC=com"
```powershell

### Example 4: Get Top-Level OUs Only

```powershell
Get-ADOrganizationalUnit -Filter * -SearchBase "DC=contoso,DC=com" `
    -SearchScope OneLevel
```powershell

### Example 5: Find OUs Matching Pattern

```powershell
Get-ADOrganizationalUnit -Filter "Name -like '*Finance*'" |
Select-Object Name, DistinguishedName
```powershell

### Example 6: Get OUs with Specific Properties

```powershell
Get-ADOrganizationalUnit -Filter * -Properties * |
Select-Object Name, Description, Created, Modified
```powershell

### Example 7: Get OUs Protected from Deletion

```powershell
Get-ADOrganizationalUnit -Filter * -Properties ProtectedFromAccidentalDeletion |
Where-Object { $_.ProtectedFromAccidentalDeletion -eq $true } |
Select-Object Name
```powershell

### Example 8: Get OUs in Specific Location

```powershell
$searchBase = "OU=Boston,DC=contoso,DC=com"
Get-ADOrganizationalUnit -Filter * -SearchBase $searchBase |
Select-Object Name, DistinguishedName
```powershell

### Example 9: Get OU Count

```powershell
$ouCount = (Get-ADOrganizationalUnit -Filter * -SearchScope Subtree).Count
Write-Host "Total OUs: $ouCount"
```powershell

### Example 10: Find Empty OUs

```powershell
Get-ADOrganizationalUnit -Filter * -Properties * |
ForEach-Object {
    $objectCount = (Get-ADObject -Filter * -SearchBase $_.DistinguishedName).Count
    if ($objectCount -eq 0) {
        Write-Host "Empty: $($_.Name)"
    }
}
```powershell

---

## Filter Examples

### Filter by Name

```powershell
# Exact match
Get-ADOrganizationalUnit -Filter "Name -eq 'Finance'"

# Wildcard search
Get-ADOrganizationalUnit -Filter "Name -like '*Finance*'"
```powershell

### Filter by Description

```powershell
Get-ADOrganizationalUnit -Filter "Description -like '*department*'"
```powershell

### Combined Filters

```powershell
# Find OUs with specific name pattern and specific parent
Get-ADOrganizationalUnit -Filter "Name -like 'OU_*'" `
    -SearchBase "OU=Users,DC=contoso,DC=com" `
    -SearchScope OneLevel
```powershell

---

## Common Usage Scenarios

### Scenario 1: List OU Structure

```powershell
function Show-OUStructure {
    param(
        [string]$SearchBase = (Get-ADDomain).DistinguishedName,
        [int]$Level = 0
    )

    Get-ADOrganizationalUnit -Filter * -SearchBase $SearchBase -SearchScope OneLevel |
    ForEach-Object {
        $indent = "  " * $Level
        Write-Host "$indent├─ $($_.Name)"
        Show-OUStructure -SearchBase $_.DistinguishedName -Level ($Level + 1)
    }
}

Show-OUStructure
```powershell

### Scenario 2: OU Inventory Report

```powershell
$report = Get-ADOrganizationalUnit -Filter * -Properties Description |
Select-Object Name, @{Name="Path";Expression={$_.DistinguishedName}}, Description |
Sort-Object Name

$report | Out-GridView
# Or export to CSV
$report | Export-Csv -Path "C:\OU_Inventory.csv" -NoTypeInformation
```powershell

### Scenario 3: Verify OU Exists Before Operation

```powershell
$ouName = "Finance"
$searchBase = "DC=contoso,DC=com"

try {
    $ou = Get-ADOrganizationalUnit -Filter "Name -eq '$ouName'" `
        -SearchBase $searchBase -ErrorAction Stop
    Write-Host "✓ OU found: $($ou.DistinguishedName)"
}
catch {
    Write-Host "✗ OU not found: $ouName"
}
```powershell

### Scenario 4: Get Objects in OU

```powershell
$ou = Get-ADOrganizationalUnit -Filter "Name -eq 'Finance'"

# Count objects
$objectCount = (Get-ADObject -Filter * -SearchBase $ou.DistinguishedName).Count
Write-Host "Objects in OU: $objectCount"

# List users in OU
Get-ADUser -Filter * -SearchBase $ou.DistinguishedName | Select-Object Name, SamAccountName
```powershell

---

## Best Practices

✅ **Always specify SearchBase when appropriate**
- Reduces search scope
- Improves query performance
- Avoids unintended results

✅ **Use OneLevel for direct children**
- When you only need immediate children
- Much faster than Subtree searches

✅ **Store DistinguishedName in variables**
- Use for subsequent operations
- More reliable than paths

✅ **Handle multiple results**
- Some filters may return multiple OUs
- Use Where-Object to narrow results

✅ **Request only needed properties**
- Include -Properties only when required
- Reduces memory usage for large queries

---

## Troubleshooting

### Problem: No OUs Returned

```powershell
# Verify domain context
Get-ADDomain

# Check if filter is too restrictive
Get-ADOrganizationalUnit -Filter "Name -eq 'Finance'" -SearchBase "DC=contoso,DC=com"

# Try wildcard to test
Get-ADOrganizationalUnit -Filter "Name -like '*'"
```powershell

### Problem: OU Not Found by Name

```powershell
# Search for similar names
Get-ADOrganizationalUnit -Filter "Name -like '*Finance*'"

# Get all OUs in specific path
$searchBase = "OU=Users,DC=contoso,DC=com"
Get-ADOrganizationalUnit -Filter * -SearchBase $searchBase
```powershell

---

## FAQs

### Q: How do I get the root OUs only?
A: Use `-SearchScope OneLevel` on the domain root.
```powershell
Get-ADOrganizationalUnit -Filter * `
    -SearchBase "DC=contoso,DC=com" `
    -SearchScope OneLevel
```powershell

### Q: How do I count all OUs?
A: Use measure-object or .Count property.
```powershell
(Get-ADOrganizationalUnit -Filter *).Count
```powershell

### Q: Can I get OUs with GPOs linked?
A: No direct property, but you can cross-reference with Get-GPLink.

### Q: How do I get OU creation date?
A: Use -Properties with * or "Created".
```powershell
Get-ADOrganizationalUnit -Filter * -Properties Created | Select-Object Name, Created
```powershell

---

## Related Commands

- **[New-ADOrganizationalUnit](/powershell-new-adorganizationalunit)** - Create OUs
- **[Set-ADOrganizationalUnit](/powershell-set-adorganizationalunit)** - Modify OUs
- **[Remove-ADOrganizationalUnit](/powershell-remove-adorganizationalunit)** - Delete OUs
- **[Move-ADObject](/powershell-move-objects-ou)** - Move objects between OUs

---

## See Also

- **[Active Directory OU Overview](/active-directory-ou)** - OU concepts
- **[Active Directory OU Structure](/active-directory-ou-structure)** - Design patterns
- **[PowerShell New-ADOrganizationalUnit](/powershell-new-adorganizationalunit)** - Create OUs

---

**Last Updated:** February 6, 2026
**Difficulty Level:** Intermediate
**Reading Time:** 8 minutes