# Filtering two services at the same time.

**URL:** <https://forums.powershell.org/t/filtering-two-services-at-the-same-time/4859>\
**Category:** PowerShell Help\
**Created:** [September 10, 2015, 11:40am UTC](https://forums.powershell.org/t/filtering-two-services-at-the-same-time/4859 "2015-09-10T11:40:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![helder-guzman](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@helder-guzman](https://forums.powershell.org/u/helder-guzman)\
**Post date:** [September 10, 2015, 11:40am UTC](https://forums.powershell.org/t/filtering-two-services-at-the-same-time/4859/1 "2015-09-10T11:40:43Z")

</div>

HI

Quick question

I need to filter two services using wmi. can you do it in one line like

Get-WmiObject -Computer $servername Win32\_Service | Where-Object name (“tlmagent”, “OpswareAgent”)

and then convert to hml the output.

would you create a new psobject to write-output ?

---

<div class="post-metadata">

**Author:** ![tombakerfl](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@tombakerfl](https://forums.powershell.org/u/tombakerfl)\
**Post date:** [September 10, 2015, 1:07pm UTC](https://forums.powershell.org/t/filtering-two-services-at-the-same-time/4859/2 "2015-09-10T13:07:04Z")

</div>

If you can pipeline it, just use Select to grab the properties you want.  
Use a hashtable with Label=“Title” and Expression={} to create custom columns

When combining data from multiple cmdlets, then I like to use a [pscustomobject] and populate the appropriate properties from each cmdlet.

```
Get-WmiObject -Computer $servername Win32_Service | Where-Object {$_.name -in "tlmagent", "OpswareAgent"} | select PSComputerName,Name,DisplayName,StartMode,Status,@{Label='LogonAs';Expression={$_.StartName}} | ConvertTo-Html -Fragment
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 10, 2015, 11:00pm UTC](https://forums.powershell.org/t/filtering-two-services-at-the-same-time/4859/3 "2015-09-10T23:00:23Z")

</div>

You should always filter as far left as possible in the pipeline. Your command is getting ALL services and then filtering. Consider:

```
$services = Get-WmiObject -Computer $servername Win32_Service -Filter "Name = 'tlmagent' or Name = 'OpswareAgent' "

$services | ConvertTo-HTML
```

Another

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [September 10, 2015, 11:37pm UTC](https://forums.powershell.org/t/filtering-two-services-at-the-same-time/4859/4 "2015-09-10T23:37:07Z")

</div>

Rob’s absolutely right about filtering early in the pipeline. In this case it means structuring your WQL query to get just what you want and nothing more. You will also want to use Select-Object so you are only displaying the properties you are interested in.

```
$servername = "myserver"
$services = Get-WmiObject -Computer $servername Win32_Service -Filter "Name = 'usosvc' or Name = 'spooler' "
$services | select Name, Status | ConvertTo-HTML | out-file -FilePath .\foo.htm
invoke-item -path .\foo.htm
```

---

<div class="post-metadata">

**Author:** ![helder-guzman](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@helder-guzman](https://forums.powershell.org/u/helder-guzman)\
**Post date:** [September 11, 2015, 10:34am UTC](https://forums.powershell.org/t/filtering-two-services-at-the-same-time/4859/5 "2015-09-11T10:34:29Z")

</div>

Thanks, that helped me a lot

---

<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:44pm UTC](https://forums.powershell.org/t/filtering-two-services-at-the-same-time/4859/6 "2024-05-16T20:44:47Z")

</div>


