Power Shell menu

Hey people

I have written a powershell script, it has a menu system, that goes to a sub menu. and i in the sub menu,
I want one of the options to be back to main menu. How can i achieve this. or a command i can use to do this, or go back to a specific part of the script

script is

# PowerShell console window Style
$pshost = get-host
$pswindow = $pshost.ui.rawui

    $newsize = $pswindow.buffersize
    
    if($newsize.height){
        $newsize.height = 3000
        $newsize.width = 150
        $pswindow.buffersize = $newsize
    }

    $newsize = $pswindow.windowsize
    if($newsize.height){
        $newsize.height = 50
        $newsize.width = 150
        $pswindow.windowsize = $newsize
    }

# MAIN Menu system
$Loop = $true
While ($Loop)
{
    write-host
    write-host                                              ' 1)   go to menu1'
    write-host                                              ' 2)   go to menu 2'
    write-host                                              ' 3)   exit'
                                       

    

    $opt = Read-Host "Select an option [0-3]"
    write-host $opt
    switch ($opt)


"1" {
$Loop = $true
While ($Loop)

{   write-host
    write-host                                              ' 1)   script 1'
    write-host                                              ' 2)   script 2'
    write-host                                              ' 3)   script 3'
    write-host                                              ' 4)   script 4 '
    write-host                                              ' 5)   script 5 '
    write-host                                              ' 6)   back to Main Menu'
    write-host                                              
    

    $opt = Read-Host "Select an option [0-6]"
    write-host $opt
    switch ($opt)


"1" { #script}


"2" { #script}

  "3" { #script# }


  "4" {#script }

  "5" {#script #}

  "6" {???????????}

      default {"$_ is an invalid choice"}

      }
   }
 


}

"3"  {Exit }
      default {"$_ is an invalid choice"}

      }
   
   }

May 20th, 2015 8:56am

There is lot of menus available on the internet. Why reinvent the wheel.

I personally do like a little modified
http://mspowershell.blogspot.cz/2009/02/cli-menu-in-powershell.html

It only works on standard powershell console, in powershell_ise or other GUI tools it throws some errors :(
I use this menu a lot, for Exchange purposes (common tasks, getting some info..) and so on.

function DrawMenu
{
	## supportfunction to the Menu function below
	param ($menuItems, $menuPosition, $menuTitle)
	$fcolor = $host.UI.RawUI.ForegroundColor
	$bcolor = $host.UI.RawUI.BackgroundColor
	$l = $menuItems.length + 1
	clear-host
	$menuwidth = $menuTitle.length + 4
	Write-Host "`t" -NoNewLine
	Write-Host ("*" * $menuwidth) -fore $fcolor -back $bcolor
	Write-Host "`t" -NoNewLine
	Write-Host "* $menuTitle *" -fore $fcolor -back $bcolor
	Write-Host "`t" -NoNewLine
	Write-Host ("*" * $menuwidth) -fore $fcolor -back $bcolor
	Write-Host ""
	Write-debug "L: $l MenuItems: $menuItems MenuPosition: $menuposition"
	for ($i = 0; $i -le $l;$i++) 
	{
		Write-Host "`t" -NoNewLine
		if ($i -eq $menuPosition) {Write-Host "$($menuItems[$i])" -fore $bcolor -back $fcolor}
		else{Write-Host "$($menuItems[$i])" -fore $fcolor -back $bcolor}
	}
}

function Menu
{
	param ([array]$menuItems, $menuTitle = "MENU")
	$vkeycode = 0
	$pos = 0
	DrawMenu $menuItems $pos $menuTitle
	While ($vkeycode -ne 13)
	{
	$press = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown")
	$vkeycode = $press.virtualkeycode
	Write-host "$($press.character)" -NoNewLine
	If ($vkeycode -eq 38) {$pos--}
	If ($vkeycode -eq 40) {$pos++}
	if ($pos -lt 0) {$pos = $menuitem.Length-1} else {if ($pos -ge $menuItems.length) {$pos = 0}}
	DrawMenu $menuItems $pos $menuTitle
	}
	Write-Output $($menuItems[$pos])
}

do
{
$menuitem = "1. Users","2. Computers","X. Exit"
$selection = Menu $menuitem "AD Resource Administrator v 1.00"
switch ($selection)
	{
	"1. Users" {"Users Account Management";do
		{
		$menuitem = "1.1. Display User Properties","1.2. Create new User","1.X. Back"
		$selection = Menu $menuitem "1. User Account Management"
		switch ($selection)
			{
			"1.1. Display User Properties"
				{
				write-host "This will display the AD user properties ..."
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"1.2. Create new User"
				{
				write-host "This will create new AD user"
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"1.X. Back"{"Back to main menu"}
			}
		}until ($selection -eq "1.X. Back")
	}
	"2. Computers" {"Computer Account Management";do
		{
		$menuitem = "2.1. Display computer properties","2.2. Create new computer","2.3. Move computer to an OU","2.X. Back"
		$selection = Menu $menuitem "2. Computer Account Management"
		switch ($selection)
				{
			"2.1. Display computer properties"
				{
				write-host "Script finished, press any key to continue ..."
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"2.2. Create new computer"
				{
				write-host "Script finished, press any key to continue ..."
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"2.3. Move computer to an OU"
				{
				write-host "Script finished, press any key to continue ..."
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"2.X. Back"{"Back to main menu"}
			}
		}until ($selection -eq "2.X. Back")
		}
	
	"X. exit"{"Exiting script.."}
	}
}until ($selection -eq "X. exit")

  • Proposed as answer by Mekac 13 hours 0 minutes ago
Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 9:24am

Cheers Fella, Il Give it a go :)

Im using it for exchange scripts, so the Service desk can easily and quickly check permissions, grant, set OOO ect....

May 20th, 2015 9:39am

There is lot of menus available on the internet. Why reinvent the wheel.

I personally do like a little modified
http://mspowershell.blogspot.cz/2009/02/cli-menu-in-powershell.html

It only works on standard powershell console, in powershell_ise or other GUI tools it throws some errors :(
I use this menu a lot, for Exchange purposes (common tasks, getting some info..) and so on.

function DrawMenu
{
	## supportfunction to the Menu function below
	param ($menuItems, $menuPosition, $menuTitle)
	$fcolor = $host.UI.RawUI.ForegroundColor
	$bcolor = $host.UI.RawUI.BackgroundColor
	$l = $menuItems.length + 1
	clear-host
	$menuwidth = $menuTitle.length + 4
	Write-Host "`t" -NoNewLine
	Write-Host ("*" * $menuwidth) -fore $fcolor -back $bcolor
	Write-Host "`t" -NoNewLine
	Write-Host "* $menuTitle *" -fore $fcolor -back $bcolor
	Write-Host "`t" -NoNewLine
	Write-Host ("*" * $menuwidth) -fore $fcolor -back $bcolor
	Write-Host ""
	Write-debug "L: $l MenuItems: $menuItems MenuPosition: $menuposition"
	for ($i = 0; $i -le $l;$i++) 
	{
		Write-Host "`t" -NoNewLine
		if ($i -eq $menuPosition) {Write-Host "$($menuItems[$i])" -fore $bcolor -back $fcolor}
		else{Write-Host "$($menuItems[$i])" -fore $fcolor -back $bcolor}
	}
}

function Menu
{
	param ([array]$menuItems, $menuTitle = "MENU")
	$vkeycode = 0
	$pos = 0
	DrawMenu $menuItems $pos $menuTitle
	While ($vkeycode -ne 13)
	{
	$press = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown")
	$vkeycode = $press.virtualkeycode
	Write-host "$($press.character)" -NoNewLine
	If ($vkeycode -eq 38) {$pos--}
	If ($vkeycode -eq 40) {$pos++}
	if ($pos -lt 0) {$pos = $menuitem.Length-1} else {if ($pos -ge $menuItems.length) {$pos = 0}}
	DrawMenu $menuItems $pos $menuTitle
	}
	Write-Output $($menuItems[$pos])
}

do
{
$menuitem = "1. Users","2. Computers","X. Exit"
$selection = Menu $menuitem "AD Resource Administrator v 1.00"
switch ($selection)
	{
	"1. Users" {"Users Account Management";do
		{
		$menuitem = "1.1. Display User Properties","1.2. Create new User","1.X. Back"
		$selection = Menu $menuitem "1. User Account Management"
		switch ($selection)
			{
			"1.1. Display User Properties"
				{
				write-host "This will display the AD user properties ..."
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"1.2. Create new User"
				{
				write-host "This will create new AD user"
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"1.X. Back"{"Back to main menu"}
			}
		}until ($selection -eq "1.X. Back")
	}
	"2. Computers" {"Computer Account Management";do
		{
		$menuitem = "2.1. Display computer properties","2.2. Create new computer","2.3. Move computer to an OU","2.X. Back"
		$selection = Menu $menuitem "2. Computer Account Management"
		switch ($selection)
				{
			"2.1. Display computer properties"
				{
				write-host "Script finished, press any key to continue ..."
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"2.2. Create new computer"
				{
				write-host "Script finished, press any key to continue ..."
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"2.3. Move computer to an OU"
				{
				write-host "Script finished, press any key to continue ..."
				$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
				}
			"2.X. Back"{"Back to main menu"}
			}
		}until ($selection -eq "2.X. Back")
		}
	
	"X. exit"{"Exiting script.."}
	}
}until ($selection -eq "X. exit")

  • Proposed as answer by Mekac Wednesday, May 20, 2015 6:09 PM
Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 1:17pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics