• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 53
  • Last Modified:

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?
1
Jose Colmenares
Asked:
Jose Colmenares
  • 2
  • 2
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now