Home » Blog » Active Directory » How to Find Computer OU in Active Directory & Export CSV List 

How to Find Computer OU in Active Directory & Export CSV List 

author
Published By siddharth
Anuraag Singh
Approved By Anuraag Singh
Published On July 23rd, 2024
Reading Time 10 Minutes Reading
Category Active Directory

Even experienced IT administrators sometimes wonder how to find computer OU in Active Directory environment. Getting the exact location of a computer is just part of the process, as they then have to export the results in CSV format. However, depending on the structure of the AD, this is easier said than done. Organizations may find that their AD’s OUs are like a complicated tree, with layers of hierarchical arrangement.

The fact that a computer can simultaneously be part of more than one OU does not help in this assignment. Moreover, if administrators were absent when the AD design was being finalized, then it became all the more difficult to pinpoint the location where a computer is in this digital maze.

Not to worry, as there are multiple methods through which admins can not only locate but also export the results in one go. Before we begin we want to clear out the most confusing aspect of object location i.e. the difference between a group, container, and OU.

Compare Groups, Containers, and OUs in an Active Directory

Feature Group Container OU (Organizational Unit)
Purpose Manages permissions Organizes objects Manages objects & Policies
Members Users & Groups OUs, Users, Groups Users, Groups, OUs, Computers
Group Policy Not applicable Not applicable Can apply Group Policy
Permissions Manages access Limited permissions Manages permissions & delegation
Creation Manual Default & Manual Manual
Example Marketing Team Users container Finance department OU

Now that we know what to look for let’s start with the method that is by far the most requested, i.e. PowerShell.

How to Find OU of a Computer Using PowerShell?

We can not only get the computer location but also change user logon name in Active Directory using PowerShell scripts. The one we are about to use is provided below.

# Import the Active Directory module
Import-Module ActiveDirectory
# Get all computer objects
$computers = Get-ADComputer -Filter * -Property DistinguishedName
# Create a custom object to store computer name and OU
$computerOUs = $computers | ForEach-Object {
    $dnComponents = ($_ | Select-Object -ExpandProperty DistinguishedName).Split(',')
    $ouComponents = $dnComponents | Where-Object { $_ -like 'OU=*' }
    $ouPath = ($ouComponents -join ',').Replace('OU=', '').Replace(',', '/')
    [PSCustomObject]@{
        ComputerName = $_.Name
        OU = $ouPath
    }
}
# Export the results to a CSV file
$computerOUs | Export-Csv -Path "ComputerOUs.csv" -NoTypeInformation
Write-Output "The list of computer OUs has been saved to ComputerOUs.csv"

Explanation of the script

PowerShell

Import-Module ActiveDirectory: Imports the Active Directory module to use its cmdlets.

Get-ADComputer -Filter * -Property DistinguishedName: Retrieves all computer objects, including their distinguished names.

ForEach-Object: Iterates over each computer object to extract and format the OU path.

Split and Where-Object: Splits the DN into components and filters out the ones that represent OUs.

[PSCustomObject]: Creates a custom PowerShell object for each computer with its name and OU path.

Export-Csv: Exports the custom objects to a CSV file.

Next up we cover another code-based method available inside Active Directory environments.

How to Find OU of a Computer Using CMD?

The following command line query helps to bring out the computer location, although some limitations exist. Like not being able to export the results in CSV.

dsquery computer -limit 0 | dsget computer -dn -samid

Command Line

After locating the computers many admins want to export user group membership from Active Directory to get started with a cross forest active directory migration. Moreover, if you feel code and scripts to be technical, then you can access the computer location data with the help of GUI options like ADAC, and ADUC.

Here is an advanced script that churns out the Computer along with its OU

echo Computer,Organizational Unit > C:\Users\Administrator\Desktop\result.csv && dsquery computer | dsget computer -dn -samid | for /f "tokens=1,2* delims=," %a in ('findstr /i "OU="') do @for /f "tokens=1,* delims= " %c in ("%c") do @for /f "tokens=2 delims==" %e in ("%b") do @for /f "tokens=1 delims=$" %f in ("%d") do @(echo Computer: %f, OU: %e & echo %f,%e >> C:\Users\Administrator\Desktop\result.csv)

Computer Location Detection in AD With CMD

Explanation of the Query
With this command, you can search the entire AD for all computer objects (Domain Controllers are also treated as Computers by the AD) and list out the Organizational Unit of that computer.

Create a blank CSV with just the headers (Computers and Organizational Unit). Then the query iterates to find AD computer objects and pull out the  Distinguished Name and SamID. Then it scans for the  “OU=”, which it uses to separate the OU from the rest of the computer object details.

It uses loops to perform the required task and prints out the result in the console. Simultaneously the admin receives a CSV file output for documentation purposes.

You can convert this into a batch script which adds reusability. Note if you don’t change the csv file path it overwrites the existing report. The script is designed only to fetch two things the computer name and its OU. Moreover, the above query fails for edge cases like Nested OU which is quite a common scenario in modern AD environments. Besides that, complexity makes admins wonder if there is any other way to find what OU a computer is in. Yes, there is.

Default Active Directory Options to Get Computer’s Path

There are two options for admins first one is ADUC or active directory users and computers portal.

  • Open ADUC
  • Click on the search icon.
  • Select Computers > Click on Find Now.
  • Choose any one of the results
  • On the Properties Window > toggle “Members Of” and view the location.

use ADUC and Find Computer OU in Active Directory

Sometimes the ADUC may not be installed in your AD environment. In that case, instead of asking how to find computer OU in Active Directory use the Admin Center instead.

  • Launch ADAC
  • Go to Global Search
  • Toggle Convert to LDAP and type (&(objectClass=computer))
  • Click on Apply > See the Path Column for Computer location Data.

How to Find Computer OU in Active Directory via ADAC

However, none of the two traditional GUI methods allow admins to export the results in CSV format. Not to mention they they need to check for each Computer object one by one as the location won’t be visible en-mass.

Significantly increasing the overall time of the operation. Admins can view the OU and try to filter out computer objects how ever this is counterintuitive and may only be a marginal upgrade over the default method. Therefore, a better approach would be to skip over the traditional methods altogether.

Smart Way to Get Computer List from Active Directory

Would be to use the SysTools Active Directory Reporting Software. Designed with a user-first approach the tool simplifies finding computer location in any AD environment.  Moreover, it provides the CSV export ability of PowerShell along with the easiness of the GUI not found even in the native AD management tools.

Download Now Purchase Now

So check out the following steps and get the path of a computer object in AD

How to Find Computer OU in Active Directory via Automated Utility Step-By-Step

  • Step 1. Launch the software on your machine. Wait for the default administrator credentials to fill up and log in.
  • Step 2. Select the “REGISTER DOMAIN CONTROLLER” button and fill in the details, like the domain-friendly name and IP address.
  • Step 3. Go to the Domain Details Page in the Info section, add your admin details, and validate the domain.
  • Step 4. Then go to the Reports column under the Computers workload and choose the All category.
  • Step 5. Click on the Preview button and check the OU column.
  • Step 6. Expand the Download button and select CSV.

Compare and Contrast the Methods

Due to the availability of multiple methods admins might get confused on what to use so here is a tabular comparison matrix highlighting the pros and cons of the various methods described in this post.

Method Difficulty Functionality Export to CSV?
Active Directory Users and Computers (ADUC) Easy View OU membership for individual computers No
Active Directory Administrative Center (ADAC) Easy Search OUs using LDAP queries, limited information No (Bypass Via Copy/Pasting)
Command Line Queries Moderate Find computer OU using CMD queries Possible
PowerShell Scripting Moderate Powerful filtering and data manipulation, bulk retrieval Yes (With Additional Code)
Professional Reporting Tools (e.g., SysTools) Easy User-friendly interface, pre-built reports, extensive filtering Yes

It is now more clear than ever that the automated utility offers the best means of computer OU retrival.

Conclusion

In this write-up, we taught users how to find computer OU in Active Directory.  So now they should have no problems in formulating a new AD Audit Checklist. Moreover, when locating computers they can either use the Command line or PowerShell scripts both can export out a CSV file. However, the complexity involved may turn some users away. So instead of using command line query or inbuilt GUI tools that lack CSV export options, administrators can use the professional tool instead.

Frequently Asked Questions on How to Find Computer OU in Active Directory

I want the OU of a particular computer I am using Get-ADComputer cmdlet, but I only remember the first few characters of the computer name?

You can substitute the missing characters with a wild card symbol. In both PowerShell and LDAP the asterix ( * ) symbol will do the trick. Otherwise, you can also use other filtering options if similar to the one that are used to locate inactive computers in AD.

Is it Possible for one computer object to exist in more than one OU how does the CSV report behave in such a situation?

When admins search for how to find computer ou using cmd, PowerShell, or any other previously unknown method on thing does not change that Computers follow a one to one mapping with their OU. Even when there is OU nesting a computer can only have one primary OU, but it can inherit Group Policies from parent OUs in the hierarchy. However, within a OU there can be multiple computer objects.

The CSV reflects the same it shows the entire OU path until of the object. Only the PowerShell and Tool possess this ability for command line admins have to add in the functionality themselves.

I know for sure that there is a computer object in the OU but during the search, it won’t show up at all how is this possible?

There can be several reasons for which the computer is absent from your searches.

It is possible that a the computer object is deleted. To verify check the Event Viewer logs.

Your privilege to use cmd or PowerShell may have been revoked by admin so consult with the relevant figures in your organization regarding the same.

Why my Computer location CSV contains Domain Controllers?

Every Active Directory treats DCs as computer objects.

Connect With Us

+9111-28084986