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

Shawn Berta 26 Reputation points
2022-10-20T12:47:56.83+00:00

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,
-SB

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,429 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,507 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaveK 1,851 Reputation points
    2022-10-20T14:01:30.773+00:00

    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 46,551 Reputation points
    2022-10-20T15:25:06.443+00:00

    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 |  
        ForEach-Object{  
            $cname = $_.trim()  # remove leading/trailing whitespace  
            Try{  
                $distinguishedName = (Get-ADComputer $cname -ErrorAction STOP).distinguishedName  
                $DnWithoutCn = $distinguishedName -replace $regex, '$1'  
                [PSCustomObject]@{  
                    ComputerName = $cname  
                    Location     = $DnWithoutCn  
                }  
            }  
            Catch{  
                [PSCustomObject]@{  
                    ComputerName = $cname  
                    Location     = "Unknown"  
                }  
            }  
        } | Export-CSV c:\Junk\loc.csv -NoTypeInformation  
    
    1 person found this answer helpful.

Your answer

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