New PowerShell 3.0 Comparators -NotIn and -In
PowerShell 1.0 not only has comparison operators such as -Match, -Like and -Contains, but also their negative counterparts such as -NotContains.
Here in PowerShell 3 we have two new members of the comparsion operators team: -In and -NotIn.
Windows PowerShell -In Topics
- Example 1: Testing Numbers in a Range
- Example 2: PowerShell’s -NotIn
- The Secret to Understanding PowerShell 3’s -In and -NotIn
- Comparing PowerShell’s -In with -Match
- Troubleshooting PowerShell -In and -NotIn
In this example we are asking PowerShell the question: ‘Is 77 in the range 70 to 80?’
# PowerShell -In
77 -In 70..80
Note 1: The key to using -In (and -NotIn) is mastering the ranging dots .. between the values
Once you have mastered the -In comparator, you’ll have no trouble with its sister command -NotIn, it works in a complimentary manner in according to what logic would predict.
# PowerShell -NotIn
5 -NotIn 70..80
The easiest way to learn which comparator to choose for which script is to compare them! That is how I appreciated the nuances of -Like and -Match. In the case of -In, substitute -Contains and observe the similarities and differences. For me, it all comes down to those two ranging dots, which -In loves, but -Contains seems to ignore.
# Windows PowerShell -Contains
77 -Contains 70..80
Note 2: This makes no sense, thus abandon -Contains and go back to -In. See Example 2 above
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
# PowerShell -Match
‘8080’ -Match ’80’
But ’80’ -Match ‘8080’ would be False
# PowerShell -In
‘8080’ -In ’80’
Also ’80’ -In ‘8080’ would be False
Conclusion: PowerShell’s -In works best with a range expressed as: 0..8080. In my opinion -In (or -NotIn) are rarely the best comparison operator for text.
# PowerShell -In
’80’ -In ‘1..8080’
Incorrect version of PowerShell
You must provide a value expression on the right-hand side of the ‘-‘ operator.
At line:2 char:6
Unexpected token ‘in’ in expression or statement.
This is what you get in PowerShell 1.0 or 2.0, -In is new in PowerShell 3.0
You get the ‘wrong’ result, 'False' when all logic tells you it should be true. Check you have two ranging dots .. and not three … dots. Strange but true!
This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems. Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.
There are so many good gadgets; it’s like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools. Try the SolarWinds Engineer’s Toolset now!
Further Research on Conditional Operators
-In and -NotIn are members of PowerShell's conditional operators. A good way to research these operators is with:
Summary of PowerShell -In and -NotIn
PowerShell 1.0 uses comparison operators such as, -Match, -Like and -Contains, PowerShell 3.0 brings -In and -NotIn. The key to understanding these latest operators is to pay close attention to the ranging .. dots.
If you like this page then please share it with your friends
See more Microsoft PowerShell v 3.0