Reputation: 897
I've been looking online for a specific answer to better help me understand how this works. In PHP we use the " . " to concatenate strings. However in powershell I see things like this:
Dir | where {$_.extension -eq ".txt"} |
Rename-Item –NewName { $_.name –replace “.“,”-” }
I can see that the "Dir" command is piped to "Where" but, I don't understand what its defining a variable for using:
$_.extension
Is this a way of adding extra operators to a function?? I'm pretty confused. I'm getting better but, I need to know how exactly periods and the $_. work when using the cmdlets and what not.
Any help is appreciated.
Upvotes: 0
Views: 1114
Reputation: 68273
Powershell has very good help files included that can answer many questions.
See:
get-help about_operators
and you will find that the dot is used as both a Property dereferencing operator and a scope operator, with explanations of the use of each.
Can also see this under about_operators on TechNet
Upvotes: 6
Reputation: 696
DIR
command is similar to Get-ChildItem
command. The |
is similar to foreach
statement. The $_
sign indicates each element in foreach
loop. In your case, the code should get all which have .txt
extension from some location and then rename each of those elements due to { $_.name –replace “.“,”-” }
rule
Upvotes: 0
Reputation: 33089
It's the member access operator. $_
is a special variable (the loop variable in this case). Therefore, $_.extension
accesses or invokes the property extension
on $_
.
Upvotes: 2