Home > OS >  How to populate Combobox with result of Powershell command
How to populate Combobox with result of Powershell command

Time:10-14

Can someone assist on how to fill a combobox with the result of a powershell command?

Im trying to fill a combobox with the result of a "Get" cmdlet but I only get some powershell parameters as result.

$ButtonCollectionSearch.Add_Click({
    $name = $textboxlogonname.text
    $ComboBox = New-Object System.Windows.Forms.ComboBox
    $ComboBox.Width = 400
    $Collections = Get-RDSessionCollection | fl  -Property CollectionName

    Foreach ($Collection in $Collection) {
        $ComboBox.Items.Add($Collection);
    }
    $ComboBox.Location = New-Object System.Drawing.Point(120, 10)
    $main_form.Controls.Add($ComboBox)
})

enter image description here

CodePudding user response:

The reason you're getting formatting metadata is that you asked for formatting metadata - by piping all your data through fl (which is a alias for Format-List).

Since we just want the value of the CollectionName, use ForEach-Object -MemberName in place of fl -Property:

$Collections = Get-RDSessionCollection | ForEach-Object -MemberName CollectionName

You'll also want to address the typo in the foreach loop declaration - change:

Foreach ($Collection in $Collection) {

to:

Foreach ($Collection in $Collections) {
  • Related