Search code examples
arrayspowershellforeachcombobox

DriveLetter ComboBox not populating correctly


I'm creating a GUI script showing currently unused/unassigned drive letters. While the function works properly (ECHO button writes correct Letters to PS) the ComboBox is populated by a single letter (see image). Could someone explain to me what I did wrong?

Write-Host & ComboBox

Current Assigned Letters

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Test' 
$form.Size = New-Object System.Drawing.Size(280,400) 
$form.StartPosition = 'Manual'
$form.Location      = '10,10'
$form.Topmost = $true  

$button = New-Object System.Windows.Forms.Button
$button.Size = New-Object System.Drawing.Size(50,20)
$button.Location = New-Object System.Drawing.Size(20,20)
$button.Text = "ECHO"
$button.Add_Click($button_click)

$comboBox1 = New-Object System.Windows.Forms.ComboBox 
$comboBox1.Location = New-Object System.Drawing.Point(80, 55) 
$comboBox1.Size = New-Object System.Drawing.Size(98, 10) 
$ComboBox1.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;

$AllLetters = 67..90 | ForEach-Object {[char]$_ + ":"}
$UsedLetters = Get-WmiObject Win32_LogicalDisk | Select -Expand DeviceID
$FreeLetters = $AllLetters | Where-Object {$UsedLetters -notcontains $_}
ForEach ($Letter in $FreeLetters) { $comboBox1.Items.Add($Drives) }



$button_click = {Write-Host ($FreeLetters)}                   

$form.Controls.AddRange(@($button, $combobox1))
$form.ShowDialog()

Solution

  • In this Line:

    ForEach ($Letter in $FreeLetters) { $comboBox1.Items.Add($Drives) }
    

    Change $Drives to $Letter