Results are shown as @{...}

Hi guys,

I am still a beginner with Powershell and I am trying to create a Windows Forms application.

This is what I need: a toolstripcombobox, where its items are updated according to a variable.

Let me show you a summary:

$toolstripcombobox1_Click={
$Location = dir *.txt | select Basename
$toolstripcombobox1.Items.Clear()
$toolstripcombobox1.Items.AddRange($Location)
}

I have 2 textfiles in the same directory as the Powershell script, called File1.txt and File2.txt.

I would like my combobox items to have the name of those files, but instead they always appear as

@{BaseName=File1}

@{BaseName=File2}

(See attachment)

How can I make them to be called simply File1 and File2?

 

I figured it out in another way:

$toolstripcombobox1_Click={
$toolstripcombobox1.Items.Clear()
dir *.txt | sort Basename| ForEach-Object {$toolstripcombobox1.Items.AddRange($_.basename)}
}

Thanks for your effort!

If somebody could still tell me what was wrong in my first post, please tell me!

 

Have a nice day!

When you see output as @{this=that;those=these}, or something similar, it’s because you’ve taken multiple objects (a collection) and tried to convert them all to a single string. That formatting is just how PowerShell deals with that situation.

In your original, it’s because you were selecting the BaseName property of multiple objects. That’s always a collection of objects. In your revision, you used ForEach-Object to go through them one at a time. So rather than converting the collection to a single string (which PowerShell can’t really do all that well), it converts a single object to a single string (which it does just fine).