# Add condition in where-object

**URL:** <https://forums.powershell.org/t/add-condition-in-where-object/20312>\
**Category:** PowerShell Help\
**Created:** [August 30, 2022, 4:42pm UTC](https://forums.powershell.org/t/add-condition-in-where-object/20312 "2022-08-30T16:42:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![deuval](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@deuval](https://forums.powershell.org/u/deuval)\
**Post date:** [August 30, 2022, 4:42pm UTC](https://forums.powershell.org/t/add-condition-in-where-object/20312/1 "2022-08-30T16:42:56Z")

</div>

Hello, the following code works for me :

```auto
$screenCSV = Import-Csv "h:\glpi_screen.csv" -Delimiter ";" -Header "Nam","Model","Inventorynumber","SerialNumber"

	#get serial screen number and model
	$numSerie=gwmi -computerName $env:computername WmiMonitorID -Namespace root\wmi | select-Object SerialNumberID,UserFriendlyName |
	ForEach-Object {
    	$tableScreen+=@([pscustomobject]@{serialNumber = ($_.SerialNumberID -ne 0 | foreach {[char]$_}) -join '' ;
                                   	Model = ($_.UserFriendlyName -ne 0 | foreach {[char]$_}) -join ''})
	}
    
	get the CSV line in terms of serial number and model
	$tableScreen | ForEach-Object {
  	 
        	$requestCSVScreen=$screenCSV | Where-Object -Property "SerialNumber" -match "$($_.serialNumber)" 
     	 
        	write-host "======================= $requestCSVScreen.SerialNumber"
        	$requestCSVScreen[0].Model

```

BUT

i need add a condition in my where-object. I need something like that :

```auto
$requestCSVScreen=$screenCSV | Where-Object -Property "SerialNumber" -match "$($_.serialNumber)" -and -Property "Model" -eq "$($_.Model)"

```

i tried also something like that :

```auto
$requestCSVScreen=$screenCSV | Where-Object { ("SerialNumber" -match "$($_.serialNumber)") -and ("Model" -eq "$($_.Model)")}

```

But does not works. How can i add a condition in where-object ?

---

<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:** [August 30, 2022, 7:18pm UTC](https://forums.powershell.org/t/add-condition-in-where-object/20312/2 "2022-08-30T19:18:48Z")

</div>

Your code is very badly formatted, incomplete and mixed with plain text. You should make it as easy as possible for people willing to help you to actually help you. 😏

> [@deuval](#):
>
> i need add a condition in my where-object. I need something like that :

If you have more than one condition you want to use you can either use `Where-Object` two times or simply use the _traditional_ syntax and combine conditions like this:

```auto
$requestCSVScreen = 
$screenCSV | 
    Where-Object {$_.SerialNumber -match "$($_.serialNumber)" -and $_.Model -eq "$($_.Model)"}

```

It just does not make any sense with the properties you specified. If you want to compare the elements of one CSV object with another you should use `Compare-Object` instead.

Could you please explain a little more detailed what you’re actually trying to do?

---

<div class="post-metadata">

**Author:** ![gilrac](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gilrac/32/3928_2.png) [@gilrac](https://forums.powershell.org/u/gilrac)\
**Post date:** [September 9, 2022, 6:01pm UTC](https://forums.powershell.org/t/add-condition-in-where-object/20312/3 "2022-09-09T18:01:42Z")

</div>

deuval,

It is easier to think of it as lines in your .CVS file. The where-object will allow you to filter by a specific value(s) to identify the line that contains the data you need. From there you need to specify what that data is.

> [@deuval](#):
>
> `$requestCSVScreen=$screenCSV | Where-Object -Property "SerialNumber" -match "$($_.serialNumber)" -and -Property "Model" -eq "$($_.Model)"`

A way I would do it is:

```auto
$requestCSVScreen=($screenCSV | Where-Object {$_.Model -eq "<first requirement>" -and $_.SerialNumber -eq "<second requirement>"}).<value you need>

```

Please keep in mind that -and is both are required and -or only one is required. the last item in the string is the item that contains the value you need like “Nam” or “Inventorynumber”

---

<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/add-condition-in-where-object/20312/4 "2024-05-16T20:16:39Z")

</div>

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


