Powershell script control to come back to Main Menu

Abhishek Reddy 61 Reputation points
2022-02-22T06:13:45.367+00:00

Powershell script control to come back to Main Menu

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2022-02-22T14:27:09.427+00:00

    Your code is very difficult to read. When posting code, you should use the Code Sample tool, the 101010 icon. And you should also indent your statements to make it easier to see what code is part of which statement. You have everything shifted to the leftmost column.

    Your Show-Menu function doesn't do much. I don't see much value in making that a function. You would be better off by putting the application part of your code into functions.

    Organize your script like this.

    function Process1 {
        Write-Host ""
        Write-Host "Do option 1 here."
        Write-Host "" 
    } 
    
    function Process2 {
        Write-Host ""
        Write-Host "Do option 2 here."
        Write-Host "" 
    } 
    
    $loop = $true
    While ($loop) {
        Write-Host "Enter 1 to do option 1."
        Write-Host "Enter 2 to do option 2."
        Write-Host "Enter q to quit."
        $select = Read-Host
        switch ($select) {
            '1' { Process1 } 
            '2' { Process2 }
            'q' { $loop = $false }
            Default { Write-Host "Invalid selection." }
        }
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.