Confusion with using Break and nested if statements

Viewed 142

where does a break bring me? If I have nested if's does a break bring me 1 up or all the way out?

It this example below if I want to get to the if ($UTC) how do I get there?

If ($UTC) {
    If (!$NoSeperators) {  
        If ($JustDate) {
            $FullPath = Join-AnyPath $DirectoryName ((Get-Date).ToUniversalTime().ToString("yyyy_MM_dd-") + ($ReportNameExt))
            Break 
        }

        IF ($NoSeconds) { 
            $FullPath = Join-AnyPath $DirectoryName ((get-date).ToUniversalTime().ToString("yyyy_MM_ddThhmm-") + ($ReportNameExt))
            Break
        }

        $FullPath = Join-AnyPath $DirectoryName ((Get-Date).ToUniversalTime().ToString("yyyy_MM_ddThhmmss-") + ($ReportNameExt))
      
    }
    # Remove dash and underscores with NoSeperators switch.
    IF ($NoSeperators) {
        IF ($JustDate) {
            $FullPath = Join-AnyPath $DirectoryName ((Get-Date).ToUniversalTime().ToString("yyyyMMdd") + ($ReportNameExt))
            Break
        }

        IF ($NoSeconds) {
            $FullPath = Join-AnyPath $DirectoryName ((get-date).ToUniversalTime().ToString("yyyyMMddThhmm") + ($ReportNameExt))
            Break
        }

        $FullPath = Join-AnyPath $DirectoryName ((Get-Date).ToUniversalTime().ToString("yyyyMMddThhmmss") + ($ReportNameExt))
    }
}
2 Answers

The break and continue statements only apply to:

Notably, they are not meaningful in if statements and in the pipeline (such as in script blocks passed to the ForEach-Object and Where-Object cmdlets).

Outside these contexts, break and continue should be avoided, because their behavior is treacherous:

  • PowerShell looks up the call stack for an enclosing loop / switch statement anywhere and breaks out of that.

    • For instance, if you use break outside of a relevant statement but the script that called yours happens to have called from inside a relevant statement, your script is exited as a whole, and the calling script's statement is broken out of.
  • If there is no such statement on the entire call stack, execution ends quietly altogether.


As for what you tried:

Unless the code snippet in your question is embedded in a break-relevant statement (that you're not showing), your script will exit as a whole when break executes - either way, this is not what you intended.

To solve your problem, use single if statements with elseif branches, as shown in Ashish Jain's helpful answer.

You can try below code, just use the if-else your task would be done.

If ($UTC) {
    If (!$NoSeperators) {  
        If ($JustDate) {
            $FullPath = Join-AnyPath $DirectoryName ((Get-Date).ToUniversalTime().ToString("yyyy_MM_dd-") + ($ReportNameExt))
        }
        elseif ($NoSeconds) { 
            $FullPath = Join-AnyPath $DirectoryName ((get-date).ToUniversalTime().ToString("yyyy_MM_ddThhmm-") + ($ReportNameExt))
        }
        else {
            $FullPath = Join-AnyPath $DirectoryName ((Get-Date).ToUniversalTime().ToString("yyyy_MM_ddThhmmss-") + ($ReportNameExt))
        }
    }
    # Remove dash and underscores with NoSeperators switch.
    else {
        IF ($JustDate) {
            $FullPath = Join-AnyPath $DirectoryName ((Get-Date).ToUniversalTime().ToString("yyyyMMdd") + ($ReportNameExt))
        }
        elseif ($NoSeconds) {
            $FullPath = Join-AnyPath $DirectoryName ((get-date).ToUniversalTime().ToString("yyyyMMddThhmm") + ($ReportNameExt))
        }
        else {
            $FullPath = Join-AnyPath $DirectoryName ((Get-Date).ToUniversalTime().ToString("yyyyMMddThhmmss") + ($ReportNameExt))
        }
    }
}
Related