PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

Geek Sync Live

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Understanding break, continue, return, and exit

Do you know off-hand what "break", "continue", "return", and "exit" do? These are powerful language constructs, and here is a test function to illustrate how different their effects are:


function Test-Function {
    $fishtank = 1..10

    Foreach ($fish in $fishtank)
        if ($fish -eq 7)
            break      # <- abort loop
            #continue  # <- skip just this iteration, but continue loop
            #return    # <- abort code, and continue in caller scope
            #exit      # <- abort code at caller scope 

        "fishing fish #$fish"



'Script done!'

Simply comment out one of the keywords and run the script to see how the loop behaves.

Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.