Get-ADOrganizationalUnit count members

Welcome Forums General PowerShell Q&A Get-ADOrganizationalUnit count members

Viewing 7 reply threads
  • Author
    Posts
    • #213804
      Participant
      Topics: 2
      Replies: 3
      Points: 32
      Rank: Member

      Hi,

      I’m a begginer in powershell and i encounter an issue with my little script

      This is my code :

      $OUPath= Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | ? {$_.distinguishedname -notmatch 'OU=_Désactivés'}
      $OuName = Get-ADOrganizationalUnit -filter * -SearchBase $OUPath | Select-Object Name
      $UserNumber= "1"
      write-host $OuName";"$UserNumber

      The result is :

      @{Name=Test};1

      But What i want is :

      Test;1

      In fact my real script is :

      Remove-Item -Path C:\test\UserOU.csv
      $UserNumber=''
      $exportPath= 'C:\test\UserOU.csv'
      $OUPath= Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | ? {$_.distinguishedname -notmatch 'OU=_Désactivés'}
      Foreach ($OUPath in $OUPath) {
      $UserNumber = @(Get-AdUser -filter * -SearchBase $OUPath |Where {$_.enabled -eq "True"}).count
      Write-Host $OUPath";"$UserNumber
      Write-Output $OUPath";"$UserNumber | out-file -FilePath $exportPath -append
      }

      But the result shows me the OU’s distinguishedname but the name it’s all I need.

       

      PS : Sorry for my english, i’m a french user.

       

      Regards,

    • #213828
      Participant
      Topics: 4
      Replies: 82
      Points: 251
      Helping Hand
      Rank: Contributor

      Looks to me like “Name” is a property of the $OUPath object.  Try this instead:

      Write-Host $OUPath.Name, $UserNumer -Separator ";"
      • This reply was modified 1 month, 3 weeks ago by Mike R..
    • #213849
      Participant
      Topics: 2
      Replies: 3
      Points: 32
      Rank: Member

      Hi Mike,

      Thanks,

      I’ve already tried that, it works fine for the write-host but when i use the write-output and the export (the last line of my last code) the content of my csv file looks like :

      PLD
      2
      #NAME?
      ;

      instead of something like the write-host which is :

      PLD;2

      Regards,

      • This reply was modified 1 month, 3 weeks ago by Poison06.
      • This reply was modified 1 month, 3 weeks ago by Poison06.
      • This reply was modified 1 month, 3 weeks ago by Poison06.
    • #213864
      Participant
      Topics: 4
      Replies: 82
      Points: 251
      Helping Hand
      Rank: Contributor

      Are you really just making a csv with “;” delimiters?  If so, you can just do this:

      $exportPath= 'C:\test\UserOU.csv'
      $OUPath= Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | ? {$_.distinguishedname -notmatch 'OU=_Désactivés'}
      $OUPath | 
          Select-Object -Property name, 
                                  @{n=usernumber;e={@(Get-AdUser -filter * -SearchBase $_ |Where {$_.enabled -eq "True"}).count}} |
              Export-Csv -Path $exportPath -Delimiter ";"
      • This reply was modified 1 month, 3 weeks ago by Mike R..
    • #214032
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Assuming your query for ADOrganizationalUnits provides a list of OUs you could use something like this:

      $exportPath = 'C:\test\UserOU.csv'
      Remove-Item -Path $exportPath -Force
      $OUList = Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | Where-Object { $_.distinguishedname -notmatch 'OU=_Désactivés' }
      $Result = Foreach ($OU in $OUList) {
          $UserList = Get-AdUser -filter "enabled -eq '$true'" -SearchBase $OU 
          [PSCustomObject]@{
              OrganizationalUnit = $OU
              UserCount          = $UserList.count
          }
      }
      $Result | Export-Csv -Path $exportPath -NoTypeInformation
    • #214155
      Participant
      Topics: 2
      Replies: 3
      Points: 32
      Rank: Member

      Thanks to both of you.

      @Olaf I’ve used your code.
      I’ve just added 2 things :

      .name in this part

      OrganizationalUnit = $OU.name

      because if not, the result was the full distinguishedname of the OU instead of the name only.

      And | measure-object in this part

      $UserList = Get-AdUser -filter "enabled -eq '$true'" -SearchBase $OU | measure-object

      because if not, the result wasn’t correct for OU with 0 or 1 user.

      Final code is :

      $exportPath = 'C:\test\UserOU.csv'
      Remove-Item -Path $exportPath -Force
      $OUList = Get-ADOrganizationalUnit -ldapfilter '(name=*)' -SearchBase "OU=Test,OU=Utilisateurs,OU=Titi,DC=TOTO,DC=local" | Where-Object { $_.distinguishedname -notmatch 'OU=_Désactivés' }
      $Result = Foreach ($OU in $OUList) {
      $UserList = Get-AdUser -filter "enabled -eq '$true'" -SearchBase $OU | measure-object
      [PSCustomObject]@{
      OrganizationalUnit = $OU.Name
      UserCount = $UserList.count
      }
      }
      $Result | Export-Csv -Path $exportPath -NoTypeInformation
    • #214188
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      because if not, the result wasn’t correct for OU with 0 or 1 user. …

      OK. I wouldn’t expect to find OUs with 0 or 1 user only. 😉 Another approach would be to cast an array no matter how many objects you get in return … like this:

      $UserList = @(Get-AdUser -filter "enabled -eq '$true'" -SearchBase $OU)
    • #214272
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      The search scope should be specified or you will get counts of any child containers, this worked for me:

      Import-Module ActiveDirectory
      
      $results= Get-ADOrganizationalUnit -Filter {Name -ne '_Disabled'} -SearchBase "OU=Demo,DC=DEMO,DC=LOCAL" |
                Select Name,
                       DistinguishedName,
                       @{Name='UserCount';Expression={[int]@(Get-AdUser -Filter {Enabled -eq $true} -SearchBase $_.DistinguishedName -SearchScope OneLevel).count}}
      
      $results
      
Viewing 7 reply threads
  • You must be logged in to reply to this topic.