# Getting away from filter \*

**URL:** <https://forums.powershell.org/t/getting-away-from-filter/6218>\
**Category:** PowerShell Help\
**Created:** [April 13, 2016, 10:10pm UTC](https://forums.powershell.org/t/getting-away-from-filter/6218 "2016-04-13T22:10:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wei-yen-tan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wei-yen-tan/32/91_2.png) [@wei-yen-tan](https://forums.powershell.org/u/wei-yen-tan)\
**Post date:** [April 13, 2016, 10:10pm UTC](https://forums.powershell.org/t/getting-away-from-filter/6218/1 "2016-04-13T22:10:36Z")

</div>

I know the need to filter but this has always stumped me with a query like this where the properties does not exist in the default view. ie:

```
Get-Adcomputer -filter {$_.operatingsystem -like "*2012 R2*"} -properties operatingsystem
```

This gives an error and I can understand why because properties is outside the filter scriptblock.

If I try :

```
Get-Adcomputer -filter {$_.operatingsystem -like "*2012 R2*" -properties operatingsystem }
```

So now i can only resort to putting that in where-object.

Can I do this better and then comply with best practices?

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 13, 2016, 10:18pm UTC](https://forums.powershell.org/t/getting-away-from-filter/6218/2 "2016-04-13T22:18:17Z")

</div>

These AD filters are always a challenge to get right. You are using the filter in the same way as with Where-Object. This doesn’t work, because the AD Cmdlets take in a other filter format. Try this:

```
Get-ADComputer -Filter "OperatingSystem -like '*2012 R2*'" -Properties OperatingSystem
```

---

<div class="post-metadata">

**Author:** ![wei-yen-tan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wei-yen-tan/32/91_2.png) [@wei-yen-tan](https://forums.powershell.org/u/wei-yen-tan)\
**Post date:** [April 13, 2016, 10:46pm UTC](https://forums.powershell.org/t/getting-away-from-filter/6218/3 "2016-04-13T22:46:40Z")

</div>

Thanks Richard!

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [April 13, 2016, 11:09pm UTC](https://forums.powershell.org/t/getting-away-from-filter/6218/4 "2016-04-13T23:09:17Z")

</div>

This works for me

Get-ADComputer -Filter {OperatingSystem -eq ‘Windows Server 2012 R2 Standard’ } -Properties OperatingSystem | select Name, OperatingSystem

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 13, 2016, 11:27pm UTC](https://forums.powershell.org/t/getting-away-from-filter/6218/5 "2016-04-13T23:27:25Z")

</div>

@Richard: What is the reason you use a scriptblock instead of a string for the filter parameter?

---

<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:41pm UTC](https://forums.powershell.org/t/getting-away-from-filter/6218/6 "2024-05-16T20:41:35Z")

</div>

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


