PowerShell 3.0 -NotIn and -In

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

In this example we are asking PowerShell the question: ‘Is 77 in the range 70 to 80?’

# PowerShell -In
77 -In 70..80
True

Note 1: The key to using -In (and -NotIn) is mastering the ranging dots .. between the values

Example 2: PowerShell’s -NotIn

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
True

The Secret to Understanding PowerShell 3’s -In and -NotIn

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
False

Note 2: This makes no sense, thus abandon -Contains and go back to -In.  See Example 2 above

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

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.

Download a free trial of Solarwinds’ Network Performance Monitor

Comparing Windows PowerShell’s -In with -Match

# PowerShell -Match
‘8080’ -Match ’80’
True

But ’80’ -Match ‘8080’ would be False

# PowerShell -In
‘8080’ -In ’80’
False

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’
True

Troubleshooting PowerShell -In and -NotIn

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

Dottiness
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!

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

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!

Download your fully functional trial copy of the Engineer’s Toolset v10

Further Research on Conditional Operators

-In and -NotIn are members of PowerShell's conditional operators.  A good way to research these operators is with:

Help about_Comparison_Operators

-eq
-ne
-gt
-ge
-lt
-le
-Like
-NotLike
-Match
-NotMatch
-Contains
-NotContains
-In
-NotIn
-Replace

See more of what’s new in PowerShell v 3.0 »

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

PowerShell 3.0  • What’s New in PowerShell 3.0  • PowerShell 3.0 Foreach-Object

PowerShell Show-Command  • Out-GridView -PassThru  • PowerShell Ordered Hash Tables

PowerShell Home  • PowerShell 3.0 Get-ChildItem  • PowerShell 3 -NotIn  • PowerShell 3.0 Where