How to collect where/ what OU specified Computers in AD are "published at"...

Shawn Berta 26 Reputation points

Hello Everyone,

I hope all is well with you and yours. I wanted to ask if anyone could help in creating a PowerShell script that would use a Text file list of current systems in AD, to find which OU the systems are located in, in AD. Basically I like to use a list of systems from a (.txt) file, and get the systems "published at" path. Please let me know if more information about this request is needed. Thank you in advance, and take care.

Thank you,

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,152 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,174 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaveK 1,846 Reputation points

    Hi, Should be a fairly simple one if I understand what your after.

    Get-Content -Path .\servers.txt | Get-ADComputer -Properties CanonicalName | Select Name, CanonicalName  

    Server.txt is just a plain text list of servers, one per line. If you wish to save the output you could you could add a Export-CSV to the end making it something like

    Get-Content -Path .\servers.txt | Get-ADComputer -Properties CanonicalName | Select Name, CanonicalName | Export-Csv -Path server_paths.csv  -NoClobber -NoTypeInformation  
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 42,906 Reputation points

    By "OU" I'm assuming you mean the distinguished name of the container in which computer object was found.

    $regex = 'CN=.*?(?<!\\),(.*)'  
    Get-Content C:\Junk\Computers.txt |  
            $cname = $_.trim()  # remove leading/trailing whitespace  
                $distinguishedName = (Get-ADComputer $cname -ErrorAction STOP).distinguishedName  
                $DnWithoutCn = $distinguishedName -replace $regex, '$1'  
                    ComputerName = $cname  
                    Location     = $DnWithoutCn  
                    ComputerName = $cname  
                    Location     = "Unknown"  
        } | Export-CSV c:\Junk\loc.csv -NoTypeInformation  
    1 person found this answer helpful.