Populating dropdown lists in PowerShell

Populating a second dropdown list based on your choice in the first dropdown list

Following on from my post yesterday in the week regarding the creating of forms in PowerShell, I came across a situation that I need to work on. I have a form that contains 2 dropdown lists. The first list contains a list of all the volumes that are on my NetApp filer. The second list is populated with a list of all the snapshots from which ever volume I choose in the first list.

When I change my choice in drop-down list 1, I want to see the contents of list 2 change and reflect my choice by listing the snapshots of the selected volume.

So here is the code:

# import the Data-ONTap module
Import-module DataONTAP
# Connect to you Netapp filer
Connect-NaController "NetApp filer Name"     # change this to your filer name

# Get a list of all filer volumes
[array]$MyDropDownList1 = Get-NaVol
# Function to return the name of the selected snaphsot from the second DropDown box
function Return-DropDown {
$script:Choice = $DropDown2.SelectedItem.ToString()
# Function tp populate the second DropDown box with a list of snapshots of the choosen volume
# in the first dropdown list
function populate_snapshots{
    # The line below clears the DropDown2 list of snapshots
    $volume = $DropDown1.SelectedItem
    [array]$MyDropDownList2 = Get-NaSnapshot -TargetName $volume
    ForEach ($Item in $MyDropDownList2) {
    [void] $DropDown2.Items.Add($Item)
function SelectSnapshot{
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    # Set the size of your form
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 500
    $Form.height = 200
    $Form.Text = ”Multiple DropDown boxes”
    # Set the font of the text to be used within the form
    $Font = New-Object System.Drawing.Font("Times New Roman",12)
    $Form.Font = $Font
    $DropDown1 = new-object System.Windows.Forms.ComboBox
    $DropDown1.Location = new-object System.Drawing.Size(130,20)
    $DropDown1.Size = new-object System.Drawing.Size(280,40)
    $DropDown2 = new-object System.Windows.Forms.ComboBox
    $DropDown2.Location = new-object System.Drawing.Size(130,60)
    $DropDown2.Size = new-object System.Drawing.Size(280,40)
    $Button = new-object System.Windows.Forms.Button
    $Button.Location = new-object System.Drawing.Size(130,100)
    $Button.Size = new-object System.Drawing.Size(100,40)
    $Button.Text = "Select Snapshot"
    $CancelButton = new-object System.Windows.Forms.Button
    $CancelButton.Location = new-object System.Drawing.Size(255,100)
    $CancelButton.Size = new-object System.Drawing.Size(100,40)
    $CancelButton.Text = "Cancel"
    ForEach ($Item in $MyDropDownList1) {
        [void] $DropDown1.Items.Add($Item.Name)
    [void] $Form.ShowDialog()
    return $script:choice
# call the function
$option = @()
$option = SelectSnapshot
# Return your selection in text format
Write-Host $option

As always, feel free to play/modify the code and have fun.

For more information on using PowerShell to create forms:


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.