# Querying multiple AD domains to find a list of users using filter -eq

**URL:** <https://forums.powershell.org/t/querying-multiple-ad-domains-to-find-a-list-of-users-using-filter-eq/14032>\
**Category:** PowerShell Help\
**Created:** [March 25, 2020, 12:30pm UTC](https://forums.powershell.org/t/querying-multiple-ad-domains-to-find-a-list-of-users-using-filter-eq/14032 "2020-03-25T12:30:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![powerdude](https://avatars.discourse-cdn.com/v4/letter/p/46a35a/32.png) [@powerdude](https://forums.powershell.org/u/powerdude)\
**Post date:** [March 25, 2020, 12:30pm UTC](https://forums.powershell.org/t/querying-multiple-ad-domains-to-find-a-list-of-users-using-filter-eq/14032/1 "2020-03-25T12:30:43Z")

</div>

&nbsp;

Hi All,

I am currently using this script to export a list of AD users from a CSV file input. The process is running correctly, but it generates several errors. This is because I believe it queries all the domains returning an error when it doesn’t find the object in the domain.

```
$csvdata = Import-Csv -Path $csvfile

$result = foreach ($domain in $domains)

{

```
$csvdata | ForEach-Object { Get-ADUser -Server $domain -Identity $_.muid } | select-object -property DistinguishedName

```

}

$result | Out-file -Append -FilePath $txtfile1
```

I get returned the following error even though it then finds the user
```
Get-ADUser : Cannot find an object with identity: 'XXXXXXXX' under: 'DC=corp,DC=domain,DC=com'.
At line:3 char:33
+ ... ForEach-Object { Get-ADUser -Server $domain -Identity $_.muid } | sel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (XXXXXXXX:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
```
I understand if I use the cmdlet filter something like -eq it would then return no errors, but I am unable to make it work so far.

Is there any way you can help me with the script above to filter only the users found and return no errors?

&nbsp;

&nbsp;

Thanks,

PowerDude

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [March 25, 2020, 12:58pm UTC](https://forums.powershell.org/t/querying-multiple-ad-domains-to-find-a-list-of-users-using-filter-eq/14032/2 "2020-03-25T12:58:52Z")

</div>

```
$csvdata = Import-Csv -Path $csvfile

$result = foreach ($domain in $domains)

{

    $csvdata | ForEach-Object { try { Get-ADUser -Server $domain -Identity $_.muid -EA 1 } catch { } } | select-object -property DistinguishedName

}

$result | Out-file -Append -FilePath $txtfile1
```

---

<div class="post-metadata">

**Author:** ![powerdude](https://avatars.discourse-cdn.com/v4/letter/p/46a35a/32.png) [@powerdude](https://forums.powershell.org/u/powerdude)\
**Post date:** [March 25, 2020, 2:55pm UTC](https://forums.powershell.org/t/querying-multiple-ad-domains-to-find-a-list-of-users-using-filter-eq/14032/3 "2020-03-25T14:55:02Z")

</div>

> [@](#):
>
> PowerShell
> 
> 12 lines
> 
> \<textarea class="ace\_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;" spellcheck="false" wrap="off"\>\</textarea\>
> 
> 1
> 
> 2
> 
> 3
> 
> 4
> 
> 5
> 
> 6
> 
> 7
> 
> 8
> 
> 9
> 
> 10
> 
> 11
> 
> 12
> 
> $csvdata·=·Import-Csv·-Path·$csvfile
> 
> $result·=·foreach·($domain·in·$domains)
> 
> {
> 
> ····$csvdata·|·ForEach-Object·{ try { Get-ADUser·-Server·$domain·-Identity·$\_.muid·-EA 1 } catch { } }·|·select-object·-property·DistinguishedName
> 
> }
> 
> $result·|·Out-file·-Append·-FilePath·$txtfile1
> 
> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Hi Sam,

&nbsp;

Many thanks, that worked like a charm 😃

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/querying-multiple-ad-domains-to-find-a-list-of-users-using-filter-eq/14032/4 "2024-05-16T20:30:01Z")

</div>


