# Get-ADObject -Filter not working?

**URL:** <https://forums.powershell.org/t/get-adobject-filter-not-working/21225>\
**Category:** PowerShell Help\
**Created:** [December 24, 2022, 2:03pm UTC](https://forums.powershell.org/t/get-adobject-filter-not-working/21225 "2022-12-24T14:03:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pj483](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@pj483](https://forums.powershell.org/u/pj483)\
**Post date:** [December 24, 2022, 2:03pm UTC](https://forums.powershell.org/t/get-adobject-filter-not-working/21225/1 "2022-12-24T14:03:00Z")

</div>

Hi all. I am running the following code:

```auto
[pscustomobject]$resultSet = Get-ADObject -Filter '(objectClass -eq "user" -or objectClass -eq "group" -or objectClass -eq "organizationalUnit")' -Server ...

```

but in the $resultSet array, I’m getting values where “objectClass” equals “computer”. Any ideas what’s going on?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 24, 2022, 2:14pm UTC](https://forums.powershell.org/t/get-adobject-filter-not-working/21225/2 "2022-12-24T14:14:49Z")

</div>

If you don’t like to get computers you should exclude them. 😉

```auto
Get-ADObject -Filter '(objectClass -eq "user" -or 
    objectClass -eq "group" -or 
    objectClass -eq "organizationalUnit") -and 
    objectClass -ne "computer"'

```

---

<div class="post-metadata">

**Author:** ![pj483](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@pj483](https://forums.powershell.org/u/pj483)\
**Post date:** [December 24, 2022, 3:05pm UTC](https://forums.powershell.org/t/get-adobject-filter-not-working/21225/3 "2022-12-24T15:05:10Z")

</div>

Err… Surely there’s no point to “-eq” if I need to also use “-ne” 🤨

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 24, 2022, 3:06pm UTC](https://forums.powershell.org/t/get-adobject-filter-not-working/21225/4 "2022-12-24T15:06:46Z")

</div>

Did you try it? Did it work?

---

<div class="post-metadata">

**Author:** ![pj483](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@pj483](https://forums.powershell.org/u/pj483)\
**Post date:** [December 24, 2022, 3:40pm UTC](https://forums.powershell.org/t/get-adobject-filter-not-working/21225/5 "2022-12-24T15:40:45Z")

</div>

No. As I’m parsing the resulting array based on the “objectClass” value, I just ignore the entries I don’t want. My question was more about why they’re being returned **at all**.

---

<div class="post-metadata">

**Author:** ![mawazo](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mawazo](https://forums.powershell.org/u/mawazo)\
**Post date:** [December 24, 2022, 6:00pm UTC](https://forums.powershell.org/t/get-adobject-filter-not-working/21225/6 "2022-12-24T18:00:40Z")

</div>

searched online and found this. hope it helps

> <https://stackoverflow.com/questions/17977923/why-does-get-adobject-not-filter-objectclass-user-correctly>

> **[Deciding What to Find - Win32 apps](https://learn.microsoft.com/en-gb/windows/win32/ad/deciding-what-to-find?redirectedfrom=MSDN)**
>
> Before you search a directory, consider how your search will perform based on your approach. The data and properties to be returned affect where you bind to start a search, the depth of your search, your query filter, and search performance.

after reading, tested below seems to work

```auto
 Get-ADObject -Filter "(objectClass -eq 'user' -and objectCategory -eq 'person') -or (objectClass -eq 'group') -or (objectClass -eq 'organizationalUnit')"

```

---

<div class="post-metadata">

**Author:** ![Matt](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Matt](https://forums.powershell.org/u/Matt)\
**Post date:** [December 27, 2022, 1:40am UTC](https://forums.powershell.org/t/get-adobject-filter-not-working/21225/7 "2022-12-27T01:40:07Z")

</div>

> [@pj483](#):
>
> Err… Surely there’s no point to “-eq” if I need to also use “-ne” 🤨

Lesson learned is the code is always right. Meaning it did exactly what you said to do, just not what you wanted it to do. When this happens, asking for help is not necessarily wrong, but diving back into the documentation will clarify what is going on.

---

<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:16pm UTC](https://forums.powershell.org/t/get-adobject-filter-not-working/21225/8 "2024-05-16T20:16:11Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


