0

I'm trying to figure out the correct syntax and whether to use Select-Object or Where-Object to accomplish what I want.

Basically, if I run a basic Get- cmdlet which returns an object's list of parameters and their values in table format I want to hide the parameters with null values.

For example, I want this output: (The output is example)

AmirOU            Count
----------------- -----
tegdg               0
wfw                 0
Amir                7
wtw                 58
wretret             0
ergre               2
iubuibfr            0
User                593
wetwe               0
wdqdd               50
wetwe               0
Groups              0
wtw                 0
ddewe               17
wetwe               0
wetwe               72
wf                  11
fdgd                1
fdbd                53
rete                0
sf4t                0
4tg                 0
sgsdt               0
dsfet               0
ergre               0

To look like this:

AmirOU            Count
----------------- -----

Amir                7
wtw                 58
ergre               2
User                593
5t5                 50
grtg                17
wetwe               72
wf                  11
fdgd                1
fdbd                53

This is my code:

$total = $null
Get-ADOrganizationalUnit -filter * -SearchBase 'OU=Amir,OU=test,DC=sina,DC=local' | 

foreach {

    if($_.distinguishedname -notmatch 'DisabledUsers|Exchange'){



        $users=Get-ADUser -filter * -searchbase $_.distinguishedname -searchscope Onelevel | where-object enabled -eq true 

        $test=($users | measure-object).count

        $total += $test



        New-Object psobject  -Property @{

            Count= $test

            AmirOU = $_.Name;

        }

    }

}

Write-Host "`nTotalt: $total"
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Amir
  • 85
  • 2
  • 8

1 Answers1

2

You could simply add a test in there to see if the .count is not 0 like this:

$total = 0

Get-ADOrganizationalUnit -filter * -SearchBase 'OU=Amir,OU=test,DC=sina,DC=local' | 
    ForEach-Object {
         if ($_.distinguishedname -notmatch 'DisabledUsers|Exchange') {
            $users = Get-ADUser -filter * -searchbase $_.distinguishedname -searchscope Onelevel | Where-Object { $_.Enabled -eq $true }
            $test = ($users | Measure-Object).count

            if ($test) {
                $total += $test

                New-Object psobject -Property @{
                    Count = $test
                    AmirOU = $_.Name
                }
            }
        }
    }

Write-Host "`nTotal: $total"
Theo
  • 57,719
  • 8
  • 24
  • 41
  • 1
    See this [Q&A](https://stackoverflow.com/questions/26504589/how-does-the-count-property-work-in-powershell) why using the variable `$test` and `Measure-Object` aren't neccessary. --> `if ($users.count){...}` –  Oct 12 '18 at 12:29
  • 1
    @LotPings Sure, just thought to leave as much of the OP's original code intact, although I did change the `$total = $null` into `$total = 0` and altered the `Where-Object` a bit. Could have changed the `Measure-Object` aswell, but... – Theo Oct 12 '18 at 12:40