Powershel to extract PC names from OU

I like to know how I can extract all computers devices from different OU's using powershell . I would like the output to go to a csv file so that the following information shows for example


and so on
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Give this script a try.  Where you see this:
Get-ADParent $_.distinguishedName -Direction "forward"

You can have "forward" to list the OU in
format, or you can have "backward" to list the OU in


Function Get-ADParent
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]

        [ValidateSet("backward", "forward")]
    $parts = $DistinguishedName -split '(?<![\\]),'
    If ($direction -eq "backward") {
        $parts[1..$($parts.Count-1)] -join ','
    Else {
        $domain = ($parts[1..$($parts.Count-1)] | Where {$_.Substring(0,3) -eq "DC="})
        ($domain -join ".") + "\" + ($parts[($($parts.Count-1)-$domain.Count)..1] -join '\') -Replace "DC=", "" -Replace "OU=", "" -Replace "CN=", ""

Get-ADComputer -Filter {Name -like "DCROY*"} | 
Select Name, @{n="OU";e={Get-ADParent $_.distinguishedName -Direction "forward"}} | Group OU |
ForEach {

Open in new window

Will SzymkowskiSenior Solution ArchitectCommented:
Give this a shot.

Put your OU DistinguishedNames in a text file and save it as c:\ous.txt. Then run the script below...
Import-Module activedirectory
$OUs = Get-Content "c:\ous.txt"
    foreach ($OU in $OUs) {
        Get-ADComputer -Filter * -SearchBase $OU | ft $OU, Name -AutoSize

Open in new window

The easiest way I find to get a path without much effort or dealing with distinguished names and string manipuation is to return the canonicalName. Its sortable and you don't have to muck around with string manipulation unless you really want to. And last but not least you can do it all as a 1 liner.

Get-ADComputer -Filter * -Properties canonicalName | select name, @{n="Path";e={$_.canonicalName -replace $_.name}} | export-csv -notype c:\results.csv

Open in new window

If you specifically just want the direct OU name that's also easy to grab with little effort.

Get-ADComputer -Filter * -Properties canonicalName | select name, @{n="OU";e={split-path (Split-Path $_.CanonicalName -Parent) -Leaf}} | export-csv -notype c:\results.csv

Open in new window

You can be more specific by using the SearchBase and SearchScope parameters and of course by not using a wildcard in the filter.

Caveat to using split-path to split an LDAP path is it reverses the slash from / to \. Easily fixed with a -replace "\\","/" if you really want to.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
One point for clarification.  You say you want a .CSV, and then your example shows something that is not a .CSV.  So which is it that you want?
gd6627Author Commented:
THAnk you very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.