Where method and boolean "-and"

As a Powershell beginner I misunderstand some behavior of the boolena logic operatorr “-and”.
I’m developing a simple script that does something depending on current active status of my laptop and network setting: Lapop: so wired (ethernet or not (wi-fi), on power or on battery and is it at my home (where my NAS is as well) or is it in the office of my (volunteer-)work (other ip-address).
So I tried a lot and I have a simple straight forward solution to that, but during development I stumbled upon a – in my opinion strange – behavior and I’m looking for an explanation.

Situation:

All interfaces on my laptop:

PS C:\> Get-NetIPAddress -AddressFamily IPv4 | select -Property ipaddress, interfacealias

ipaddress       interfacealias              
---------       --------------              
192.168.1.118   Ethernet 4                  
172.22.32.1     vEthernet (Default Switch)  
192.168.1.116   Ethernet 2                  
169.254.31.247  Local Area Connection* 2    
169.254.84.130  Bluetooth Network Connection
169.254.171.204 Local Area Connection* 1    
192.168.1.117   Wi-Fi                       
127.0.0.1       Loopback Pseudo-Interface 1

This command below + output is OK for me:

PS C:\> Get-NetIPAddress -AddressFamily ipv4 | where {$_.InterfaceAlias -eq "Wi-Fi" -or $_.InterfaceAlias -like "Ethernet*"}   | Select -Property ipaddress, interfacealias

ipaddress       interfacealias
---------       --------------
192.168.1.118   Ethernet 4    
192.168.1.116   Ethernet 2    
169.254.230.159 Wi-Fi

This as well:

PS C:\> Get-NetIPAddress -AddressFamily ipv4 | where {$_.IPv4Address -like "192.168.1.11[6,8]"} | select -Property ipaddress

ipaddress    
---------    
192.168.1.118
192.168.1.116

But combining the 2 critera with a boolean -and results in a for me unexpected output:

PS C:\> Get-NetIPAddress -AddressFamily ipv4 | where {{$_.InterfaceAlias -eq "Wi-Fi" -or $_.InterfaceAlias -like "Ethernet*"} **-and** {$_.IPAddress -like "192.168.1.11[6,8]"}}  | Select -Property ipaddress, interfacealias

ipaddress       interfacealias              
---------       --------------              
172.22.32.1     vEthernet (Default Switch)  
192.168.1.116   Ethernet 2                  
169.254.31.247  Local Area Connection* 2    
169.254.84.130  Bluetooth Network Connection
169.254.171.204 Local Area Connection* 1    
169.254.230.159 Wi-Fi                       
127.0.0.1       Loopback Pseudo-Interface 1

I think “and-ing” 2 criteria should not extend the results, but should deliver less results (not for this combination, but in general).

Please can you explain this to me? What is wrong with my perception of this?
Kind regards,

Hans Troost

retyped 2 missing underscores for the 2 Interfacealias criteria, but same weird result:

Get-NetIPAddress -AddressFamily ipv4 | where {{$_.InterfaceAlias -eq “Wi-Fi” -or $_.InterfaceAlias -like “Ethernet*”} **-and** {$_.IPAddress -like “192.168.1.11[6,8]”}} | Select -Property ipaddress, interfacealias

Hans,
Welcome to the forum. :wave:t4:

Before we proceed … please go back, edit your question again and fix the formatting of your code.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Thanks Olaf for your reply. It is nearly bed-time here now, but tomorrow I will edit my post according to what you menioned. Is that possible - editing afterwards ? I will be happy to do so tomorrow,

Kind regards,

Hans Troost

You used curly braces where you needed parenthesis … If I got it right …

Try it this way:

Get-NetIPAddress -AddressFamily ipv4 | 
Where-Object {
    ($_.InterfaceAlias -eq "Wi-Fi" -or 
    $_.InterfaceAlias -like "Ethernet*") -and 
    $_.IPAddress -like "192.168.1.11[6,8]"
}  | 
Select-Object -Property ipaddress, interfacealias

Thanks Olaf, that is the solution. Works fine now.

I will look where to find a clear explanation about when/when not to use curly braces or parenthesis. Its is clear that I started to fast with powershell as an old IT-man: “if everything else fails, read the manual”. So I will do my homework now…

Thanks and apologies, Hans

You’re very welcome. … and no need to appologize. We all started once and have been there.

That’s what we here for. :wink: