Share via


Windows PowerShell: using SWITCH CHOICE

How to build a PowerShell script using SWITCH CHOICE?

Press 1 to get System Service details.
Press 2 to get System BIOS information.
Press 3 to exit.

Solution:



function switchingpowershell()
{
[int]$Option = 0
while ( $Option -lt 1 -or $Option -gt 5 ){
Write-host "1. Get Computer Services" -ForegroundColor Yellow
Write-host "2. Get BIOS Information" -ForegroundColor Yellow
Write-host "3. Quit and exit" -ForegroundColor Yellow
Write-Host " "
Write-Host " "
  
[Int]$Option = read-host "Please enter an option 1 to 3..." }
Switch( $Option ){
  1{Get-WmiObject -Class Win32_Service
    switchingpowershell}
  2{Get-WmiObject -Class Win32_BIOS
    switchingpowershell}
  3{Write-host "Script Terminated!!!" -ForegroundColor Green
      Break}
}
}
switchingpowershell

Call your custom function as required so that the script won't terminate until you press 3.