Reputation: 69
What does this mean: $_
and %
in Powershell?
1..10 | Foreach {if($_%2){"$_ is odd number"}}
Upvotes: 3
Views: 25270
Reputation: 25031
%
Foreach-Object
. You can execute the Get-Alias
command to see other potential aliases that may contain special characters like Where-Object
's alias ?
.$_
$PSItem
Foreach-Object
script block ({}
).Where-Object {}
script block and Select-Object
hash tables.@
@
character@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 @()
.
$myArray = @()
and $myArray = @("value1","value2")
.$variable = @{}
or $variable = @{Property=Value}
.@'
or @"
and closing the string value with a corresponding '@
or "@
.
Extra Reading and Notable Links:
See About Arithmetic Operators for information on modulus among other arithmetic operators.
See Foreach-Object for more information about Foreach-Object
and how objects are processed.
See About Splatting for more information and usage of splatting.
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.
See About Arrays for details on the array sub-expression operator.
See About Hash Tables for details on creating and manipulating hash table objects.
See About Quoting Rules to see more information and examples of using here-strings.
Upvotes: 24