Add condition in where-object

Hello, the following code works for me :

$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"


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

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

i tried also something like that :

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

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

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. :smirk:

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:

$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?


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.

A way I would do it is:

$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”