Share via

Get-ADUser SearchBase

Rising Flight 6,456 Reputation points
2025-11-26T04:22:35.9233333+00:00

Hi all,

I am trying to export all users from a specific OU and its sub-OUs in Active Directory.

What I need in the CSV:

  • OU name
  • Enabled/Disabled status
  • sAMAccountName
  • UserPrincipalName
  • Email address
  • employeeID

-employeeType

Lets say My OU path is: contoso.com/OU1, under OU1 i have atleast 200 Sub OUs

OU1 and sub OUs also have computer objects, i am looking to export users. please guide me with powershell syntax.

Windows for business | Windows Server | Directory services | Active Directory
0 comments No comments

Answer accepted by question author

Domic Vo 24,130 Reputation points Independent Advisor
2025-11-26T11:27:13.4266667+00:00

Hi Rising Flight,

你需要的是从某个 OU 及其所有子 OU 中导出用户信息,同时排除计算机对象,并保存到 CSV 文件。PowerShell 是最佳工具,但语法要写得准确,才能保证数据完整。

操作步骤

先加载 Active Directory 模块:

powershell

Import-Module ActiveDirectory

查询 OU 和子 OU 中的用户并导出:

powershell

Get-ADUser -SearchBase "OU=OU1,DC=contoso,DC=com" -SearchScope Subtree -Filter * `
-Properties Enabled, sAMAccountName, UserPrincipalName, mail, employeeID, employeeType |
Select-Object Enabled, sAMAccountName, UserPrincipalName, mail, employeeID, employeeType, DistinguishedName |
Export-Csv "C:\ADUsers.csv" -NoTypeInformation -Encoding UTF8

小提示

  • 如果只想要启用的用户,可以改成 -Filter {Enabled -eq $true}

OU 名称解析可能比较复杂,直接保留 DistinguishedName 会更直观。

建议先在小范围 OU 测试,避免一次性导出过多数据。

总结Get-ADUser 搭配 -SearchScope Subtree,选择所需属性并导出到 CSV,就能得到干净的用户清单,而不会包含计算机对象。

Domic Vo.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AI answer

    2025-11-26T04:22:49.27+00:00

    To export all users from a specific Organizational Unit (OU) and its sub-OUs in Active Directory, you can use the Get-ADUser cmdlet along with the -SearchBase parameter. Here’s a PowerShell script that will help you achieve this, including the required properties in the CSV file:

    # Define the OU path
    $OUPath = "OU=OU1,DC=contoso,DC=com"
    
    # Get all users in the specified OU and its sub-OUs
    $users = Get-ADUser -Filter * -SearchBase $OUPath -Properties Enabled, sAMAccountName, UserPrincipalName, EmailAddress, employeeID, employeeType
    
    # Select the required properties and add the OU name
    $exportData = $users | Select-Object @{Name='OU'; Expression={"OU1"}}, Enabled, sAMAccountName, UserPrincipalName, EmailAddress, employeeID, employeeType
    
    # Export to CSV
    $exportData | Export-Csv -Path "C:\Path\To\Your\ExportedUsers.csv" -NoTypeInformation
    

    Explanation:

    • $OUPath: Set this variable to the distinguished name of your target OU.
    • Get-ADUser: This cmdlet retrieves all user objects from the specified OU and its sub-OUs.
    • -Filter *: This retrieves all users without any filtering.
    • -Properties: This specifies the additional properties you want to include in the output.
    • Select-Object: This is used to format the output, including the OU name and the desired properties.
    • Export-Csv: This cmdlet exports the data to a CSV file at the specified path.

    Make sure to adjust the path in the Export-Csv command to your desired output location.

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.