Forum Discussion

Dabby's avatar
Dabby
Copper Contributor
Apr 29, 2022
Solved

get-unique command get me wrong results in powershell

Hello,

 

i need some help get the unique values inside a variable or array.

The goal was to count member of ad groups. we could no be sure, if users are in multiple groups so i wanted to  put the users in a variable and get the unique entrys and count them.

but i get a wrong value 😞

 

 

 

$Users = @()
$OUs = Get-ADOrganizationalUnit -Filter 'Name -like "<OUNAME>"'
foreach ($OU in $OUs)
{

$Users+= @( Get-ADGroup -Filter * -searchbase $ou |Get-ADGroupMember -Recursive |select name)

}

$Users+= @(Get-ADGroupMember <GROUPNAME> )

$Users.count
$Users = @($users |Sort-Object name )
$Users.count
$Users = $Users |Get-Unique
$Users.count

 

 


Result:
9188
9188
1335

 

1335 is to low!

 

or:

 

 

$users.count
9188
$A = $Users | select -Unique
$A.count
737

 

 

 

why i get a different result?

 

i read somewhere while googel about the error, that the values have to be sorted before get-unique, thata why i add the  $Users = @($users |Sort-Object name ) line.

The result 1335 is way to low.  If i export thet value $users to a file and let excel do the work it removes 653 double Values and is got 8538 as result what looks much more plausible.

 

i dont understand what went wrong here. 

 

thank you.

 

Resources