none
GoTo Cmdlet in Windows Powershell RRS feed

  • Question

  • Hello everyone,

    I am editing a PS script of my colleague. I have to add an IF option; in the ELSE option I want to add a cmdlet so the script continues at a later point, because there are lines between that, that mustn't be executed.
    Is there a possibility to do that with a GoTo or Jump command?
    I am using Powershell 5.0 on a Win 10 PC.

    Thank you in advance!


    Kind Regards

    -Nikolai

    Monday, April 4, 2016 8:42 AM

Answers

  • Hi Nikolai,

    nope, there isn't such a mechanism in PowerShell and there is no need to, thanks to quite a lot of things, especially if/else. Here an example of how it works:

    # Change this to 1 or 2, depending on how you want to run this script:
    $num = 1
    
    if ($num -eq 1)
    {
        # This will run only if $num was set to 1
        Write-Host '$num was indeed 1'
    }
    else
    {
        # This will run only if $num was set to any value other than 1
        Write-Host '$num was not 1!'
    }
    # This will run no matter what $num was set to
    Write-Host "`$num was set to $num"

    For a more detailed documentation, try running this on the console:
    Get-Help about_if

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Nikolai B Monday, April 4, 2016 9:15 AM
    Monday, April 4, 2016 8:53 AM

All replies

  • Hi Nikolai,

    nope, there isn't such a mechanism in PowerShell and there is no need to, thanks to quite a lot of things, especially if/else. Here an example of how it works:

    # Change this to 1 or 2, depending on how you want to run this script:
    $num = 1
    
    if ($num -eq 1)
    {
        # This will run only if $num was set to 1
        Write-Host '$num was indeed 1'
    }
    else
    {
        # This will run only if $num was set to any value other than 1
        Write-Host '$num was not 1!'
    }
    # This will run no matter what $num was set to
    Write-Host "`$num was set to $num"

    For a more detailed documentation, try running this on the console:
    Get-Help about_if

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Nikolai B Monday, April 4, 2016 9:15 AM
    Monday, April 4, 2016 8:53 AM
  • Okay, Thank you.
    I think I can work with this solution.

    Kind Regards

    - Nikolai

    Monday, April 4, 2016 9:17 AM