PowerShell for Programmers: What happened to my operators?

Kory Thacher

Operators are one of the most frustrating things about learning PowerShell if you’re coming from just about any other language in existence. Operators like ==, <=, !=. etc. are almost ubiquitous in programming, but none of them are supported in PowerShell.

This can lead to some pretty frustrating errors with things like If statements. You might see:

The assignment expression is not valid. The input to an assignment operator must
be an object that is able to accept assignments, such as a variable or a property.

Unexpected token ')' in expression or statement.

 

In PowerShell we are still going to have all these operators, they are just going to look different, using dashes and vaguely resembling parameters.

I wondered the reason behind this myself for a while, and made a pretty inaccurate guess, but a coworker of mine recently shared the answer from Jeffrey Snover himself, who was gracious enough to let me post it here:

What was the rationale behind using –eq, -le, -gt, etc. vs. the traditional ==, <=, > found in most languages?

Bruce Payette’s book PowerShell in Action goes into this in detail. The basic idea is that as soon as you choose “>” for redirection, you are forced down the –lt path. That is why I originally tried to use “:>” for redirection so that we could use >=, etc but the community went absolutely ape-@$# over this and thus we are where we are.

Lets take a look at what we have to work with:

Comparison and logical operators:

  • No case sensitivity (“H” would be equal with “h”) on comparison operators
  • No wild card support on comparison operators

 

 

Usual

PowerShell

Test performed

==

-eq

Equality

!=

-ne

Non Equality

>

-gt

Greater Than

>=

-ge

Greater Than OR Equal To

<

-lt

 

 

Less Than

<=

-le

Less Than OR Equal To

!

! OR -not

Not

||

-or

Logical Or

&&

-and

Logical And

^

-xor

Logical Exclusive Or

 

 

 

#So, this won't work:
If(1 == 1)
{
write-host "Hello World" -ForegroundColor Cyan
}

#Instead you need:
If(1 -eq 1)
{
write-host "Hello World" -ForegroundColor Cyan
}

 

Its not so hard, but it will be quite an adjustment, so get used to seeing errors here.

“But I want wild cards!”

No worries, PowerShell has you covered here too. You can use wild cards with an alternate set of comparison operators.

Instead of –eq/-neq we will use –like/-notlike to enable windows wild card support:

 

#no wild cards: literal *
If("hello" -eq "h*")
{
write-host "equal!" -ForegroundColor green
}
else
{
write-host "not equal!" -ForegroundColor red
}

#wild cards enabled
If("hello" -like "h*")
{
write-host "equal!" -ForegroundColor green
}
else
{
write-host "not equal!" -ForegroundColor red
}

 

If you want RegEx we have –Match/-NotMatch:

#Check pattern
If("I've got 1 digit inside!" -match "\d")
{
write-host "match!" -ForegroundColor green
#cool way to see *what* matched (will only pull the first match)
$matches[0]
} 

Additionally, you might find yourself seeing people use operators you are not used to having.

  • -is, -isnot to check data types
  • -in, -contains, -notin, -notcontains to check for values in lists

“But I need case sensitivity!”

To make any of the comparison operators case sensitive, you can add a “c” prefix. –eq becomes –ceq. –Like becomes –clike and so on.

#wild cards + case sensitivity
If("hello" -clike "H*")
{
write-host "equal!" -ForegroundColor green
}
else
{
write-host "not equal!" -ForegroundColor red
}

 

 

Well that’s all for now, hopefully this helps you on your PowerShell journey and fixes some issues you may have run into from diving in. I’ll be adding to the series to cover other quirks as well so let me know in the comments if there is any particular topics confusing you!

If you find this helpful don’t forget to rate, comment and share 🙂

For the main series post, check back here.

0 comments

Discussion is closed.

Feedback usabilla icon