MR.INTJ00
MR.INTJ00

Reputation: 69

What does mean % , $_ and @ in Powershell?

What does this mean: $_ and % in Powershell?

1..10 | Foreach {if($_%2){"$_ is odd number"}}

Upvotes: 3

Views: 25270

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25031

  • %
    • In your case, it is the modulus operator. It will return the remainder of dividing the left-hand side value by the right-hand side value.
    • It defaults as a PowerShell alias for Foreach-Object. You can execute the Get-Alias command to see other potential aliases that may contain special characters like Where-Object's alias ?.

  • $_
    • Synonymous with $PSItem
    • Contains the current object in the pipeline object
    • In your case, it represents the current object passed into your Foreach-Object script block ({}).
    • It will commonly show up in the Where-Object {} script block and Select-Object hash tables.

  • @

    • A literal @ character
    • Denotes splatting
      • The syntax is @VariableName. The variable can be an array or hash table. It is commonly used with a hash table or dictionary where the Name property represents a parameter name and the value property is the value for that parameter. Then that variable is splatted into another command. An example is Get-Process @Params.
    • Used for declaring and initializing arrays via the array sub-expression operator @().

      • Examples are $myArray = @() and $myArray = @("value1","value2").
    • Used to create and/or initialize a hash table
      • The syntax is $variable = @{} or $variable = @{Property=Value}.
    • Used in here-strings
      • Here-strings are special case strings that can expand multiple lines and contain special characters
      • Denoted by beginning a string value with @' or @" and closing the string value with a corresponding '@ or "@.
        • The here-string open and close characters should be isolated on their respective lines of the right-hand side (RHS).
    • Common At symbol
      • Used in email address construction, i.e. [email protected].
      • Used in external program remote logon syntax, i.e. user@hostname.

Extra Reading and Notable Links:

  1. See About Arithmetic Operators for information on modulus among other arithmetic operators.

  2. See Foreach-Object for more information about Foreach-Object and how objects are processed.

  3. See About Splatting for more information and usage of splatting.

  4. Another good resource is About Automatic Variables, which will list PowerShell's reserved/automatic variables. They are created and maintained by PowerShell. You will notice there are some variables that have non-alpha and non-numeric characters. You should only use these variables for their intended purposes and not use their names when you create your own custom variables.

  5. See About Arrays for details on the array sub-expression operator.

  6. See About Hash Tables for details on creating and manipulating hash table objects.

  7. See About Quoting Rules to see more information and examples of using here-strings.

Upvotes: 24

Related Questions