Internet Explorer is no longer supported. Many things will still work, but your experience will be degraded and some things won't function. Please use a modern browser such as Edge, Chrome, or Firefox.

If / Else Blocks

view on GitHub

If / Else blocks allow for greater flexibility when creating plans, and reusing plan blocks in multiple situations.

For example, maybe a security certificate is only needed on production servers, so an If/Else block can be inserted so that if the server is in the Production environment, the security certificate will be installed via PowerShell.

Writing Conditions

Conditions inside if statements must be Boolean expressions —that evaluate to either true or false.

OtterScript uses strict Boolean logic. Unlike languages like JavaScript or PowerShell, it does not use truthy or falsy values — expressions like if "hello" or if 0 are invalid. Only true or false (or variables that explicitly hold Boolean values) are allowed in conditions.

✅ Valid: if $isEnabled
❌ Invalid: if $someString or if $count (where $count is a number)

Common expressions:

Example Description
$foo == "bar" Equality check
$myFlag == true Boolean comparison
$myList.Contains("abc") Membership test
$count > 3 Numerical comparison

Logical Operators

You can combine conditions using:

  • && (AND)
  • || (OR)
  • ! (NOT)

Example:

if ($myEnvironment == "Production" && $isEnabled)
{
    Log-Information "Environment is Production and the feature is enabled.";
}

You can wrap expressions in parentheses for clarity and precedence:

if (($myEnvironment == "Staging") || ($myRole == "WebServer"))
{
    Log-Information "Either the environment is Staging or the role is WebServer.";
}

Booleans in OtterScript

Boolean variables are commonly used in conditionals. You can write:

if $isEnabled

or

Which is equivalent to:

if $isEnabled == true

OtterScript emphasizes declarative logic, so conditionals often reflect current environment state (e.g., $Environment == "Production"), rather than being procedural logic blocks.

Nesting if/else Blocks

OtterScript lets you nest if/else statement blocks inside each other for complex decision trees:

if $myEnvironment == "Production"
{
    if $myRole == "Database"
    {
        Log-Information "Environment is Production and role is Database.";
    }
    else
    {
        Log-Information "Environment is Production but role is not Database.";
    }
}
else
{
    Log-Information "Environment is not Production.";
}

if/elseif/else Behavior in OtterScript

OtterScript does not have a native elseif. To write if/elseif/else logic, you simply nest if statements inside else blocks:

if $myEnvironment == "Production"
{
    Log-Information "Environment is Production.";
}
else
{
    if $myEnvironment == "Staging"
    {
        Log-Information "Environment is Staging.";
    }
    else
    {
        Log-Information "Environment is neither Production nor Staging.";
    }
}