obious
obious

Reputation: 629

Powershell: Odd output when using foreach and listbox

I'm currently experimenting and trying to get this code:

$processnames = Get-Process | select name 
foreach ($processname in $processnames)
{
[void]$AD_list_current.Items.Add($processnames)

}

to output in a listbox. It outputs as it is but for some reason it keeps displaying in the listbox as:enter image description here

Does anyone know why this is?

It would be greatly apprechiated.

Thank you

Upvotes: 1

Views: 1535

Answers (3)

it-king
it-king

Reputation: 21

Try this:

[void]$AD_list_current.Items.Add($($processnames))

Upvotes: 2

Shay Levy
Shay Levy

Reputation: 126902

Here's a shorter way to add all process names:

$list.Items.AddRange( (Get-Process | select name) )

Upvotes: 3

JNK
JNK

Reputation: 65197

You are adding $processnames instead of $processname to your list.

That's an array.

As per Joey's comment, the default ToString() method will show the type name, which is System.Object.

Upvotes: 1

Related Questions