Consult Users and desktop of All OU in my AD

Hey, Good day,

My AD has 50 OU at least.

I need a command to know how many users and desktop computers exist in each one.

How can I do it?
Jose ColmenaresAsked:
Who is Participating?

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

x
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.

MaheshArchitectCommented:
try below

Get-ADUser -Filter * -Properties distinguishedname,cn | select @{n='ParentContainer';e={$_.distinguishedname -replace "CN=$($_.cn),",''}}

Open in new window


taken from
https://social.technet.microsoft.com/Forums/windowsserver/en-US/830ff383-9057-45d8-ae10-5e567efd36f8/how-to-get-parent-container-path-of-the-ad-user-object?forum=winserverpowershell
1
footechCommented:
I'm assuming you want separate counts for users and computers.  Here's one way.
Get-ADOrganizationalUnit -Filter * | Select -ExpandProperty distinguishedName | ForEach `
{
    $ou = $_
    Get-ADObject -Filter * -SearchBase $ou -SearchScope OneLevel |
     Group -Property objectClass -NoElement | Where { "computer","user" -contains $_.Name } |
     Select Count,@{n="Type";e={$_.Name}},@{n="OU";e={$ou}}
}

Open in new window

2

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
Jose ColmenaresAuthor Commented:
Hello LVL42, good morning, the command worked exactly as I needed, I wanted to know, is there a way that instead of showing me the OU with the DistingishedName I showed it with the CanonicalName? How can I export the information to a CSV file?  try placing it at the end of the command but it did not work.

| Export-CSV D:\File.csv

Open in new window


Thanks.
1
Jose ColmenaresAuthor Commented:
the command worked exactly as I needed, I wanted to know, is there a way that instead of showing me the OU with the DistinguishedName I showed it with the CanonicalName? How can I export the information to a CSV file?
0
footechCommented:
Adding the pipe to Export-Csv at the end is exactly how you get the results to a .CSV here.  I also modified the code to show the CanonicalName.
Get-ADOrganizationalUnit -Filter * -Properties CanonicalName | ForEach `
{
    $ou = $_
    Get-ADObject -Filter * -SearchBase $ou.distinguishedName -SearchScope OneLevel |
     Group -Property objectClass -NoElement | Where { "computer","user" -contains $_.Name } |
     Select Count,@{n="Type";e={$_.Name}},@{n="OU";e={$ou.CanonicalName}}
} | Export-Csv file.csv -NoTypeInformation

Open in new window


BTW - my username is footech.  LVL (Level) 42 is my rank on EE.
2
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
Powershell

From novice to tech pro — start learning today.