I have the following code that looks for a string in multiple files, part of which I found here.
$path = C:\Windows
Set-Location -path $path
$searchWords = 'log.txt'
Foreach ($sw in $searchWords)
{
Get-Childitem -Path $path -Recurse -include "*.txt","*.dll" |
Select-String -Pattern "$sw" |
Select Path,LineNumber,@{n='SearchWord';e={$sw}}
}
The syntax I don't think I understand is this part in the last line:
@{n='SearchWord';e={$sw}}
I'll explain what I think I understand and then ask questions.
- @ I think means it is an array
- n= is shorthand for 'name'
- the colon(;) is separating the name of the column and the expression that fills the column.
- e= is shorthand for expression
- {$sw} - the brackets are necessary here to encapsulate the expression.
Question(s):
- Why is an array used to populate this column?
- Why must an expression be used and not just the variable '$sw'?
Thanks for the help!