# Dynamically creating Switch options with hastable

**URL:** <https://forums.powershell.org/t/dynamically-creating-switch-options-with-hastable/22148>\
**Category:** PowerShell Help\
**Created:** [April 15, 2023, 12:25pm UTC](https://forums.powershell.org/t/dynamically-creating-switch-options-with-hastable/22148 "2023-04-15T12:25:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramakant](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ramakant/32/1719_2.png) [@ramakant](https://forums.powershell.org/u/ramakant)\
**Post date:** [April 15, 2023, 12:25pm UTC](https://forums.powershell.org/t/dynamically-creating-switch-options-with-hastable/22148/1 "2023-04-15T12:25:18Z")

</div>

Hi All,  
I was writing script to install and uninstall applications with winget. I have created hashtable to enter applications details so user can change applications as per requirments now i wanted to use switch statement for user selection but I am not able to retrive user selection into switch statement

here is my script

```auto
$apps= @{}
$apps.Add(1,@('Chrome','Google.Chrome'))
$apps.Add(2,@('Anydesk','AnyDeskSoftwareGmbH.AnyDesk'))

Clear-Host
$ex=0
while($ex -ne 20)
{
    Write-Host "---------------Menu---------------------" -ForegroundColor Cyan
    Write-Host "1.Install Apps" -ForegroundColor Cyan
    Write-Host "2.Uninstall Apps" -ForegroundColor Cyan
    Write-Host "3.Update All Apps" -ForegroundColor Cyan
    Write-Host "20.Exit" -ForegroundColor Cyan
    Write-Host "------------------------------------------" -ForegroundColor Cyan
    switch($ex=Read-Host "Select a menu item"){
        1{ 
            $exsub=0
            while ($exsub -ne 20) 
            {
                Write-Host "---------------SubMenu---------------------"
                    for($i=1;$i -le $apps.Count; $i++)
                    {
                        Write-Host "$i Install "$apps[$i][0]
                    }            
                Write-Host "20.Exit"
                Write-Host "------------------------------------------" -ForegroundColor DarkGray
                switch($exsub=Read-Host "Select a sub-menu item"){                              
                    $exsub{
                        if($exsub -eq 20)
                        {
                            Write-Host "Ending" -ForegroundColor DarkGray
                        }
                        else {
                            Write-host "User Selected"$exsub
                            Write-Host $apps[1][1]
                            Write-Host "user choice was " $apps[$exsub][1]  
                        }
                                  
                    }              
            
                default {Write-Host "Invalid entry" -ForegroundColor DarkGray}
                }
            }
        }
    }
}
    

```

And here error

```auto
Select a sub-menu item: 20
---------------Menu---------------------
1.Install Apps
2.Uninstall Apps
3.Update All Apps
20.Exit
------------------------------------------
Select a menu item: 1
---------------SubMenu---------------------
1 Install Chrome
2 Install Anydesk
20.Exit
------------------------------------------
Select a sub-menu item: 1
User Selected 1
Google.Chrome
InvalidOperation: C:\Users\Ramakant\Desktop\Ramakant\powershell\temp2.ps1:36:29
Line |
  36 | … Write-Host "user choice was " $apps[$exsub][1]
     | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.
---------------SubMenu---------------------
1 Install Chrome
2 Install Anydesk
20.Exit
------------------------------------------
Select a sub-menu item: 20
Ending

```

Thank You in Advance.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 15, 2023, 12:53pm UTC](https://forums.powershell.org/t/dynamically-creating-switch-options-with-hastable/22148/2 "2023-04-15T12:53:18Z")

</div>

Change this

```auto
              Write-Host "user choice was " $apps[$exsub][1]  

```

to

```auto
              Write-Host "user choice was " $apps[[int]$exsub][1]

```

---

<div class="post-metadata">

**Author:** ![ramakant](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ramakant/32/1719_2.png) [@ramakant](https://forums.powershell.org/u/ramakant)\
**Post date:** [April 15, 2023, 1:23pm UTC](https://forums.powershell.org/t/dynamically-creating-switch-options-with-hastable/22148/3 "2023-04-15T13:23:58Z")

</div>

Thanks @Olaf , It worked.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/dynamically-creating-switch-options-with-hastable/22148/4 "2024-05-16T20:14:58Z")

</div>


