Chapters/basic-controllers-scripts-and-menus/basicmenu.ps1

#create a basic console based menu

#define a here string for the menu options
$menu = @"
 
        MyMenu
--------------------------
1. Get services
2. Get processes
3. Get System event logs
4. Check free disk space (MB)
5. Quit
 
Select a menu choice
"@


#Read-Host writes strings but we can specifically treat the result as
#an integer
[int]$r = Read-Host $menu

$Computername = Read-Host "Enter a computername or press Enter to use localhost"
if ($Computername -notmatch "\w+") {
    $computername = $env:COMPUTERNAME
}

#code to execute
Switch ($r) {
  1 {
      Get-Service -computername $Computername
  }
  2 {
      Get-Process -computername $Computername
  }
  3 {
      Get-Eventlog -LogName System -Newest 25 -ComputerName $Computername
  }
  4 {
      $params = @{
        ClassName = "win32_logicaldisk"
        ComputerName = $Computername
        Filter = "deviceid='c:'"
      }
      $c = Get-CimInstance @params
      $c.FreeSpace/1mb
  }
  5 {
      Write-Host "Have a nice day" -ForegroundColor Green
   }
  default { 
      Write-Warning "$r is not a valid choice"
    }
} #close switch