powershell operator and enclosure precedencer

8
Poshoholic Totally addicted to PowerShell and automation Home About Contact me Type text to search here... Submit Query Home > Essential PowerShell, PowerShell > Essential PowerShell: Know your operator and enclosure precedence Essential PowerShell: Know your operator and enclosure precedence July 8, 2009Leave a commentGo to comments PowerShell version 1 comes with a lot of operators, and the list becomes even longer in version 2 with cool new operators like -split and -join. Whether you’re writing scripts or using PowerShell interactively, dealing with multiple operators in an expression that possibly contains different enclosures (brackets, quotation marks, etc.) as well can be very tricky. It is very important to know how the PowerShell interpreter processes the expression so that you can get your expressions right the first time or, if you’re not so lucky, so that you can identify the problem in your expressions later and fix them. Recently there have been several posts on the forums where the problem has been a lack of understanding of the operator and enclosure precedence in PowerShell. That’s not too surprising because the precedence order used by the PowerShell interpreter doesn’t seem to be documented at this time. You can find the precedence order of arithmetic operators through the about_arithmetic_operators help file, you can find out some precedence details for specific operators in various operator help files, and you can find out the precedence of command types through the about_command_precedence help file, but that’s about it. There is no single help file that documents the overall operator and enclosure precedence. It doesn’t seem to be listed in any of the PowerShell books I have read either. Fortunately through some ad hoc experimentation and through some reading of the help documentation that does exist it is possible to figure out how all of this works. I’ve gone through that exercise recently and the resulting table of operator and enclosure precedence is below. Before getting to the table though there are a few important things I should mention, as follows: 1. Any items that share the same row in the table have the same precedence and are evaluated from left to right when adjacent within an expression unless otherwise indicated. 2. The intent of this table is to identify a precedence that can be used to create or troubleshoot more complicated expressions without a lot of guesswork. It is not intended to explain what each of the operators are and how you can use them (although to help understand expressions you might have to deal with I do mention a few details about some operators that function differently than the majority of operators in PowerShell). 3. If you want to learn about the individual operators and see examples showing how they can Follow Follow Page 1 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...

Upload: annu124

Post on 24-Nov-2015

31 views

Category:

Documents


2 download

DESCRIPTION

powershell

TRANSCRIPT

  • Poshoholic

    Totally addicted to PowerShell and automation

    Home

    About Contact me

    Type text to search here... Submit Query

    Home > Essential PowerShell, PowerShell > Essential PowerShell: Know your operator and

    enclosure precedence

    Essential PowerShell: Know your operator and

    enclosure precedence

    July 8, 2009Leave a commentGo to comments

    PowerShell version 1 comes with a lot of operators, and the list becomes even longer in version 2

    with cool new operators like -split and -join. Whether youre writing scripts or using PowerShell interactively, dealing with multiple operators in an expression that possibly contains different

    enclosures (brackets, quotation marks, etc.) as well can be very tricky. It is very important to know how the PowerShell interpreter processes the expression so that you can get your expressions right the first time or, if youre not so lucky, so that you can identify the problem in

    your expressions later and fix them.

    Recently there have been several posts on the forums where the problem has been a lack of

    understanding of the operator and enclosure precedence in PowerShell. Thats not too surprising because the precedence order used by the PowerShell interpreter doesnt seem to be documented at this time. You can find the precedence order of arithmetic operators through the

    about_arithmetic_operators help file, you can find out some precedence details for specific operators in various operator help files, and you can find out the precedence of command types

    through the about_command_precedence help file, but thats about it. There is no single help file that documents the overall operator and enclosure precedence. It doesnt seem to be listed in any of the PowerShell books I have read either.

    Fortunately through some ad hoc experimentation and through some reading of the help documentation that does exist it is possible to figure out how all of this works. Ive gone through that exercise recently and the resulting table of operator and enclosure precedence is below.

    Before getting to the table though there are a few important things I should mention, as follows:

    1. Any items that share the same row in the table have the same precedence and are evaluated

    from left to right when adjacent within an expression unless otherwise indicated.2. The intent of this table is to identify a precedence that can be used to create or troubleshoot

    more complicated expressions without a lot of guesswork. It is not intended to explain what

    each of the operators are and how you can use them (although to help understand expressions you might have to deal with I do mention a few details about some operators

    that function differently than the majority of operators in PowerShell).3. If you want to learn about the individual operators and see examples showing how they can

    FollowFollow

    Page 1 of 8Essential PowerShell: Know your operator and enclosure precedence | Poshoholic

    3/5/2014http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...

  • With that out of the way, here is the operator and enclosure precedence table for PowerShell:

    [] Type enclosures Any character placed inside of these enclosures is treated as part of a literal type name. The contents are not

    evaluated like an expression would be.

    Double-quoted string enclosures

    Single-quoted string enclosures

    @ @ Double-quoted here-string enclosures

    @ @ Single-quoted here-string enclosures

    {} Script block enclosures

    () Nested expression enclosures

    @() Array subexpression enclosures

    $() Subexpression enclosures

    . Property dereference operator

    :: Static member operator

    [] Index operator

    [int]

    [string[]]etc.

    Cast operators Multiple adjacent operators in this row

    have a right-to-left evaluation.

    -split (unary) Split operator (unary)

    -join (unary) Join operator (unary)

    These operators can be used as unary or

    binary operators. Their precedence varies depending on how they are used.

    , Comma operator This operator is the array element separator. It can be used as an unary or

    binary operator.

    ++ Increment operator

    - - Decrement operator

    These unary operators can be used before or after a variable or property. When used before the variable or

    property (as a prefix operator), the value is incremented or decremented

    first and then the result is passed into the expression in which it is contained. When used after the variable or

    property (as a postfix operator), the value is passed into the expression in

    which it is contained and then the variable or property is immediately incremented or decremented.

    - Negate operator

    -not Not operator

    ! Not operator

    -bnot Bitwise not operator

    Multiple adjacent operators in this row

    have a right-to-left evaluation.

    .. Range operator

    -f Format operator FollowFollow

    Page 2 of 8Essential PowerShell: Know your operator and enclosure precedence | Poshoholic

    3/5/2014http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...

  • * Multiplication operator

    / Division operator

    % Modulus operator

    + Addition operator

    - Subtraction operator

    -split

    -isplit-csplit

    (binary)

    Split operator (binary)

    -join (binary) Join operator (binary)

    -is Type is operator

    -isnot Type is not operator

    -as Type as operator

    -eq

    -ieq-ceq

    Equal to operator

    -ne-ine-cne

    Not equal to operator

    -gt-igt

    -cgt

    Greater than operator

    -ge-ige

    -cge

    Greater than or equal to operator

    -lt

    -ilt-clt

    Less than operator

    -le

    -ile-cle

    Less than or equal to operator

    -like-ilike-clike

    Like operator

    -notlike-inotlike-cnotlike

    Not like operator

    -match-imatch

    -cmatch

    Match operator

    -notmatch-inotmatch

    -cnotmatch

    Not match operator

    -contains

    -icontains-ccontains

    Contains operator

    Does not contain operator

    With the exception of the join operator

    and the type operators (-is, -isnot, and as), each of the operators in this row

    has a case-sensitive and an explicit case-insensitive variant. Case-sensitive variants are prefixed with c (e.g. -ceq)

    and case-insensitive variants are prefixed with i (e.g. ireplace).

    FollowFollow

    Page 3 of 8Essential PowerShell: Know your operator and enclosure precedence | Poshoholic

    3/5/2014http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...

  • -notcontains

    -inotcontains-cnotcontains

    -replace

    -ireplace-creplace

    Replace operator

    -band Bitwise and operator

    -bor Bitwise or operator

    -bxor Bitwise exclusive or operator

    -and Logical and operator

    -or Logical or operator

    -xor Logical exclusive or operator

    . Dot-sourcing operator

    & Call operator

    Unary operators that are only valid at the beginning of an expression, a nested expression, or a subexpression.

    = Assignment operator

    += Assignment by addition operator

    -= Assignment by subtraction operator

    *= Assignment by multiplication operator

    /= Assignment by division operator

    %= Assignment by modulus operator

    Multiple adjacent operators in this row

    have a right-to-left evaluation.

    Since this table is created through experimentation and through snippets of information about

    precedence that I was able to find in the help files, it may not be entirely accurate. If you find any problems with the precedence information provided here, please let me know and Ill update this table accordingly.

    [Update 09-July-2009: Fixed table formatting, added an index operator, added all case-sensitive and case-insensitive variants and adjusted the precedence for the property dereference and static

    member operators.]

    Thanks,

    Kirk out.

    Technorati Tags: PowerShell,PoSh,Poshoholic,Essential PowerShell,operator precedence,enclosure precedence

    Share this post:

    Share this:

    15Twitter Email Print More

    Like

    Be the first to like this.

    Related FollowFollow

    Page 4 of 8Essential PowerShell: Know your operator and enclosure precedence | Poshoholic

    3/5/2014http://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, PowerShellComments (8)Trackbacks (3)Leave a commentTrackback

    1.

    stejJuly 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 MunroJuly 9, 2009 at 1:41 pm | #2Reply | Quote

    Thanks for the feedback, Im glad you like it!

    2.

    Aravind August 19, 2010 at 9:49 pm | #3Reply | Quote

    This is very helpful.

    3.Jeremy Sproat

    January 12, 2011 at 11:02 am | #4Reply | 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 00

    with Python:

    >>> 1 | 0 & 01

    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

    FollowFollow

    Page 5 of 8Essential PowerShell: Know your operator and enclosure precedence | Poshoholic

    3/5/2014http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...

  • Kirk Munro January 12, 2011 at 11:16 am | #5Reply | 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 -orPS> $true -or $true -and $false

    FalsePS> $true -or ($true -and $false)

    True

    4.Sunil

    June 19, 2012 at 10:39 pm | #7Reply | Quote

    neatly defined. Well done Kirk!

    Kirk Munro

    June 20, 2012 at 1:43 pm | #8Reply | 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

    FollowFollow

    Page 6 of 8Essential PowerShell: Know your operator and enclosure precedence | Poshoholic

    3/5/2014http://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 arrayRSS feedTwitter

    About me

    Kirk Munro

    PS C:> Poshoholic, Microsoft MVP, Technical Product Manager

    Subscribe by email

    FollowFollow

    Page 7 of 8Essential PowerShell: Know your operator and enclosure precedence | Poshoholic

    3/5/2014http://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 MunroTopBlog at WordPress.com. The INove Theme.

    FollowFollow

    Page 8 of 8Essential PowerShell: Know your operator and enclosure precedence | Poshoholic

    3/5/2014http://poshoholic.com/2009/07/08/essential-powershell-know-your-operator-and-enclosu...