Moving to another section in a PowerShell script

I want to move from one section in a script to another section in the same PowerShell script. My script is only now going from top to bottom, but I have a few tasks. for example, I want to be able to select from the list of tasks at the beginning of the script and go to task No. 2 to skip task 1.

I hope this makes sense to you. Take a look at my script:

Write-Host -ForegroundColor Yellow "welcome to my powershell script..."
""
""
""
Start-Sleep -Seconds 1
Write-Host -ForegroundColor Yellow "Choose a task:"
""
""

Write-Host -ForegroundColor Yellow "1. Clean up"
Write-Host -ForegroundColor Yellow "2. Uninstall Pre-Installed Apps" 
Write-Host -ForegroundColor Yellow "3. Something should be written here"
""
""
""

While ($Valg -ne "1" -or $Valg -ne "2" -or $Valg -ne "3") {

    $Valg = Read-Host "Choose a number from the task list"

    If ($Valg –eq "1") { Break }
    If ($Valg –eq "2") { Break }
    If ($Valg –eq "3") { Break }

    if ($Valg -ne "1" -or $Valg -ne "2" -or $Valg -ne "3") {
        ""
        Write-Host -ForegroundColor Red "Ups. Try again..."
    }
}

#### 1. First task should come here (clean up)
#some code here for the "cleaning up" task 

#### 2. Second task here
#some code here for the "Uninstall Pre-Installed Apps" task

#### 3. Third task there
#Some code for the third task here

#### And so on...
+4
source share
2 answers

I am updating the answer to a full script based on the inputs "bluuf" and "majkinator". Use the Switch-Case design with the features as shown below. This is a complete working solution.

#region Function Definitions. These come first before calling them
        function FirstTask
        (
            [string] $inputVariable
        )
        {
            "write any scipt for First task here without quotes. Input is: " + $inputVariable
        }

        function SecondTask
        {    
            "write any scipt for Second task here without quotes"
        }
        function ThirdTask
        {    
            "write any scipt for Third task here without quotes"
        }    

#endregion

#region Showing Options
Write-Host -ForegroundColor Yellow "welcome to my powershell script..."
""
""
""
Start-Sleep -Seconds 1
Write-Host -ForegroundColor Yellow "Choose a task:"
""
""

Write-Host -ForegroundColor Yellow "1. Clean up"
Write-Host -ForegroundColor Yellow "2. Uninstall Pre-Installed Apps" 
Write-Host -ForegroundColor Yellow "3. Something should be written here"
Write-Host -ForegroundColor Yellow "0. Exit"
""
""
""
#endregion

#region Getting input
While ($true) {

    $Valg = Read-Host "Choose a number from the task list"

    If ($Valg –eq "0") 
        { 
            "Thanks for using my utility"; 
            Break; 
        }

    If (($Valg -ne "1") -and ($Valg -ne "2") -and ($Valg -ne "3") -and ($Valg -ne "0")) {
        ""
        Write-Host -ForegroundColor Red "Ups. Try again..."
    }

    #region Main Code

    switch ($Valg) 
        { 
            1{
                FirstTask("sending input");
            } 
            2 {
                SecondTask;
            } 
            3 {
                ThirdTask;
            } 
            default { "Please select a correct option."}
        }
    #endregion
}
#endregion
+1
source

, array of PSCustomObject, ( ). , ( ), script:

# define a task list with the messages to display and the functions to invoke:
$taskList = @(
    [PSCustomObject]@{Message = 'Clean up'; Task = { Cleanup }}
    [PSCustomObject]@{Message = 'Uninstall Pre-Installed Apps'; Task = { Uninstall }}
    [PSCustomObject]@{Message = 'Something should be written here'; Task = { Print }}   
)

# define the functions:
function Cleanup()
{
    Write-Host "Cleanup"
}

function Uninstall()
{
    Write-Host "Uninstall"
}

function Print()
{
    Write-Host "Print"
}


# let the user pick a task:
Write-Host -ForegroundColor Yellow "Choose a task:"

$taskList | foreach -Begin { $i = 1;} -Process { 
    Write-Host -ForegroundColor Yellow ('{0}. {1}' -f ($i++), $_.Message) 
}

do 
{
    $value = Read-Host 'Choose a number from the task list'

}
while($value -match '\D+' -or $value -le 0 -or $value -gt $taskList.Count)

# invoke the task:
& $taskList[$value-1].Task
+3

All Articles