Documente Academic
Documente Profesional
Documente Cultură
Related
Follow Follow
Page 4 of 8 Essential PowerShell: Know your operator and enclosure precedence | Poshoholic
3/5/2014 http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...
Categories: Essential PowerShell, PowerShell Tags: enclosure precedence, Essential PowerShell,
operator precedence, PoSh, Poshoholic, PowerShell
Comments (8)Trackbacks (3)Leave a commentTrackback
1.
stej
July 9, 2009 at 1:58 am | #1
Reply | Quote
Great, I was always wondering, whats the precedence of operators. It is very well defined
e.g. for C#, but I havent found anything for PowerShell.
You saved my time!
Kirk Munro
July 9, 2009 at 1:41 pm | #2
Reply | Quote
Thanks for the feedback, Im glad you like it!
2.
Aravind
August 19, 2010 at 9:49 pm | #3
Reply | Quote
This is very helpful.
3.
Jeremy Sproat
January 12, 2011 at 11:02 am | #4
Reply | Quote
Theres a subtle gotcha with the bitwise and logical operators.
AND operators should have a higher precedence than OR operators (much like * happens
before +). However, in PowerShell v2.0 (and probably v1.0) -band and -bor are evaluated
left to right.
Compare this PowerShell snippet:
PS> 1 -bor 0 -band 0
0
with Python:
>>> 1 | 0 & 0
1
When in doubt, use parentheses!
Essential PowerShell: Beware of
promiscuous types
Essential PowerShell: Learn how
to find what you are looking for
whence in PowerShell
Follow Follow
Page 5 of 8 Essential PowerShell: Know your operator and enclosure precedence | Poshoholic
3/5/2014 http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...
Kirk Munro
January 12, 2011 at 11:16 am | #5
Reply | Quote
Nice catch, I didnt pick up that one. Youre right about parentheses though, I use
them simply because it eliminates the guesswork for someone reading the script later.
Thanks for sharing!
Kirk out.
koenraad
April 27, 2011 at 8:03 am | #6
Reply | Quote
The same is true for -and and -or
PS> $true -or $true -and $false
False
PS> $true -or ($true -and $false)
True
4.
Sunil
June 19, 2012 at 10:39 pm | #7
Reply | Quote
neatly defined. Well done Kirk!
Kirk Munro
June 20, 2012 at 1:43 pm | #8
Reply | Quote
Thanks! The details in this post actually made it into PowerShell documentation, so
youll see docs with operator precedence in PowerShell in the next release, plus
theyre available online now (http://technet.microsoft.com/en-
us/library/hh847842.aspx).
Kirk out.
1. July 19, 2009 at 3:19 pm | #1
Episode 77 Edward Haletky Talks About VMware PowerScripting Podcast
2. June 14, 2010 at 2:22 am | #2
Seril: Windows Powershell Tipy a triky (st 10.) TechNet Blog CZ/SK Site Home
TechNet Blogs
3. June 15, 2010 at 1:43 pm | #3
Seril: Windows Powershell Pasti, chytky a nechtn pekvapen (st 10.) TechNet
Blog CZ/SK Site Home TechNet Blogs
Leave a Reply
Follow Follow
Page 6 of 8 Essential PowerShell: Know your operator and enclosure precedence | Poshoholic
3/5/2014 http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...
Recover deleted Active Directory objects with the AD Recycle Bin PowerPack PowerShell Quick
Tip: When you store an array, make sure it is an array
RSS feed
Twitter
About me
Kirk Munro
PS C:> Poshoholic, Microsoft MVP, Technical Product Manager
Subscribe by email
Follow Follow
Page 7 of 8 Essential PowerShell: Know your operator and enclosure precedence | Poshoholic
3/5/2014 http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...
Useful Links
PowerShell Team Blog
PowerShell.org
Legal
The posts on this blog are provided as is with no warranties and confer no rights. The opinions
expressed on this site are mine and mine alone, and do not necessarily represent those of my
employer or anyone else for that matter. All trademarks acknowledged.
2012 Kirk Munro
Top
Blog at WordPress.com. The INove Theme.
Follow Follow
Page 8 of 8 Essential PowerShell: Know your operator and enclosure precedence | Poshoholic
3/5/2014 http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...