1

Starting with this bit of code from this answer:

stringList.ForEach(item => listBox1.Items.Add(item));

I've put my own variables into the line like so:

$listAgencies = $Window.FindName('listAgencies')
$arr_listAgencies = ($hash_AgencyOffices['ADMIN1','ADMIN2'].Keys | ForEach-Object ToString)
$arr_listAgencies.ForEach(item => $listAgencies.Items.Add(item))

I have confirmed that $arr_listAgencies is a list of the values I need in the ListBox $listAgencies, but I'm guessing I am entirely missing the importance of item in that last line: (item => $listAgencies.Items.Add(item)).

When I run this PS kindly informs me that there is a missing ')' in method call and there is an unexpected token 'item' in expression or statement.

Is item supposed to be $._, the pipeline variable? I thought that might have been it and tried it, but the error was essentially the same.

What am I missing?

1 Answer 1

3

In item => {code} the => operator is not available in PowerShell. It's a lambda expression operator for C#.

In order to achieve what you want, try this

$arr_listAgencies | ForEach-Object {
    $listAgencies.Items.Add($_) | Out-Null
}
Sign up to request clarification or add additional context in comments.

1 Comment

That indeed worked. Thank you for clarifying that for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.