Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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:  SolarWinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

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.

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

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: