PowerShell – Import CSV file to ComboBox and get selected value into variable

Posted: April 21, 2021 in Scripts

File 1.csv:

ServerName
Server1
Server2
Server3

This code will “load” this file into PowerShell form and will output selected value into variable

###################Load Assembly for creating form & button######
 
[void][System.Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.VisualBasic")
 
 
#####Define the form size & placement
 
$form = New-Object "System.Windows.Forms.Form";
$form.Width = 500;
$form.Height = 190;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form.ControlBox = $True
 
 
##############Define text label2
 
$textLabel2 = New-Object "System.Windows.Forms.Label";
$textLabel2.Left = 25;
$textLabel2.Top = 80;
 
$textLabel2.Text = $WF;
 
 
############Define text box2 for input
 
$cBox2 = New-Object "System.Windows.Forms.combobox";
$cBox2.Left = 150;
$cBox2.Top = 80;
$cBox2.width = 200;
 
 
###############"Add descriptions to combo box"##############

import-csv "C:\Users\Komp\Desktop\1.csv" | ForEach-Object {
    $cBox2.Items.Add($_.ServerName)
     
}
 
 
#############define OK button
$button = New-Object "System.Windows.Forms.Button";
$button.Left = 360;
$button.Top = 45;
$button.Width = 100;
$button.Text = “Ok”;
$Button.Cursor = [System.Windows.Forms.Cursors]::Hand
$Button.Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::BOLD)
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$cBox2.Text;
$form.Close();};
$button.Add_Click($eventHandler) ;
 
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel2);
$form.Controls.Add($cBox2);
#$ret = $form.ShowDialog();
 
#################return values
$button.add_Click({
     
    #Set-Variable -Name locationResult -Value $combobox1.selectedItem -Force -Scope Script # Use this
    $script:locationResult = $cBox2.selectedItem # or this to retrieve the user selection
})
 
$form.Controls.Add($button)
$form.Controls.Add($cBox2)
 
$form.ShowDialog()
 
$output = $script:locationResult

Combo box values different from Displayed values

Let’s say we want do display in GUI one value, but real output value is something different, for example

In form we want to show Country code (Serbia), but actual value is Code (SRB)

1.csv:

Country, Code
Serbia, SRB
Germany, DE
Russia, RU

I’ll show just what’s changed:

###############"Add descriptions to combo box"##############

$input = import-csv "C:\Users\Komp\Desktop\1.csv"
$cBox2.DisplayMember = 'Country'
    $input | ForEach-Object {
    $cBox2.Items.Add($_)
}

#################return values
$button.add_Click({
     
    #Set-Variable -Name locationResult -Value $combobox1.selectedItem -Force -Scope Script # Use this
    $script:locationResult = $cBox2.selectedItem.Code # or this to retrieve the user selection
    
})

Output will be SRB

Advertisement
Comments
  1. David says:

    Nice example, but is there a way how to define the Value and the Text to the Combobox? Something like user will se in the Combobox for example “Germany” but the value inside will be “DE”?

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s