idarryl
idarryl

Reputation: 769

Count the number of rows in Powershell Array

The I wrote the below script to return the name of all the Organisational Units in the domain that are empty.

I would like the script to also tell me the total number of empty OU's at the end, by counting the lines returned. I've tried assigning an $array to the New-Object, and adding various versions of $array.count or | Measure-Object at the end of the script, but all return 0.

Get-ADOrganizationalUnit -Filter * |
foreach {
    $o = Get-ADObject -filter * -searchbase $_.distinguishedname -searchscope 1
    $total = ($o | Measure-Object).Count 
    New-Object psobject -Property @{
        Name=$_.distinguishedname
        } |
        where-object {$total -le "0"}
    }

Upvotes: 2

Views: 14401

Answers (1)

Shay Levy
Shay Levy

Reputation: 126772

$ou = Get-ADOrganizationalUnit -Filter * | 
Where-Object { -not (Get-ADObject -SearchBase $_.DistinguishedName -Filter * -SearchScope OneLevel) }


# get the count
$ou|measure

$list OUs
$ou 

Upvotes: 4

Related Questions